Article Source: http://www.yesky.com/softchannel/72342371961929728/20020704/1618964.SHTML first steps fully understand Socket 1. What is Socket
The so-called socket is also also referred to as "socket", used to describe the IP address and port, a handle of a communication chain. Applications typically send requests or response network requests to the network via "socket".
Take J2SDK-1.3 as an example, Socket and Serversocket class libraries are located in the Java.net package. ServerSocket is used for server-side, and Socket is used when it is created. When the connection is successful, both ends of the application generate a Socket instance, operate this instance, and complete the session you need. For a network connection, the socket is equal and there is no difference, not because of the different levels of different levels in the server or on the client. Whether it is Socket or Serversocket, their work is done through the SocketImpl class and their subclasses.
Important Socket API:
Java.net.socket inherits in java.Lang.Object, there are eight constructors, and there are not many methods. Here is three ways to use the most frequent methods, and other methods can see JDK-1.3 documentation.
The ACCEPT method is used to generate "blocked" until a connection is accepted, and a client's Socket object instance is returned. "Block" is a term that makes the program to run temporary "stay" in this place until a session is generated, and then the program continues; usually "blocked" is generated by the loop.
The GetInputStream method obtains a network connection input while returning an IUTPUTSTREAM object instance.
. GetputStream method The other end of the connection will be entered while returning an OutputStream object instance.
Note: The GetInputStream and getOutputStream methods generate an IOException, which must be captured because they return the stream objects, usually by another stream object.
2. How to develop a server for a Server-Client model
Development principle:
Server, use the ServerSocket to listen to the specified port, the port can be specified (Since the port 1024 is usually belonging to the reserved port, it is not possible to use in some operating systems, so it is recommended to use a port greater than 1024, waiting for the customer connection request, customer connection After the session; after the session is completed, turn it off.
The client, uses socket to send a connection request for a port of a server on a server on the network, once the connection is successful, open the session; after the session is complete, turn off the Socket. The client does not need to specify the port, usually temporarily, dynamically assigned a 1024 port.
{Establish server}
import java.net *;. import java.io *;. public class Server {private ServerSocket ss; private Socket socket; private BufferedReader in; private PrintWriter out; public Server () {try {ss = new ServerSocket (10000); while (true) {socket = ss.accept (); in = new BufferedReader (new InputStreamReader (socket.getInputStream ())); out = new PrintWriter (socket.getOutputStream (), true); String line = in.readLine () Out.Println ("You Input is:" line; out.close (); in .close (); socket.close ();} ss.close ();} catch (ooException e) {}} public Static void main (String [] args) {new server ();}} This program has established a server that has been listening to 10000 ports, waiting for the user to connect. Return to a message to the client after establishing a connection, and then end the session. This program can only accept one customer connection at a time.
{Establish client}
Import java.io. *; import java.net. *; public class client {socket socket; buffredreader in; printwriter out; public client () {try {socket = new socket ("xxx.xxx.xxx.xxx", 1000 ); in = new BufferedReader (new InputStreamReader (socket.getInputStream ())); out = new PrintWriter (socket.getOutputStream (), true); BufferedReader line = new BufferedReader (new InputStreamReader (System.in)); out.println (line.readline (); line.close (); out.close (); in.close (); socket.close ();} catch (ooexception e) {}} public static void main (String [] args ) {new client ();}}
This client is connected to the address of the address xxx.xxx.xxx.xxx, the port is 1000, and enter a line of information from the keyboard, send it to the server, and then accepts the return information of the server, and finally ends the session