Computer questions (Elementary)-Java Network Operations Socket implementation client-server-side communication II (Java)
In the previous section, a client sends a request like a server. In this section, the server returns information to the client. The implementation principle is very simple. Based on the original principle, the server outputs a stream, you can implement the input stream on the client. The specific code is as follows:
Server:
Package com. socket. demo; import java. io. bufferedReader; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. io. printWriter; import java.net. serverSocket; import java.net. socket; public class Server {public static void main (String [] args) {try {ServerSocket serverSocket = new ServerSocket (8888); System. out. println ("---------------- server running, start listening for requests ----------------"); Socket socket = serverSocket. accept (); // start listening for InputStream inputStream = socket. getInputStream (); BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream); // obtain the request content String info; while (info = bufferedReader. readLine ())! = Null) {System. out. println ("I Am a server, client request:" + info);} socket. shutdownInput (); // The server returns the information OutputStream outputStream = socket to the client. getOutputStream (); PrintWriter printWriter = new PrintWriter (outputStream); printWriter. write ("Hello client, I have received your information"); printWriter. flush (); // close the resource printWriter. close (); outputStream. close (); bufferedReader. close (); inputStream. close (); socket. close (); serverSocket. close () ;}catch (Exception exception ){}}}
Client:
Package com. socket. demo; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. io. printWriter; import java.net. socket; import java.net. unknownHostException; public class Client {public static void main (String [] args) {try {// send to port 8888 Socket socket = new Socket ("127.0.0.1", 8888 ); // output stream OutputStream output Stream = socket. getOutputStream (); PrintWriter printWriter = new PrintWriter (outputStream); printWriter. write ("Hello on the server, I am a client. "); PrintWriter. flush (); socket. shutdownOutput (); // This cannot be missing !!!!!!!!!!!!!! // Accept the information returned by the server. InputStream inputStream = socket. getInputStream (); BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream); String info; while (info = bufferedReader. readLine ())! = Null) {System. out. println ("I Am a client, the server returns:" + info);} // close the resource bufferedReader. close (); inputStream. close (); printWriter. close (); outputStream. close (); socket. close ();} catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}
The reason is that the output is not closed on the client, that is, this line of code:
Socket. shutdownOutput ();
Note.