标签:
纸上得来终觉浅
网络编程涉及的内容很多,它和多线程,I/O是Java的核心,也是Java产生时就很擅长的部分。之前用C和libpcap库做网关上的一个抓包小程序累得要死,光是网卡的选择,绑定和连接就很费劲,实现一个C/S简单的通信连接都很麻烦,主要是可考虑的太多了,现在看到java还有更高级的接口,不需要关注很多东西(现在看来和那些东西死磕真的毫无意义),实现一个简单的通信模拟是在太简单方便了:
1.TCP方式:
Client传送一张图片给Server,Server保存并且返回Client端一条消息:
<pre name="code" class="java">package roadArchitectWeb.Test; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.nio.channels.ServerSocketChannel; import org.junit.Test; import sun.print.resources.serviceui; /*Tcp Socket网络编程*/ public class Test11 { @Test public void client(){ FileInputStream fileInputStream = null; OutputStream outputStream = null; InputStream inputStream = null; Socket socket = null; try { fileInputStream = new FileInputStream(new File("hello1.jpg")); InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); socket = new Socket(inetAddress,8980); outputStream = socket.getOutputStream(); inputStream = socket.getInputStream(); byte[] b = new byte[1024]; int length; while(fileInputStream.available()!=0 && (length=fileInputStream.read(b)) != -1){ outputStream.write(new String(b, 0, length).getBytes()); } if(fileInputStream != null) try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Test11.client()write"); while(inputStream.available()!=0 &&(length=inputStream.read(b))!=-1){ System.out.println("Test11.client():"+new String(b,0,length)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(outputStream != null) try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } if(inputStream != null) try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } if(socket!=null) try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void server(){ FileOutputStream fileOutputStream = null; InputStream inputStream = null; OutputStream outputStream = null; Socket socket = null; ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(8980); socket = serverSocket.accept(); inputStream = socket.getInputStream(); fileOutputStream = new FileOutputStream("hello2.jpg"); byte[] b = new byte[1024]; int length; while(inputStream.available()!=0 && (length = inputStream.read(b)) != -1){ length = inputStream.read(b); fileOutputStream.write(b, 0, length); } if(fileOutputStream != null) try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("收到图片"); outputStream = socket.getOutputStream(); outputStream.write("服务端返回客户端信息".getBytes()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(outputStream != null) try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } if(inputStream != null) try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } if(socket!=null) try { socket.close(); } catch (IOException e) { e.printStackTrace(); } if(serverSocket!=null) try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }2.UDP方式:
Client发送一个消息(返回消息一样,就不做了):
package roadArchitectWeb.Test; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import org.junit.Test; public class Test12 { @Test public void client(){ DatagramSocket datagramSocket= null; try { byte[] buf = "客户端发送的".getBytes(); DatagramPacket datagramPacket = new DatagramPacket(buf,0,buf.length, InetAddress.getByName("127.0.0.1"),9390); datagramSocket = new DatagramSocket(); datagramSocket.send(datagramPacket); System.out.println("Test12.client():已经发送"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (datagramSocket!=null) { datagramSocket.close(); } } } @Test public void server(){ DatagramSocket datagramSocket= null; try { byte[] buf = new byte[30]; DatagramPacket datagramPacket = new DatagramPacket(buf, 0, buf.length); datagramSocket = new DatagramSocket(9390); datagramSocket.receive(datagramPacket); System.out.println("Test12.server():"+new String(datagramPacket.getData(), 0,datagramPacket.getLength())); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(datagramSocket!=null) datagramSocket.close(); } } }这只是高级接口,更多网络编程后面再总结
标签:
原文地址:http://blog.csdn.net/jintao_ma/article/details/51347850