170410. Simple example of Java socket Communication (TCP)

Source: Internet
Author: User
Tags sin

Service-Side code:

 PackageCom.bobohe.socket;ImportJava.io.*; Importjava.net.*; ImportJava.applet.Applet; Public classTalkserver { Public Static voidMain (String args[]) {Try{serversocket server=NULL; Try{Server=NewServerSocket (4700); //Create a ServerSocket to listen for customer requests on port 4700             } Catch(Exception e) {System.out.println ("Can not listen to:" +e); //error, printing error message} socket Socket=NULL; Try{Socket=server.accept (); //use the Accept () block to wait for a customer request, a customer//When the request arrives, a socket object is generated and the execution continues             } Catch(Exception e) {System.out.println ("Error." +e); //error, printing error message} String Line; BufferedReader is=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //The input stream is obtained by the socket object and the corresponding BufferedReader object is constructedprintwriter os=NewPrintWriter (Socket.getoutputstream ()); //The output stream is obtained by the socket object, and the PrintWriter object is constructedBufferedReader sin=NewBufferedReader (NewInputStreamReader (system.in)); //constructing BufferedReader objects from System standard input devicesSystem.out.println ("Client:" +is.readline ()); //print a string read from the client on standard output Line=Sin.readline (); //reads a string from a standard input              while(!line.equals ("Bye")) {                 //If the string is "Bye", the loop is stoppedos.println (line); //output The string to the clientOs.flush (); //refreshes the output stream so that the client receives the string immediatelySystem.out.println ("Server:" +Line ); //Print the Read-in string on the system standard outputSystem.out.println ("Client:" +is.readline ()); //reads a string from the client and prints it to the standard output Line=Sin.readline (); //reads a string from the system standard input             } //Continue loopingos.close ();//close the socket output streamis.close ();//close the socket input streamsocket.close ();//Close Socketserver.close ();//Close ServerSocket         } Catch(Exception e) {System.out.println ("Error:" +e); //error, printing error message         }     } }

Client code:

 PackageCom.bobohe.socket;ImportJava.io.*; Importjava.net.*;  Public classtalkclient { Public Static voidMain (String args[]) {Try{Socket Socket=NewSocket ("127.0.0.1", 4700); //make a customer request to port 4700 on this machineBufferedReader sin=NewBufferedReader (NewInputStreamReader (system.in)); //constructing BufferedReader objects from System standard input devicesprintwriter os=NewPrintWriter (Socket.getoutputstream ()); //The output stream is obtained by the socket object, and the PrintWriter object is constructedBufferedReader is=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //The input stream is obtained by the socket object and the corresponding BufferedReader object is constructedString ReadLine; ReadLine= Sin.readline ();//reads a string from the system standard input              while(!readline.equals ("Bye")) {                 //stop Looping if the string read in from standard input is "bye"os.println (ReadLine); //output A string read from the system standard input to the serverOs.flush (); //refreshes the output stream so that the server receives the string immediatelySystem.out.println ("Client:" +ReadLine); //Print the Read-in string on the system standard outputSystem.out.println ("Server:" +is.readline ()); //reads a string from the server and prints it to the standard outputReadLine= Sin.readline ();//reads a string from the system standard input             } //Continue loopingos.close ();//close the socket output streamis.close ();//close the socket input streamsocket.close ();//Close Socket         } Catch(Exception e) {System.out.println ("Error" + e);//Error, print error message         }     } }

170410. Simple example of Java socket Communication (TCP)

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.