Java Simple Implementation Socket

Source: Internet
Author: User

Server Side

 packagesocket;Importjava.io.BufferedReader;Importjava.io.IOException;Importjava.io.InputStreamReader;Importjava.io.PrintWriter;Importjava.net.ServerSocket;Importjava.net.Socket; public classServerluo { publicServerluo () {Try{serversocket SS=NewServerSocket (9999);//Server PortSystem.out.println ("server is listening"); Socket s=ss.accept ();//Monitor//System.out.println ("a");InputStreamReader iw=NewInputStreamReader (s.getinputstream ());//receiving information from the clientBufferedReader br=NewBufferedReader (iw); //System.out.println (s1);PrintWriter PW=NewPrintWriter (s.getoutputstream (),true);//send a message to the client//pw.println ("hello Client you sent the message I have Received!") ");InputStreamReader is=NewInputStreamReader (system.in);//read the contents of the consoleBufferedReader bd=NewBufferedReader (is);  while(true) {String S1=Br.readline (); System.out.println ("the client said:" +s1+ "\ r \ n"); System.out.println ("server said:" + "\ r \ n"); String S2=Bd.readline ();                        Pw.println (s2); }             }Catch(Exception E) {}} public Static voidmain (String []args) {serverluo SL=NewServerluo (); }}

Client Side

 packagesocket;Importjava.io.BufferedReader;Importjava.io.InputStreamReader;Importjava.io.PrintWriter;Importjava.net.Socket; public classClient { publicclient () {Try{Socket SK=NewSocket ("127.0.0.1", 9999); PrintWriter PW=NewPrintWriter (sk.getoutputstream (),true);//sending information to the server//pw.println ("i am the client");InputStreamReader is=NewInputStreamReader (system.in); BufferedReader BB01=NewBufferedReader (is); InputStreamReader im=NewInputStreamReader (sk.getinputstream ());//receiving information from the serverBufferedReader bb02=NewBufferedReader (im); //System.out.println (s);                         while(true) {System.out.println ("the Client said:"); String S3=Bb01.readline ();            Pw.println (s3); String S4=Bb02.readline (); System.out.println ("the Server said:" +s4+ "\ r \ n"); }              }Catch(Exception E) {}} public Static voidmain (String []args) {client cl=NewClient (); }}

Java Simple Implementation Socket

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.