Use of sockets

Source: Internet
Author: User

ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket;//Main class Talkclient.class Public classtalkclient{ Public Static voidMain (String args[]) {Try{            //make a request to port 4700 of 127.0.0.1Socket socket =NewSocket ("127.0.0.1", 4700);//            //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 ()); //The input stream is obtained by the socket object and the corresponding BufferedReader object is constructedBufferedReader is=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ()));            String ReadLine; ReadLine=Sin.readline ();  while(!readline.equals ("Bye") {os.println (readline);                Os.flush (); System.out.println ("Client:" +ReadLine); System.out.println ("Server:" +is.readline ()); ReadLine=Sin.readline ();            } os.close ();            Is.close ();        Socket.close (); }Catch(Exception e) {System.out.println ("Error" +e); }    }}
ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; 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);    } 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 outputLine=Sin.readline (); //reads a string from a standard input     while(!line.equals ("Bye") {os.println (line); //output The string to the clientOs.flush (); System.out.println ("Server:" +Line ); System.out.println ("Client:" +is.readline ()); Line=Sin.readline (); }//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); }  }}

Use of sockets

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.