您的位置:首页 > 理论基础 > 计算机网络

---java 网络编程总结

2016-07-15 16:26 543 查看

InetAddress

/*
* inetAddress代表ip地址
* 创建Inetaddress对象:getByName(String name)
* getHostName():获取域名
* getHostAddress:获取IP地址
*/
public class TestInetAddress {
@Test
public void test() throws Exception {
// 创建一个Inetaddress对象
InetAddress ia = InetAddress.getByName("www.baidu.com");
System.out.println(ia); // www.baidu.cm/61.135.169.125
// InetAddress ia2 = InetAddress.getByName("61.135.169.125");
// System.out.println(ia2);/// 61.135.169.125

System.out.println(ia.getHostName());// www.baidu.com
System.out.println(ia.getHostAddress());// 61.135.169.125

// 获取本机ip:getlocalhost
InetAddress ia3 = InetAddress.getLocalHost();
System.out.println(ia3);// tuxianchao_PC/192.168.1.105
System.out.println(ia3.getHostName());// tuxianchao_PC
System.out.println(ia3.getHostAddress());// 192.168.1.105

}
}


tcp

/*
*
* TCP编程例一:客户端给服务端发送信息。服务端输出此信息到控制台上
* 网络编程实际上就是Socket的编程
*/
public class TestTCP1 {
@Test
public void Client() {
Socket socket = null;
OutputStream os = null;
try {
// 1.创建一个Socket的对象,通过构造器指明服务端的IP地址,以及其接收程序的端口号
socket = new Socket("127.0.0.1", 9090);
// 2.getOutputStream():发送数据,方法返回OutputStream的对象
os = socket.getOutputStream();
// 3.具体的输出过程
os.write("我是客户端".getBytes());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 4.关闭相应的流和Socket对象
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

@Test
public void Server() {

// 1.创建一个ServerSocket的对象,通过构造器指明自身的端口号
ServerSocket ss = null;
// 2.调用其accept()方法,返回一个Socket的对象
Socket s = null;
// 3.调用Socket对象的getInputStream()获取一个从客户端发送过来的输入流
InputStream is = null;
try {
ss = new ServerSocket(9090);
System.out.println("服务端已经启动");
s = ss.accept();
is = s.getInputStream();
// 4.对获取的输入流进行的操作
System.out.print("客户端发来:");
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.print(str);
}
System.out.println();
System.out.println(
"收到:" + s.getInetAddress().getHostName() + "    " + s.getInetAddress().getHostAddress() + "的连接");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5.关闭相应的流以及Socket、ServerSocket的对象
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}


/*
*
* TCP编程例二
*/
public class TestTCP2 {
@Test
public void Client() {
Socket s = null;
OutputStream os = null;
InputStream is = null;
try {
s = new Socket("127.0.0.1", 9090);
os = s.getOutputStream();
os.write("我是客户端".getBytes());
s.shutdownOutput();// 显示的告诉服务端发送完毕
is = s.getInputStream();
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

@Test
public void Server() {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
try {
ss = new ServerSocket(9090);
System.out.println("服务端已经启动");
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.print(str);
}
os = s.getOutputStream();
os.write("我已经收到客户端发来的消息".getBytes());
s.shutdownOutput();// 显示的告诉客户端发送完毕

System.out.println();
System.out.println("收到:" + s.getInetAddress().getHostName() + s.getInetAddress().getHostAddress() + "的消息");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (is != null) {

try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {

try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}
}


/*
* TCP编程例三:从客户端发送文件给服务端,服务端保存到本地。
* 并返回“发送成功”给客户端。并关闭相应的连接。
*/
public class TestTCP3 {
@Test
public void Client() {
Socket s = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream is = null;
try {
// 1.创建Socket的对象
s = new Socket("127.0.0.1", 9090);
// 2.从本地获取一个文件发送给服务端
os = s.getOutputStream();
fis = new FileInputStream(new File("C:\\Users\\tuxianchao\\Desktop\\image.jpg"));
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
os.write(b, 0, len);
}
s.shutdownOutput();
// 3.接收来自于服务端的信息
is = s.getInputStream();
byte[] b1 = new byte[1024];
int len1;
while ((len1 = is.read(b1)) != -1) {
String str = new String(b1, 0, len);
System.out.print(str);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 4.关闭相应的流和Socket对象
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

@Test
public void Server() {

ServerSocket ss = null;

Socket s = null;

InputStream is = null;
FileOutputStream fos = null;

OutputStream os = null;
try {
// 1.创建ServerSocket对象
ss = new ServerSocket(9090);
System.out.println("服务端已经启动");
// 2.创建Socket对象
s = ss.accept();
// 3.将从客户端发送来的信息保存到本地
is = s.getInputStream();
fos = new FileOutputStream(new File("C:\\Users\\tuxianchao\\Desktop\\image1.jpg"));
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);// 将文件写出到本地
}

System.out
.println("收到来自于:" + s.getInetAddress().getHostName() + s.getInetAddress().getHostAddress() + "的文件");
System.out.println("文件已经写入本地");
// 4.发送"接收成功"的信息反馈给客户端
os = s.getOutputStream();
os.write("已经收到文件".getBytes());

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5.关闭相应的流和Socket及ServerSocket的对象
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}
}


udp

*
* UDP编程实现发送消息
*
*
*/
public class TestUDP {
@Test
// 发送端
public void Send() {
DatagramSocket ds = null;
try {
// 创建一个DatagramSocket
ds = new DatagramSocket();
// 要发送的信息
byte[] b = "这里是发送的数据".getBytes();
// 创建一个数据报:每一个数据报不能大于64k,都记录着数据信息,
// 将长度为 length 偏移量为 offset 的包发送到指定主机上的指定端口号。
DatagramPacket packet = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 9090);

// 发送数据报
ds.send(packet);
System.out.println("(发送端)发送完毕");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭
if (ds != null) {
ds.close();
}
}
}

@Test
public void Rceive() {

DatagramSocket ds = null;

try {
// 创建DatagramSocket
ds = new DatagramSocket(9090);
// 接收数据的容器
byte[] b = new byte[1024];
// 创建DatagramPacket来接收数据
DatagramPacket packet = new DatagramPacket(b, 0, b.length);

// 接收数据报
ds.receive(packet);
System.out.println("(接收端)收到消息:");
// 输出收到的信息
String str = new String(packet.getData(), 0, packet.getLength());
System.out.println("(接收端)" + str);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭
if (ds != null) {
ds.close();
}
}
}
}


url

/*
*
* URL:统一资源定位符,一个URL的对象,对应着互联网上一个资源。
* 可以通过URL的对象调用其相应的方法,将此资源读取(“下载”)
*
*
*
* public String getProtocol( ) 获取该URL的协议名
*
* public String getHost( ) 获取该URL的主机名
*
* public String getPort( ) 获取该URL的端口号
*
* public String getPath( ) 获取该URL的文件路径
*
* public String getFile( ) 获取该URL的文件名
*
* public String getRef( ) 获取该URL在文件中的相对位置
*
* public String getQuery( ) 获取该URL的查询名
*/
public class TestURL {

@Test
public void Test() {

InputStream is = null;
URL mUrl = null;
try {
// 1.创建一个URL的对象
mUrl = new URL("http://www.baidu.com");
// 2.将服务端的资源读取进来:openStream()
is = mUrl.openStream();
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

// 如果既有数据的输入,又有数据的输出,则考虑使用URLConnection
// 这里把获取到的数据写入到本地文件

InputStream is1 = null;
FileOutputStream fos = null;
try {
URLConnection urlConn = mUrl.openConnection();
is1 = urlConn.getInputStream();
fos = new FileOutputStream(new File("C:\\Users\\tuxianchao\\Desktop\\1.html"));
byte[] b1 = new byte[1024];
int len1;
while ((len1 = is1.read(b1)) != -1) {
fos.write(b1, 0, len1);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is1 != null) {
try {
is1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  网络编程