Implement a server, through the text data we send, and then back to capitalization, to achieve a service-side interaction with the client, with over as the closing tag, the specific code is as follows:
Client:
Packagecom.seven.tcp;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.net.Socket; Public classtranclient { Public Static voidMain (string[] args)throwsexception{Socket Socket=NewSocket ("127.0.0.1", 10001); //text data, using a character stream, reading the keyboardBufferedReader reader =NewBufferedReader (NewInputStreamReader (system.in)); //responsible for writingBufferedWriter writer =NewBufferedWriter (NewOutputStreamWriter (Socket.getoutputstream ())); //Read return DataBufferedReader in =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); String Line=NULL; while(line = Reader.readline ())! =NULL){ if("Over". Equals (line)) { Break; } writer.write (line); Writer.newline (); Writer.flush (); System.out.println ("Server return:" +in.readline ()); } reader.close (); Socket.close (); }}
Service side:
Packagecom.seven.tcp;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classTranserver { Public Static voidMain (string[] args)throwsException {serversocket serversocket=NewServerSocket (10001); Socket Socket=serversocket.accept (); //reading DataBufferedReader reader =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //Write back DataBufferedWriter writer =NewBufferedWriter (NewOutputStreamWriter (Socket.getoutputstream ())); String Line=NULL; while(line = Reader.readline ())! =NULL) {writer.write (Line.touppercase ()); Writer.newline (); Writer.flush (); } writer.close (); Socket.close (); } }
This way, you can implement lowercase capitalization, enter over to end the connection = =.
Java Network Programming (6)--Implement a server to capitalize lowercase