On-Machine topic (beginner)-Java network operation-socket implements client and server-side communication two (Java)

Source: Internet
Author: User

The previous section implemented a client like server-side send request, this section will implement server-to-client callback information, the implementation of the principle is very simple, on the original basis, the server-side implementation of the output stream, the client implementation of the input stream can be, the specific code is as follows:

Server-side:

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 ("Run----------------server, start monitoring requests----------------"); Socket socket=serversocket.accept ();//Start monitoring InputStream inputstream=socket.getinputstream (); BufferedReader bufferedreader=new BufferedReader (New InputStreamReader (InputStream));//Get Request Content string Info;while (( Info=bufferedreader.readline ())!=null) {System.out.println ("I am the server side, client request is:" +info);} Socket.shutdowninput ();//server-side to the client callback information OutputStream Outputstream=socket.getoutputstream (); PrintWriter printwriter=new PrintWriter (outputstream);p rintwriter.write ("Client Hello, I have received your message");p Rintwriter.flush () ;//Close 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 8888 port socket socket =new Socket ("127.0.0.1", 8888);//output Stream OutputStream outputstream=socket.getoutputstream (); PrintWriter printwriter=new PrintWriter (outputstream);p rintwriter.write ("Server side Hello, I am the client. ");p Rintwriter.flush (); Socket.shutdownoutput ();//This is not less!!!!!!!!!!!!!! Receive server-side backhaul information InputStream inputstream=socket.getinputstream (); BufferedReader bufferedreader=new BufferedReader (New InputStreamReader (InputStream)); String Info;while ((Info=bufferedreader.readline ())!=null) {System.out.println ("I am the client, server side returns::" +info);} Close resource Bufferedreader.close (); Inputstream.close ();p rintwriter.close (); Outputstream.close (); Socket.close ();} catch (Unknownhostexception e) {e.printstacktrace ();} catCH (ioexception e) {e.printstacktrace ();}}} 

Run the following example:

Server Console output:


Client Console output:


How do I switch the console?


Click the small button next to this computer to select the console.

There is also a small problem when implementing this feature, as follows:


The reason is that the output is not turned off on the client, that is, this line of code:

Socket.shutdownoutput ();

Be careful here.


Like friends please pay attention to me, thank you

On-Machine topic (beginner)-Java network operation-socket implements client and server-side communication two (Java)

Related Article

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.