Computer questions (Elementary)-Java Network Operations Socket implementation client-server-side communication II (Java)

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.