Start the Server side and enter a dead loop to listen to a port for connection requests. Then run the client side, the clients make a connection request, the service side to listen to the request after the client sent back to accept the message, the connection established, start a thread to process the request, and then continue to the dead loop to listen to other requests. After the client enters the string, press ENTER to send the data to the server. The server replies to the client data after reading the data. This request has been processed and the thread that started has died. If the client receives a return data other than "OK", the connection request is sent again and the data is sent, and the server initiates a thread for the connection to respond ... The client exits until the returned data received by the client is "OK".
Service-side code
ImportJava.io.BufferedReader;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;ImportJava.io.InputStreamReader;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public class tcpserver { Public Static Final intPORT =12345;//port number to monitor Public Static void Main(string[] args) {System.out.println ("Server starts ... \ n"); TCPServer Server =NewTCPServer (); Server.init (); } Public void Init() {Try{ServerSocket ServerSocket =NewServerSocket (PORT); while(true) {//Once there is a blockage, it means that the server and the client have been connectedSocket client = Serversocket.accept ();//Handle this connection NewHandlerthread (client); } }Catch(Exception e) {System.out.println ("Server exception:"+ e.getmessage ()); } }Private class handlerthread implements Runnable { PrivateSocket socket; Public Handlerthread(Socket client) {socket = client;NewThread ( This). Start (); } Public void Run() {Try{//Read client dataDataInputStream input =NewDataInputStream (Socket.getinputstream ()); String clientinputstr = Input.readutf ();//Here to pay attention to the client output stream write method corresponds, otherwise it will throw //Eofexception //Processing client dataSystem.out.println ("The server Received:"+ clientinputstr);//Reply message to clientDataOutputStream out =NewDataOutputStream (Socket.getoutputstream ()); System.out.println ("service-side input:");//Send a line of keyboard inputString s =NewBufferedReader (NewInputStreamReader (system.in)). ReadLine (); Out.writeutf (s); Out.close (); Input.close (); }Catch(Exception e) {System.out.println ("Server Run Exception:"+ e.getmessage ()); }finally{if(Socket! =NULL) {Try{Socket.close (); }Catch(Exception e) {socket =NULL; System.out.println ("Server End finally exception:"+ e.getmessage ()); } } } } }}
Client code
ImportJava.io.BufferedReader;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;ImportJava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.net.Socket; Public class TCPClient { Public Static FinalString ip_addr ="localhost";//server address Public Static Final intPORT =12345;//server port number Public Static void Main(string[] args) {System.out.println ("Client-initiated ..."); System.out.println ("When the server-side character is received as \" Ok\ ", the client terminates \ n"); while(true) {Socket socket =NULL;Try{//Create a stream socket and connect it to the specified port number on the specified hostSocket =NewSocket (IP_ADDR, PORT);//Read server-side dataDataInputStream input =NewDataInputStream (Socket.getinputstream ());//Send data to server sideDataOutputStream out =NewDataOutputStream (Socket.getoutputstream ()); System.out.println ("Client input:"); String str =NewBufferedReader (NewInputStreamReader (system.in)). ReadLine (); Out.writeutf (str); String ret = Input.readutf (); System.out.println ("client receives:"+ ret);//Disconnect if "OK" is received if("OK". Equals (ret)) {System.out.println ("The client will close the connection"); Thread.Sleep ( -); Break; } out.close (); Input.close (); }Catch(Exception e) {System.out.println ("Client exception:"+ e.getmessage ()); }finally{if(Socket! =NULL) {Try{Socket.close (); }Catch(IOException e) {socket =NULL; System.out.println ("Client finally exception:"+ e.getmessage ()); } } } } } }
Test results
Java TCP Network programming simple instance