http://www.cnblogs.com/hq-antoine/archive/2012/02/11/2346474.html 1.1 ServerSocket class
Create a ServerSocket class and establish a listening service at the specified port of the computer on which the statement is run, such as:
ServerSocket mylistener=new ServerSocket (600);
This specifies that the port that provides the listening service is 600, a computer can provide multiple services at the same time, these different services are differentiated by port number, and different services are provided on different port numbers. To listen for possible client requests at any time, execute the following statement:
Socket Linksocket=mylistener. Accept ();
The statement invokes the Accept () method of the ServerSocket object, which executes the server-side program in a wait state, and the program blocks until a request is captured from the client side. and returns a Socket object that is used to communicate with the client link-socket. Thereafter, the server program can read and write data to the remote client as long as the data is read and written to the socket object. To end the listener, close the ServerSocket object:
MyListener. Close ();
1. 2 Socket Class
When the client program needs to obtain information and other services from the server side, a socket object should be created:
Socket mysocket=new socket ("Servercomputername", 600);
The constructor for the socket class has two parameters, the first parameter is the host address of the server computer to which you want to connect, and the second parameter is the port number on the server machine that provides the service.
Once the socket object is successfully established, a connection can be established between the client and the server, and the data is passed between the two endpoints through this connection. Using the Socket class method Getoutputstream () and getInputStream () respectively obtain the input/output stream to read and write data to the socket, and finally return the data read from the server side back to the server side.
When the communication between the server and client ends, you can call the close () method of the socket class to close the socket and remove the connection.
ServerSocket generally only used to set the port number and monitoring, the real communication is the server-side socket and client socket, after the ServerSocket to accept, will be the active transfer.
1. Server-side programming
On the server side, use the ServerSocket class's constructor serversocket (int port) to create an object of the ServerSocket class, the port parameter delivery port, which is the port on which the server listens for connection requests. If an error occurs at this point, the IOException exception object is thrown, otherwise the ServerSocket object is created and the readiness to receive the connection request begins.
The service program starts with the Accept () method that calls ServerSocket until the connection is established. After the connection is established, accept () returns a recently created socket object that binds the client's IP address or port number.
2. Client Programming
When the client program needs to communicate with the server program, it needs to create a socket object on the client. The socket class has a constructor socket (inetaddress Addr,int port) and a socket (String host,intport), and two constructors have created a socket-based streaming socket for the connection server-side flow socket. For the first Inetad-dress subclass object, the IP address of the server host is obtained through the addr parameter, and for the second function the host parameter package is assigned to the InetAddress object, if no IP address matches the host parameter, Then the unknownhostexception exception object is thrown. Two functions are given the port number of the server via the parameter port. Assuming the connection has been established, the network API will bundle the client's IP address and any port number in a socket-based streaming socket, otherwise two functions will throw a IOException object.
If a socket object is created, it can be obtained from the service program via the Get-inputstream () method, or the output stream can be sent by calling the Getoutputstream () method. After the read-write activity is completed, the client program calls the close () method to close the stream and stream the socket.
Elementary procedures to explain:
Server-side code:
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;
public class Myserver {
Establish the ServerSocket and set its port number
Private ServerSocket SS;
public static final int port=8962;
Public Myserver () {
try{
Ss=new ServerSocket (port);
}catch (IOException e) {
E.printstacktrace ();
}
}
public void SetConnection () throws ioexception{
Create a server-side socket
Socket s;
OutputStream OS;
try{//serversocke.accept () t returns a socket object
S=ss.accept ();
Os=s.getoutputstream ();
Os.write ("Hello". GetBytes ());
Os.close ();
S.close ();
}catch (IOException e) {
E.printstacktrace ();
}
}
public static void Main (string[] args) throws IOException {
Myserver ms=new Myserver ();
Ms.setconnection ();
}
}
Client code:
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.net.Socket;
public class MyClient {
public static final String ip= "172.16.221.134";
public static final int port=8962;
Private Socket S;
Public MyClient () throws ioexception{
try{
S=new Socket (Ip,port);
}catch (IOException e) {
E.printstacktrace ();
}
}
public void SetConnection () throws ioexception{
InputStream is;
try{
Is=s.getinputstream ();
BufferedReader br=new BufferedReader (New InputStreamReader (IS));
System.out.println (Br.readline ());
}catch (IOException e) {
E.printstacktrace ();
}
}
public static void Main (String args[]) throws ioexception{
MyClient mc=new myclient ();
Mc.setconnection ();
}
}
The difference between socket and ServerSocket in Java socket programming