A classic case of TCP socket communication based on multithreading

Source: Internet
Author: User
Tags readline

Server-side

 PackageCom.thinkvenus.study.socket;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;ImportJava.net.Socket;/*** Server thread processing class*/ Public classThreadserverextendsThread {Privatesocket socket;  Publicthreadserver (socket socket) { This. Socket =socket; }        /*** Actions performed by threads, responding to client requests*/@Override Public voidrun () {InputStream is=NULL; InputStreamReader ISR=NULL; BufferedReader BR=NULL; OutputStream OS=NULL; PrintWriter PW=NULL; Try{ is=Socket.getinputstream (); ISR=NewInputStreamReader (IS); BR=NewBufferedReader (ISR);            String msg;  while(msg = Br.readline ())! =NULL) {System.out.println ("Here is the server side, the client said:" +msg); } socket.shutdowninput ();//Close Input//gets the output stream that responds to the client's requestOS =Socket.getoutputstream (); PW=Newprintwriter (OS); Pw.write ("Welcome You");        Pw.flush (); } Catch(IOException e) {e.printstacktrace (); } finally {                        //Close Resource            Try {                if(PW! =NULL) Pw.close (); if(OS! =NULL) Os.close (); if(BR! =NULL) Br.close (); if(ISR! =NULL) Isr.close (); if(Is! =NULL) Is.close (); if(Socket! =NULL) Socket.close (); } Catch(IOException e) {e.printstacktrace (); }                    }    }    }

 PackageCom.thinkvenus.study.socket;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket;/*** TCP-based socket communication*/ Public classServer { Public Static voidMain (string[] args) {Try{serversocket ServerSocket=NewServerSocket (8888); System.out.println ("***************** server is about to start, waiting for client connection **********"); intCount = 0; //looping through a client's connection             while(true) {Socket Socket= Serversocket.accept ();//Start listening .Threadserver ts =Newthreadserver (socket);                Ts.start (); Count++; System.out.println ("Number of clients:" +count); }                    } Catch(IOException e) {e.printstacktrace (); }            }    }

Client:

 PackageCom.thinkvenus.study.socket;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;ImportJava.net.Socket;Importjava.net.UnknownHostException; Public classClient { Public Static voidMain (string[] args) {Try{Socket Socket=NewSocket ("localhost", 8888); OutputStream OS=Socket.getoutputstream (); PrintWriter PW=NewPrintWriter (OS);//wrapping the output stream as a print streamPw.println ("Username=aaron; Pwd= "+math.random ());                        Pw.flush (); Socket.shutdownoutput ();//Turn off output//get the input stream to get the server-side responseInputStream is =Socket.getinputstream (); InputStreamReader ISR=NewInputStreamReader (IS); BufferedReader BR=NewBufferedReader (ISR);            String msg;  while(msg = Br.readline ())! =NULL) {System.out.println ("I am the client," said the service-side: "+msg); }                        //Close ResourceBr.close ();            Isr.close ();            Is.close ();            Pw.close ();            Os.close ();                    Socket.close (); } Catch(unknownhostexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }    }

A classic case of TCP socket communication based on multithreading

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.