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