文章字数:340,阅读全文大约需要1分钟
java,net
提供了两个TCP
通信的Socet
类,分别代表客户端和服务端
构造方法 1 2 3 4 5 6 7 8 9 10 Socket(InetAddress address, int port); Socket(InetAddress address, int port, boolean stream); Socket(String host, int prot); Socket(String host, int prot, boolean stream); Socket(SocketImpl impl) Socket(String host, int port, InetAddress localAddr, int localPort) Socket(InetAddress address, int port, InetAddress localAddr, int localPort) ServerSocket(int port); ServerSocket(int port, int backlog); ServerSocket(int port, int backlog, InetAddress bindAddr)
Client 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket; public class Client { public static final int port = 8080 ; public static final String host = "localhost" ; public static void main (String[] args) { System.out.println("Client Start..." ); while (true ) { Socket socket = null ; try { socket = new Socket(host,port); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStream out = new PrintStream(socket.getOutputStream()); System.out.print("请输入: \t" ); String str = new BufferedReader(new InputStreamReader(System.in)).readLine(); out.println(str); String ret = input.readLine(); System.out.println("服务器端返回过来的是: " + ret); if ("OK" .equals(ret)) { System.out.println("客户端将关闭连接" ); Thread.sleep(500 ); break ; } out.close(); input.close(); } catch (Exception e) { System.out.println("客户端异常:" + e.getMessage()); } finally { if (socket != null ) { try { socket.close(); } catch (IOException e) { socket = null ; System.out.println("客户端 finally 异常:" + e.getMessage()); } } } } } }
Server 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket; public class Server { public static final int port = 8080 ; public static void main (String[] args) { System.out.println("Server...\n" ); Server server = new Server(); server.init(); } public void init () { try { ServerSocket serverSocket = new ServerSocket(port); while (true ) { Socket client = serverSocket.accept(); new HandlerThread(client); } } catch (Exception e) { System.out.println("服务器异常: " + e.getMessage()); } } private class HandlerThread implements Runnable { private Socket socket; public HandlerThread (Socket client) { socket = client; new Thread(this ).start(); } public void run () { try { BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); String clientInputStr = input.readLine(); System.out.println("客户端发过来的内容:" + clientInputStr); PrintStream out = new PrintStream(socket.getOutputStream()); System.out.print("请输入:\t" ); String s = new BufferedReader(new InputStreamReader(System.in)).readLine(); out.println(s); out.close(); input.close(); } catch (Exception e) { System.out.println("服务器 run 异常: " + e.getMessage()); } finally { if (socket != null ) { try { socket.close(); } catch (Exception e) { socket = null ; System.out.println("服务端 finally 异常:" + e.getMessage()); } } } } } }