ServerSocket class
The socket class represents the client's socket. Whenever you want to connect to a remote server application, you have to build a socket. If you want to perform a server application, such as an HTTP service or an FTP service, you need to use a different approach. Because your server must always be idle on the boot, so it doesn't know when the client tries to connect it.
This time, you need to use the Java.net.ServerSocket class. It implements a server socket. A server socket waits for a connection from the client. Once it receives a connection request, it creates a Socket instance to handle the problem of communicating with the client.
To create a server socket, you can use one of the four ServerSocket class constructor methods to implement it. You need to develop the IP address and port that the server socket listens on. Typically, an IP address, if it is 127.0.0.1, means that the server socket will listen for the local machine. This monitored IP address is considered to be a binding address. Another important property of the server socket is its backlog property, which is the maximum queue length of a connection request that can be accepted before the server socket rejects the connection request.
One of the constructor functions of the ServerSocket class is as follows:
public ServerSocket (int port, int backLog, inetaddress bindingaddress);
For this constructor, the binding address must be an instance of the java.net.InetAddress. An easy way is to construct a Inetaddres object by calling its static method Getbyname. This method comes with a string parameter containing the host name:
Inetaddress.getbyname ("127.0.0.1");
The following line of code constructs a serversocket that listens on the local machine's 8080 port, backlog set to 1.
New ServerSocket (8080, 1, inetaddress.getbyname ("127.0.0.1"));
Once you have a ServerSocket instance, you can call the Accept method to tell it to wait for the incoming connection request. This method returns only if there is a connection request. It returns an instance of the socket class. This socket object is able to send and receive byte streams from the client application, which is the socket class mentioned in the first section. In fact, accept is the only one in this article that is used in the application.
Application Application
Our Web server application is part of the Ex01.pyrmont package and contains three classes:
Httpserver
Request
Response
The entry for this application (static Main method) is the Httpserver class. It creates a Httpserver instance to invoke its await method. As the method name implies, the await method waits for an HTTP request on a specified port, processes them, and then sends a response to the client. It remains in the waiting state until a shutdown command is received. (The reason for the command name await instead of wait is that wait is an important method for threading in the System.Object Class)
Applications only send static resources, such as HTML and picture files from a specific directory. Dynamic headers (such as dates or cookies) are not supported.