Socket communication Basics for Java communication

Source: Internet
Author: User

The two programs that are in the network environment communicate with each other through an interactive connection. Each connected communication end is called a socket. A complete socket communication program should contain the following steps:

① Create socket;

② Open the input/output stream connected to the socket;

④ the socket to read and write according to certain protocol;

④ close the socket.

Socket communication is divided into two parts: Server side and client. Server-side must start first, and then wait for the socket connection, if the connection is reached to start the input and output operation, through the input stream to obtain the client's request information, through

The output stream returns the client information. The client simply creates a socket-specific IP and port number for the server.

Look at the server code below:

 Public classSocketserver { Public Static intPort = 3333;  Public StaticString IP = "172.30.1.101";  Public Static voidMain (string[] args)throwsIOException {//Start the serverServerSocket Server =NewServerSocket (port); System.out.println ("Server running ..."); //Monitor SocketSocket socket =server.accept (); //IO streamBufferedReader br =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); PrintWriter PW=NewPrintWriter (NewOutputStreamWriter (Socket.getoutputstream ())); String Client=NULL; //If you receive the exit signal from the client, close the socket         while(!" Exit ". Equalsignorecase (client =Br.readline ())) {System.out.println ("From client say:" +client); Pw.println ("Server Get your info:" +client);          Pw.flush ();          } br.close ();                    Pw.close ();          Socket.close ();      Server.close (); }  }  

See the client code below:

 Public classsocketclient { Public Static voidMain (string[] args)throwsIOException {//connecting to a serverSocket socket =NewSocket (Socketserver.ip,socketserver.port); System.out.println ("Client Running ..."); System.out.println ("-----Insert your info:"); //users enter information sent to the server in the consoleBufferedReader Brconsole =NewBufferedReader (NewInputStreamReader (system.in)); //IO streamPrintWriter PW =NewPrintWriter (Socket.getoutputstream ()); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); String Server=NULL; //if the user enters exit, the socket is closed         while(!" Exit ". Equalsignorecase (Server =Brconsole.readline ()))            {pw.println (server);            Pw.flush (); System.out.println ("From server say:" +br.readline ()); }        //when the user enters exit, the exit message is sent to the server and the server closes the current socketpw.println (server);                Pw.flush ();        Pw.close ();        Brconsole.close ();                Br.close ();    Socket.close (); }}

It is important to note that:

When testing, start the server first, and then start the client;

Use PrintWriter when writing data, be sure to use println, because the other side uses Bufferreader ReadLine to fetch data, if you use print, Bufferreader cannot read the data.

This is only a base instance and does not support concurrency.

Socket communication Basics for Java communication

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.