Socket class When the client needs to communicate with the server program, the client program creates a socket object in the client, and the Socket class has several constructor. Two commonly used constructors are socket (Inetaddress Addr, INT port), and socket (String Host, INT port), two constructor created a streamlined text of Socket-based connection server-side stream jacket. For the first inetaddress subclass object, the IP address of the server host is obtained through the ADDR parameter. For the second function Host parameter package is assigned to the inetaddress object, if there is no IP address consistent with the Host parameter, then the unknownhostexception exception object will be thrown . Both functions get the port number of the server through the parameter port. Assume that the connection has been established, the network API will bundle the IP address and any port number of the client based on the socket-based stream socket, otherwise the two functions will throw an IOException object. If a socket object is created, it may obtain the information transmitted from the service program by calling the GetInputStream () method of the socket, or the output stream can also be sent by calling the getoutputstream () method to call the socket. After the read and write activity is completed, the client program calls the close () method to close the flow and streamlined connections. The following code creates a service host address of 198.163.227.6, port number 13 Socket object, and then created from this new creation The input stream is read in the Socket object, and then turn off the stream and socket objects.
Socket S = New Socket ("198.163.227.6", 13); InputStream IS = S.GetinputStream (); // read from the stream.isclose (); s.close ();
Next, we will demonstrate a jacket reader, this program will create a socket object, and the socket will access the service program running on the specified host port 1000. If the access success client will send a series of commands to the service program and The response of the print service program. List2 allows us to create program SSClient's source code: Listing 2: ssclient.java
// ssclient.javaimport java.io. *; Import java.net. *; Class ssclient {public static void main (string "args) {string host =" localhost "; // if user specifies a command-line Argument, That argument // represents the host name. if (args.length == 1) Host = args [0]; bufferedreader br = null; printwriter pw = null; socket s = null; try {// create a socket That Attempts To connect to the server // program on the host at port 10000. s = new Socket (host, 10000);. // Create an input stream reader that chains to the socket's // byte-oriented input stream The input stream reader // converts bytes read from the socket to characters The // conversion is based on the platform's default character // set InputStreamReader isr;.. isr = new InputStreamReader (s.getInputStream ()); // Create a buffered reader that chains to the input Stream // reader. The Buffered Reader Supplies a communication method method // forreading entire lines of text. br = new bufferedreader isr); // Create a print writer that chains to the socket's // oriented output stream byte- The print writer creates an // intermediate output stream writer that converts // characters sent to the socket to bytes The conversion // is.. Based on The Platform's Default Character Set. Pw = new printwriter (s.getoutputstream (), true); // send the date command to the server. Pw.println ("Date"); // Obtain and Print The Current Date / Time. System.Out.println (br.readline ()); // send The Pause Command to the Server. this allows seved //clients to start and verifies That the Server is spawning // Multiple threads. Pw.println (" Pause ");
// send the dow command to the server. Pw.println ("dow"); // Obtain and print the current day of week. System.out.println (br.readline ()); // send the dom record to The server. Pw.println ("DOM"); // Obtain and print the current day of month. system.out.println (br.readline ()); // send the Doy Command to the Server. Pw.println ( "DOY"); // Obtain and print the current day of year. System.out.println (br.readline ());} catch (ioException e) {system.out.println (E.TOSTRING ());} Finally {try {if (br! = null) br.close (); if (pw! = null) PW.Close (); if (s! = null) s.close ();} catch (ooException e) { }}}} Running this program will get the following results:
Tue Jan 29 18:11:51 CST 2002TESDAY2929
SSCLIT creates a socket object to contact the service program running on the host port 10000, and the IP address of the host is determined by the Host variable. SSCLIENT will get the input and output flow of the Socket, which is very easy to read and write the string with the input stream of the bufferedReader and the output stream of the PrintWriter. The SSCLIENT Server issues a variety of DATE / TIME commands and responds, each response All are printed, once the last response is printed, the FinalLe Schiter string of the Try / Catch / Finally structure will be executed, and the Finally substring will close the bufferedReader and PrintWriter before closing the socket.
Once the SSCLIENT source code is completed, you can enter Java SSClient to perform this program. If there is a suitable program running on a different host, use the hostname / IP address as the input method of the parameter, such as www.sina.com.cn It is the host running the server program, then the input method is java ssclient www.sina.com.cn.
skill
The Socket class contains many useful methods. For example, getLocaladdress () will return a reference to the inetaddress subclass object containing the client IP address; getLocalPort () will return the port number of the client; getinetaddress () will return a reference to the inetaddress subclass object containing the server IP address; getPort () Will return the port number of the service program.