Java Socket Example 01

Source: Internet
Author: User

Server-side code:

 PackageServerSide;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classEchoserver {Private intPort = 8088; PrivateServerSocket ServerSocket;  PublicEchoserver () {Try{ServerSocket=NewServerSocket (port); } Catch(IOException e) {e.printstacktrace (); }    }         Publicstring Echo (String msg) {return"Echo:" +msg; }        Privateprintwriter getwriter (socket socket) {OutputStream socketout=NULL; Try{socketout=Socket.getoutputstream (); } Catch(IOException e) {e.printstacktrace (); }        return NewPrintWriter (Socketout,true); }        PrivateBufferedReader Getreader (socket socket) {InputStream Socketin=NULL; Try{Socketin=Socket.getinputstream (); } Catch(IOException e) {e.printstacktrace (); }        return NewBufferedReader (NewInputStreamReader (Socketin)); }         Public voidService () { while(true) {Socket Socket=NULL; Try{Socket=serversocket.accept (); System.out.println ("New Connection accepted" + socket.getinetaddress () + ":" +Socket.getport ()); BufferedReader BR=Getreader (socket); PrintWriter PW=getwriter (socket); String msg=NULL;  while(msg = Br.readline ())! =NULL) {System.out.println (msg);                    Pw.println (Echo (msg)); if(Msg.equals ("Bye"))                          Break; }            }            Catch(IOException e) {e.printstacktrace (); }            finally {                Try {                    if(Socket! =NULL) {socket.close (); }                }                Catch(IOException e) {
 Packageclientside;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;ImportJava.net.Socket; Public classechoclient {Private StaticString host = "10.11.121.239"; Private Static intPort = 8088; Privatesocket socket;  Publicechoclient (String hostname) {host=hostname; Try{Socket=NewSocket (host, Port); } Catch(IOException e) {e.printstacktrace (); }    }         Publicechoclient () { This(host); }            Privateprintwriter getwriter (socket socket) {OutputStream socketout=NULL; Try{socketout=Socket.getoutputstream (); } Catch(IOException e) {e.printstacktrace (); }        return NewPrintWriter (Socketout,true); }            PrivateBufferedReader Getreader (socket socket) {InputStream Socketin=NULL; Try{Socketin=Socket.getinputstream (); } Catch(IOException e) {e.printstacktrace (); }        return NewBufferedReader (NewInputStreamReader (Socketin)); }             Public voidTalk () {Try{bufferedreader br=Getreader (socket); PrintWriter PW=getwriter (socket); BufferedReader Localreader=NewBufferedReader (NewInputStreamReader (system.in)); String msg=NULL;  while(msg = Localreader.readline ())! =NULL) {pw.println (msg);                                System.out.println (Br.readline ()); if(Msg.equals ("Bye"))                     Break; }        }        Catch(IOException e) {e.printstacktrace (); }        finally {            Try{socket.close (); }            Catch(IOException e) {e.printstacktrace (); }        }    }             Public Static voidMain (string[] args) {Newechoclient (). talk (); }}

                    e.printstacktrace ()    ;         }}}  Public Static void throws IOException {                new  echoserver (). Service ();    }}

Client Code:

Java Socket Example 01

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.