To create a server-side using ServerSocket:
The ability to accept other communication entity connection requests in Java is the Serversocket,serversocket object used to listen for the socket connection from the client.
If there is no connection, it will remain in the waiting state.
ServerSocket Construction Method:
Construction Method Summary |
serversocket () Create a non-binding server socket. |
|
serversocket (Int port)   create a server socket bound to a specific port. |
|
serversocket (int port, int backlog)   use of designated backlog Creates a server socket and binds it to the specified local port number. |
|
serversocket (Int port, int backlog, inetaddress bindaddr)   creates a server using the specified port, listening backlog , and the local IP address to bind to. |
|
Parameter port specifies the port to be bound by the server (the port to be monitored by the server), the parameter backlog specifies the length of the Client connection request queue, and the parameter bindaddr specifies the IP address to bind the server to.
bound Port: ServerSocket (int port)
ServerSocket SS = New ServerSocket (30005);
Creates a server that is bound to port 30005 and throws an exception if the runtime cannot bind to the port, possibly because the port is already occupied by another server process.
To set the length of the Client connection request queue: Backlog
Many operating systems limit the maximum length of a queue, typically 50. When a connection request in the queue reaches the maximum capacity of the queue, the host on which the server process resides rejects the new connection request
The queue can continue to join a new connection request only if the server process takes the connection request out of the queue through the ServerSocket accept method, leaving the queue empty
The backlog parameter in the ServerSocket construction method is used to set the length of the connection request queue, but in the following cases, the maximum length of the queue that the operating system qualifies is still used:
The value of the backlog parameter is greater than the maximum value of the operating system-qualified queue
The value of the backlog parameter is less than or equal to 0
The backlog is not used in the ServerSocket construction method
To receive and close a connection to a client:
Receive: ServerSocket's Accept ()
Close: Close ()
Steps to create a ServerSocket
1. Instantiate a serversocket of a specified port;
2. Call ServerSocket's accept () method;
3. Get the input and output stream through the socket;
4. Read and write to the socket;
5. Close the stream.
public static void Main (string[] args) throws IOException {//Create a ServerSocket to listen for connection requests from the client socket ServerSocket SS = new Ser Versocket (30005);//Use loops to continuously accept requests from the client while (true) {///Whenever a request is received, the server side will also produce a corresponding socketsocket s = ss.accept (); outputstream OS = S.getoutputstream (); Os.write ("Hello". GetBytes ("Utf-8"));//close Os.close (); S.close ();}}
Android------using serversocket to create server-side