I use Java to achieve the Flying Pigeon Book 1-To achieve communication

Source: Internet
Author: User

The first step:

Establish server-side client for end-to-end communication. The TCP/IP protocol is used because there is a large semaphore to pass the file.

Both the server and the client have to set up sockets and then communicate through the socket.

The service-side code is as follows:

ImportJava.io.PrintStream;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classServer { Public Static voidMain (string[] args) {//Create a service-side socket for receiving network informationServerSocket ServerSocket =NULL; Try {            //Server socket using local IP, port number using 36000ServerSocket =NewServerSocket (36000); //ensure continuous operation on the service side             while(true){                //receiving network information, not receiving it, blocking the current threadSocket socket =serversocket.accept (); //get the service-side output streamPrintStream PS =NewPrintStream (Socket.getoutputstream ()); //append information to the output streamPs.append ("Hello, this is the service side!") "); //turn off the output stream and socketPs.close ();            Socket.close (); }        } Catch(Exception e) {e.printstacktrace (); }    }}

The client code is as follows:

ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.net.Socket; Public classClient { Public Static voidMain (string[] args) {BufferedReader reader=NULL; Socket Socket=NULL; Try {            //Create a client socket to connect to the serverSocket =NewSocket ("127.0.0.1", 36000); //read the byte stream from the service sideReader =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //reads the information in the byte stream and outputs it as a stringStringBuilder Builder =NewStringBuilder ();            String Line;  while((Line=reader.readline ())! =NULL) {builder.append (line). Append ("\ n");            } System.out.println (Builder.tostring ()); //close reader and socketReader.close ();        Socket.close (); } Catch(Exception e) {e.printstacktrace (); }    }}

Run the server first, then run the client, and you will see the information in the console output expected:

I use Java to achieve the Flying Pigeon Book 1-To achieve communication

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.