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