package com.wzy.test;import java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import java.io.inputstreamreader;import java.io.outputstream;import java.io.printwriter;import java.net.Socket;/** * * * @author Wzy * Server Threading Class*/ public classServerthread extends Thread {//socket associated with this threadSocket socket; publicserverthread (socket Socket) { this. Socket =socket; } public voidrun () {printwriter PW=NULL; OutputStream out=NULL; BufferedReader BR=NULL ; InputStreamReader ISR=NULL; InputStream is=NULL; Try{ is= Socket.getinputstream ();//byte input stream//convert a byte stream to a character streamISR =NewInputStreamReader ( is); //to add buffering for an input streamBR =NewBufferedReader (isr); String Info=""; String Info1=NULL; while(info = Br.readline ())! =NULL){ //Looping Client InformationSystem. out. println ("the information that the server receives to the client is:"+info); Info1=info; } System. out. println (info1); //close the input streamSocket.shutdowninput (); //4 getting the output stream, responding to client requests out=Socket.getoutputstream (); PW=NewPrintWriter ( out); Pw.write ("hello, client, I am the server! "); Pw.flush (); //5. Close Resources } Catch(Exception E) {e.printstacktrace (); } finally{ Try{ if(pw!=NULL) {pw.close ();} if( out!=NULL){ out. 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 (); } } }}
package com.wzy.test;import java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.printwriter;import Java.net.inetaddress;import Java.net.serversocket;import java.net.Socket;/** * * * @author wzy * Server*/ public classServer { public Static voidmain (string[] Args) {intCount =0; //for (int A = 0;a<5;a++) { Try { //1. Create a server-side socket that is serversocket, specify the bound port, and listenServerSocket ServerSocket =NewServerSocket (8888);//listening on port 8888System. out. println ("the server waits for the client to connect ...."); //2. Call Accept to start listening, waiting for the client to connectSocket socket =NULL; while(true) {socket= Serversocket.accept ();//AcceptServerthread Serverthread =NewServerthread (socket); Serverthread.start (); System. out. println ("number of customer connections:"+(++count)); InetAddress Address=socket.getinetaddress (); System. out. println ("Client Ip:"+address.gethostaddress ()); }/* 3. Get the input stream and read the client information InputStream is = Socket.getinputstream ();//bytes input Stream//convert byte stream to character stream InputStreamReader ISR = new InputStreamReader (is); Add buffer for input stream BufferedReader br = new BufferedReader (isr); String info = ""; String info1 = null; While (info = Br.readline ())!=null) {//loop Read client information System.out.println ("the Server received the information for the client:" +info) ; Info1 = info; } System.out.println (info1); Close the input stream socket.shutdowninput (); 4 Gets the output stream, responding to the Client's request OutputStream out = Socket.getoutputstream (); PrintWriter pw = new PrintWriter (out); Pw.write ("hello, client, I am the Server!") "); Pw.flush (); 5. Close Resource Pw.close (); Out.close (); Br.close (); Isr.close (); Is.close (); Socket.close (); Serversocket.close ();*/ } Catch(Exception E) {e.printstacktrace (); } }//}}
package com.wzy.test;import java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import java.io.outputstream;import Java.io.printwriter;import java.net.Socket; Import java.net.UnknownHostException;/** * * * @author wzy * Client*/ public classClient { public Static voidmain (string[] Args) {Try { //1. Create a client socket, specify the server address and PortSocket socket =NewSocket ("127.0.0.1",8888); //2. Get the output stream and send the message to the serverOutputStream OS = Socket.getoutputstream ();//byte output streamPrintWriter PW =NewPrintWriter (os);//wrapping the output stream as a print streamPw.write ("hello, server!!! , I am the client"); Pw.flush ();//Refresh CacheSocket.shutdownoutput ();//turn off the output stream//3. Get the input stream and read the response from the serverInputStreaminch=Socket.getinputstream (); InputStreamReader INR=NewInputStreamReader (inch); BufferedReader BRR=NewBufferedReader (inr); String Info=NULL; while(info =brr.readline ())! =NULL) {System. out. println ("message from the client to the Server:"+info); } brr.close (); Inr.close (); inch. Close (); //4. Close ResourcesPw.close (); Os.close (); Socket.close (); } Catch(unknownhostexception E) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(ioexception E) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Java socket programming-based on TCP