Achieve one-on-one free chat

Source: Internet
Author: User

Client

 Packagecn.qianfeng.kaoti03;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintStream;ImportJava.net.Socket;classReceivesocketImplementsRunnable {PrivateSocket S;  PublicReceivesocket (Socket s) {Super();  This. S =s; } @Override Public voidrun () {//TODO auto-generated Method StubBufferedReader br =NULL; Try {             while(true) {br=NewBufferedReader (NewInputStreamReader (S.getinputstream ())); String Line=Br.readline (); if(Line! =NULL) {System.out.println (line); }            }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }}classSendsocketImplementsRunnable {PrivateSocket S;  PublicSendsocket (Socket s) {Super();  This. S =s; } @Override Public voidrun () {//TODO auto-generated Method StubBufferedReader br =NULL; Try {             while(true) {br=NewBufferedReader (NewInputStreamReader (system.in)); PrintStream PS=NewPrintStream (S.getoutputstream (),true); String Line=Br.readline (); Ps.println ("A:" +Line ); }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }} Public classTestsocket { Public Static voidMain (string[] args) {Try{Socket s=NewSocket ("localhost", 8888); NewThread (NewSendsocket (s)). Start (); NewThread (NewReceivesocket (s)). Start (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Service side

 Packagecn.qianfeng.kaoti03;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket;classSendserversocketImplementsRunnable {PrivateSocket S;  PublicSendserversocket (Socket s) {Super();  This. S =s; } @Override Public voidrun () {//TODO auto-generated Method StubBufferedReader br =NULL; PrintWriter PS=NULL; Try {             while(true) {br=NewBufferedReader (NewInputStreamReader (system.in)); PS=NewPrintWriter (S.getoutputstream (),true); String Line=Br.readline (); if(Line! =NULL) {ps.println ("B:" +Line ); }            }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }}classReceiveserversocketImplementsRunnable {PrivateSocket S;  PublicReceiveserversocket (Socket s) {Super();  This, sb=s; } @Override Public voidrun () {//TODO auto-generated Method StubBufferedReader br =NULL; Try {             while(true) {br=NewBufferedReader (NewInputStreamReader (S.getinputstream ())); String Line=Br.readline (); if(Line! =NULL) {System.out.println (line); }            }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }} Public classTestserversocket { Public Static voidMain (string[] args) {Try{ServerSocket ss=NewServerSocket (8888); Socket s=ss.accept (); System.out.println (S.getinetaddress (). GetHostName ()+ "..... connected"); NewThread (NewSendserversocket (s)). Start (); NewThread (NewReceiveserversocket (s)). Start (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Achieve one-on-one free chat

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.