Java Socket Programming

Source: Internet
Author: User

Simulating Java Scoket Server and client programming

ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket; Public classtalkclient { Public Static voidMain (String args[]) {Try {            //make a customer request to port 8000 on this machineSocket socket =NewSocket ("127.0.0.1", 8000); //constructing BufferedReader objects from System standard input devicesBufferedReader sin =NewBufferedReader (NewInputStreamReader (system.in)); //The output stream is obtained by the socket object, and the PrintWriter object is constructedPrintWriter OS =NewPrintWriter (Socket.getoutputstream ());            String readline,receive; ReadLine= Sin.readline ();//reads a string from the system standard inputBufferedReader is =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ()));  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 send:" +ReadLine); //Thread.Sleep (+);Receive =Is.readline ();                System.out.println (receive); //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) {} }}
ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classTalkserver { Public Static voidMain (String args[]) {Try{serversocket server=NULL; Try{Server=NewServerSocket (8000); //Create a ServerSocket to listen for customer requests on port 8000}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 constructedline =Is.readline (); //reads a string from a standard input             while(!line.equals ("Bye")) {                //If the string is "Bye", the loop is stoppedOS.PRINTLN ("Server Send:yeah," +Line ); //output The string to the clientOs.flush (); //refreshes the output stream so that the client receives the string immediatelySystem.out.println ("Server Receive:" +Line ); //reads a string from the client and prints it to the standard outputline =Is.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) {} }}

Java Socket Programming

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.