Java网络通信问题总结

//ServerTest.java import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class ServerTest { public static void main(String[] args){ ServerSocket ss = null; Socket s = null; try{ ss = new ServerSocket(2222); }catch(IOException e){ e.printStackTrace(); System.exit(-1); }while(true){ try{ s= ss.accept(); System.out.println("LocalPort:" + s.getLocalPort()); System.out.println("Port:" + s.getPort()); System.out.println("InetAddress:" + s.getInetAddress().getHostAddress()); System.out.println("LocalAddress:" + s.getLocalAddress().getHostAddress()); //只读发来消息的线程 new Thread(new MessageRead(s,"Client")).start(); new Thread(new MessageWrite(s,"Server")).start(); }catch (IOException e){ e.printStackTrace(); System.exit(-1); } }// ss.close(); } }

//NetTest.java //java.lang.*; math包 基本类型封装对象 System Enum String* Thread* Exception* //java.io.*; 流 RW流 Stream流 文件操作File //java.util.*; 容器 date Iterator 泛型 list set map 计时器Timer //java.sql.*; //java.net.*; //java.nio.*; import java.io.*; import java.net.Socket; public class NetTest { public static void main(String[] args){ Socket s = null; try{ s = new Socket("127.0.0.1",2222); //只读发来消息的线程 new Thread(new MessageRead(s,"Server")).start(); new Thread(new MessageWrite(s,"Client")).start(); //由此线程销毁套接字}catch(IOException e){ e.printStackTrace(); System.exit(-1); }} } class MessageWrite implements Runnable { Socket s; String localName; MessageWrite(Socket s,String localName) { this.s = s; this.localName = localName; }@Override public void run() { DataOutputStream dos = null; BufferedReader br = null; try { dos = new DataOutputStream(s.getOutputStream()); br = new BufferedReader(new InputStreamReader(System.in)); String sb; while (!(sb = br.readLine()).equalsIgnoreCase("exit")) {System.out.print(localName + ":" + sb); System.out.println(); dos.writeUTF(sb); }br.close(); //从输入设备中读入 dos.flush(); //写到Socket dos.close(); //关闭Socket流//让消息发送对象接管Socket关闭 s.close(); //关闭Socket } catch (IOException e) { e.printStackTrace(); System.exit(-1); }} } class MessageRead implements Runnable{ Socket s; String remoteName; MessageRead(Socket s,String remoteName){ this.s = s; this.remoteName = remoteName; }@Override public void run() { DataInputStream dis = null; try{ dis = new DataInputStream(s.getInputStream()); while(true){ System.out.print(remoteName + ":" + dis.readUTF()); System.out.println(); }//dis.close(); //从Socket读入 }catch(IOException e){ e.printStackTrace(); System.exit(-1); }} }

//UDPRecive.java import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class UDPRecive { public static void main(String[] args){ byte[] buffer = new byte[1024]; DatagramPacket dp = new DatagramPacket(buffer,buffer.length); DatagramSocket ds = null; DataInputStream dis = null; ByteArrayInputStream bais = null; try { ds = new DatagramSocket(6666); } catch (SocketException e) { e.printStackTrace(); }while(true){ try { ds.receive(dp); } catch (IOException e) { e.printStackTrace(); }bais = new ByteArrayInputStream(buffer); dis = new DataInputStream(bais); try { System.out.println(dis.readLong()); } catch (IOException e) { e.printStackTrace(); }} } }

//UDPSend.java import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException; public class UDPSend { public static void main(String[] args){ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); try { dos.writeLong(12345678900l); } catch (IOException e) { e.printStackTrace(); }byte[] buffer = baos.toByteArray(); DatagramPacket dp = new DatagramPacket(buffer,buffer.length,new InetSocketAddress("127.0.0.1",6666)); DatagramSocket ds = null; try { ds = new DatagramSocket(); } catch (SocketException e) { e.printStackTrace(); }try { ds.send(dp); } catch (IOException e) { e.printStackTrace(); } ds.close(); } }

    推荐阅读