Socket 发表于 2018-08-30 | 分类于 技术 Socket Socket1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283public class MainServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(4000); while (true) { Socket socket = serverSocket.accept(); System.out.println("connect..."); new ServerInputThread(socket).start(); new ServeOutputThread(socket).start(); } }}public class ServerInputThread extends Thread { private Socket socket; public ServerInputThread(Socket socket) { this.socket = socket; } @Override public void run() { while (true) { try { InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int length = inputStream.read(bytes); String str = new String(bytes, 0, length); System.out.println("waiting for read..."); System.out.println(str); } catch (IOException e) { e.printStackTrace(); } } }}public class ServeOutputThread extends Thread { private Socket socket; public ServeOutputThread(Socket socket) { this.socket = socket; } @Override public void run() { while (true) { try { OutputStream outputStream = socket.getOutputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String line = bufferedReader.readLine(); outputStream.write(line.getBytes()); } catch (IOException e) { e.printStackTrace(); } } }} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778public class MainClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1",4000); new ClientInputThread(socket).start(); new ClientOutputThread(socket).start(); }}public class ClientInputThread extends Thread { private Socket socket; public ClientInputThread(Socket socket) { this.socket = socket; } @Override public void run() { while (true) { try { InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int length = inputStream.read(bytes); String str = new String(bytes, 0, length); System.out.println("waiting for read..."); System.out.println(str); } catch (IOException e) { e.printStackTrace(); } } }}public class ClientOutputThread extends Thread { private Socket socket; public ClientOutputThread(Socket socket) { this.socket = socket; } @Override public void run() { while (true) { try { OutputStream outputStream = socket.getOutputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String line = bufferedReader.readLine(); outputStream.write(line.getBytes()); } catch (IOException e) { e.printStackTrace(); } } }} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748public class UdpTest1 { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(); String str = "hello world"; DatagramPacket packet = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName("localhost"), 7000); socket.send(packet); byte[] buffer = new byte[1000]; DatagramPacket packet2 = new DatagramPacket(buffer, 100); socket.receive(packet2); System.out.println(new String(buffer, 0, packet2.getLength())); socket.close(); }}public class UdpTest2 { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(7000); byte[] buffer = new byte[1000]; DatagramPacket packet = new DatagramPacket(buffer,1000); socket.receive(packet); System.out.println(new String(buffer, 0, packet.getLength())); String str = "welcome"; DatagramPacket packet2 = new DatagramPacket(str.getBytes(), str.length(), packet.getAddress(), packet.getPort()); socket.send(packet2); socket.close(); }}