Web Service working mechanism based on Jave (3)

Source: Internet
Author: User
Tags constructor http request connect net socket client
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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.