Server.java
1 Packagesocket;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.DataInputStream;5 ImportJava.io.DataOutputStream;6 Importjava.io.IOException;7 ImportJava.io.InputStream;8 ImportJava.io.InputStreamReader;9 ImportJava.io.OutputStream;Ten ImportJava.net.ServerSocket; One ImportJava.net.Socket; A - Public classServer { - //ports to listen on the Public Static Final intPORT = 54039; - - Public Static voidMain (string[] args) - { + NewServer (). Start (); - } + A //How to start a listener thread at Public voidStart () - { -ServerSocket ServerSocket =NULL; - Try { - //Create a new ServerSocket object that listens on port ports and may throw IOException -ServerSocket =NewServerSocket (PORT); inSystem.out.println ("Serverscoket:" +serversocket); - //Cyclic monitoring to while(true) + { - //Gets the client connection, if none is blocked theSocket Clientsocket =serversocket.accept (); * //start the thread to process the heard data and start the thread $ NewThread (Newdatarunnable (Clientsocket)). Start ();Panax Notoginseng - } the}Catch(IOException e) { + e.printstacktrace (); A}finally{ the if(NULL!=ServerSocket) + { - Try { $ serversocket.close (); $System.out.println ("Close ServerSocket success!"); -}Catch(IOException e) { -System.out.println ("Close ServerSocket failed!"); the } - }Wuyi } the - } Wu - /** About * Job handling of listening information $ */ - Private classDatarunnableImplementsRunnable - { - Socket client; A + Publicdatarunnable (Socket client) { the Super(); - This. Client =client; $ } the the Public voidrun () { the Try { the //read client socket fetch input stream, package into DataInputStream -DataInputStream DataIn =NewDataInputStream (Client.getinputstream ()); inDataOutputStream dataout =NewDataOutputStream (Client.getoutputstream ()); theString content = Datain.readutf ();//There is a read other format method, see the API specifically the //Print to console AboutSYSTEM.OUT.PRINTLN ("content read in from client:" +content); the the //Reply to client information theSystem.out.println ("Please enter what you want to reply to:"); +String reply =NewBufferedReader (NewInputStreamReader (system.in)). ReadLine (); - Dataout.writeutf (reply); the Bayi if("Close". Equals (content)) the { theSYSTEM.OUT.PRINTLN ("Client had been closed, I want to be closed too"); - } - datain.close (); the dataout.close (); the}Catch(IOException e) { the e.printstacktrace (); the } - the } the the }94}View Code
Client.java
1 Packagesocket;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.DataInputStream;5 ImportJava.io.DataOutputStream;6 Importjava.io.IOException;7 ImportJava.io.InputStream;8 ImportJava.io.InputStreamReader;9 ImportJava.net.Socket;Ten Importjava.net.UnknownHostException; One A Public classClient { - Public Static FinalString ADDR = "localhost";//Monitor this machine - Public Static Final intPORT = 54039;//Port the - Public Static voidMain (string[] args) - { - while(true) + { -Socket client =NULL; +DataInputStream DataIn =NULL; ADataOutputStream dataout =NULL; at Try { - //Create a client socket to connect to the host server side -Client =NewSocket (ADDR, PORT); - -DataIn =NewDataInputStream (Client.getinputstream ()); -Dataout =NewDataOutputStream (Client.getoutputstream ()); in - //sending data to the server toSystem.out.println ("Please enter data:"); +String content =NewBufferedReader (NewInputStreamReader (system.in)). ReadLine (); - Dataout.writeutf (content); the * //read the data returned by the server side $String reply =Datain.readutf ();Panax NotoginsengSYSTEM.OUT.PRINTLN ("Server reply:" +reply); - the}Catch(unknownhostexception e) { + //TODO auto-generated Catch block A e.printstacktrace (); the}Catch(IOException e) { + //TODO auto-generated Catch block - e.printstacktrace (); $ } $ finally{ - Try { - if(NULL!=datain) Datain.close (); the if(NULL!=dataout) Dataout.close (); - if(NULL!=client) Client.close ();Wuyi}Catch(IOException e) { the //TODO auto-generated Catch block - e.printstacktrace (); Wu } - } About } $ } -}View Code
Socket--java