Reprint Please specify : Http://blog.csdn.net/uniquewonderq
Ideas:
The test code for the server segment is as follows:
:
Package Com.test;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.inputstream;import Java.io.inputstreamreader;import Java.net.serversocket;import Java.net.socket;import Java.util.logging.Level; Import java.util.logging.logger;/** * * @author WONDERQ */public Class Server {//server-side public static void main (string[] args) {try {//1. Create a server-side socket, that is, ServerSocket, specify the bound port, and listen for this port. ServerSocket serversocket=new ServerSocket (8888); 2. Call ServerSocket's Accept () method, waiting for the client's connection System.out.println ("= = Server is about to start, waiting for client connection = ="); Socket socket=serversocket.accept (); 3. Gets the input stream used to read the login information issued by the client InputStream Is=socket.getinputstream ();//Byte input stream InputStreamReader isr=new in Putstreamreader (IS);//convert bytes into a character stream//add buffer to a character stream BufferedReader bufferedreader=new BufferedReader (ISR); String Info=null; Iterates through the client-submitted information while (Info=<span style= "font-family:arial, Helvetica, Sans-serif; " >bufferedreader.readline () </span>)!=null) {System.out.println ("I am the server, the client submits the information is:" +info); } socket.shutdowninput (); 4. Close the relevant resource bufferedreader.close (); Is.close (); Isr.close (); Socket.close (); Serversocket.close (); } catch (IOException ex) {Logger.getlogger (Server.class.getName ()). log (Level.severe, NULL, ex); } }}
The client is as follows:
Package Com.test;import Java.io.ioexception;import Java.io.outputstream;import java.io.printwriter;import Java.net.socket;import java.util.logging.level;import java.util.logging.logger;/** * * @author WONDERQ */public class Client {//clients public static void Main (string[] args) { try { //1. Create client socket, specify server address and port number socket socket =new Socket ("127.0.0.1", 8888); 2. Gets the output stream used to send information to the server outputstream os=socket.getoutputstream ();//byte output stream //convert to print stream printwriter pw=new PrintWriter (OS); Pw.write ("User name: Wonderq; Password: root"); Pw.flush ();//flush cache, output information to server side //Turn off output stream socket.shutdownoutput (); 3. Close Resource pw.close (); Os.close (); Socket.close (); } catch (IOException ex) { Logger.getlogger (Client.class.getName ()). log (Level.severe, NULL, ex);}}}
After that, run the server side and run the results:
= = Server is about to start, waiting for client connection = =
Then run the client, no output. At this point, to see the output of the server-side class, the discovery has changed:
This is:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Socket Application (v)--programming for TCP-based socket communication