Examples based on socket class and ServerSocket class

Socket class

Socket is the endpoint of network connection. Socket enables applications to read data from and write data to the network. Two applications on different computers can send or receive byte streams through connection, so as to communicate with each other.

In order to send messages from one application to another, you need to know the IP address and port number of the socket in the other application. In Java, the socket is composed of Java net. Socket representation.

To create a socket, you can use one of the many constructors in the socket class. One of the constructors takes two parameters: the host number and the port number.

Where the parameter host is the name or IP address of the remote host, and the parameter port is the port number to connect to the remote application. For example, you want to connect to Yahoo. Com through port 80 COM, you can use the following statement to create a socket object

Once an instance of the socket class is successfully created, you can use the instance to send or receive a byte stream. To send a byte stream, you need to call the getoutputstream () method of the socket class to obtain a Java io. OutputStream object,

To send text to a remote application, you usually need to create a Java. Net file using the returned OutputStream object io. Printwriter object. To receive a byte stream from the other end of the connection, you need to call getinputstream() of the socket class

Method, which returns a Java io. InputStream object.

The following code creates a socket to communicate with the local HTTP server, send HTTP requests and receive the corresponding information from the server. The following code creates a StringBuffer object to save the information and output it.

ServerSocket class

The socket class represents a client socket, that is, a socket created when you want to connect to a remote server application. But if you want to implement a server application (such as an HTTP server or FTP server),

You need another method, because the server must be on standby. It doesn't know when the client application will initiate the connection. Because of this, you need to use Java net. ServerSocket class. This is the implementation of server socket.

The ServerSocket class is different from the socket class. The server socket needs to wait for the connection request from the client. When the server socket receives the connection request, it will create a socket instance to handle the communication with the client.

To create a server socket, you can use any of the four constructors provided by the ServerSocket class. You need to specify the IP address and the port number on which the server socket listens. Typically, the IP address can be 127.0 0.1,

That is, the server socket will listen to the connection request received by the local machine. The IP address that the server socket listens to is called the binding address. Another important attribute of server sockets is backlog, which indicates that the server refuses to receive incoming data

The maximum queue length of incoming connection requests before the request.

The signature of one of the constructors of the ServerSocket class is as follows:

It is worth noting that in this constructor, the parameter binding address must be Java net. An instance of the InetAddress class. A simple way to create an InetAddress object is to call its static method getbyname(),

Pass in a string containing the host name. The code is as follows:

The following line of code creates a ServerSocket object. The ServerSocket object listens to port 8080 of the local host, and its backlog value is 1;

After creating the ServerSocket instance, you can make it wait for the incoming connection request. The connection request will be sent in through the binding address on the port on which the server socket listens. You can call the accept method of the ServerSocket class

Done. Only after receiving the connection request, the method will return. The return value is a socket instance. Then, you can use the socket object to send / receive byte streams with the client application as described above.

The above example explanation based on socket class and ServerSocket class is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>