Java network programming, socket programming, etc

Network programming refers to writing programs that run on multiple devices (computers), which are connected through the network.

java. Net package contains classes and interfaces, which provide low-level communication details. You can use these classes and interfaces directly to focus on solving problems without paying attention to communication details.

java. Net package provides support for two common network protocols:

TCP: TCP is the abbreviation of transmission control protocol. It ensures reliable communication between two applications. It is usually used for internet protocol and is called TCP / IP.

UDP: UDP is the abbreviation of user datagram protocol, a connectionless protocol. A packet that provides data to be sent between applications. This tutorial focuses on the following two topics.

Socket programming: This is the most widely used network concept, which has been explained in great detail

URL processing: this part will be discussed in another section. Click here to learn more about URL processing in Java language.

Socket programming

Sockets provide a communication mechanism between two computers using TCP. The client program creates a socket and attempts to connect to the server's socket.

When the connection is established, the server will create a socket object. The client and server can now communicate by writing and reading socket objects.

java. net. The socket class represents a socket, and Java net. The ServerSocket class provides a mechanism for server programs to listen to clients and establish connections with them.

The following steps appear when using sockets to establish a TCP connection between two computers:

The server instantiates a ServerSocket object to represent communication through the port on the server.

The server calls the accept () method of the ServerSocket class, which will wait until the client connects to the given port on the server.

When the server is waiting, a client instantiates a socket object and specifies the server name and port number to request a connection.

The constructor of the socket class attempts to connect the client to the specified server and port number. If communication is established, create a socket object on the client to communicate with the server.

On the server side, the accept () method returns a new socket reference on the server, which is connected to the socket of the client.

After the connection is established, it communicates through the use of I / O flow. Each socket has an output stream and an input stream. The output stream of the client is connected to the input stream of the server, and the input stream of the client is connected to the output stream of the server.

TCP is a bidirectional communication protocol, so data can be sent at the same time through two data streams The following is a complete set of useful methods provided by some classes to implement sockets.

Method of ServerSocket class

The server application uses Java net. ServerSocket class to get a port and listen for client requests.

The ServerSocket class has four construction methods:

Creates an unbound server socket. If the ServerSocket constructor does not throw an exception, it means that your application has successfully bound to the specified port and listens for client requests.

Here are some common methods of ServerSocket class:

Method of socket class

java. net. The socket class represents the socket used by both client and server to communicate with each other. The client obtains a socket object through instantiation, while the server obtains a socket object through the return value of the accept () method.

The socket class has five constructors

When the socket constructor returns, instead of simply instantiating a socket object, it will actually try to connect to the specified server and port.

Some interesting methods are listed below. Note that both the client and server have a socket object, so both the client and server can call these methods.

Method of InetAddress class

This class represents an Internet Protocol (IP) address. The following lists some useful methods for socket programming:

static InetAddress getByAddress(byte[] addr)

Returns the InetAddress object given the original IP address

static InetAddress getByAddress(String host,byte[] addr)

Create InetAddress based on the host name and IP address provided

static InetAddress getByName(String host)

Determine the IP address of the host given the host name

String getHostAddress()

Returns the IP address string (expressed as text)

String getHostName()

Gets the host name of this IP address

static InetAddress getLocalHost()

Return to local host

String toString()

Convert this IP address to string

Socket client instance

The following greeningclient is a client program that connects to the server through socket, sends a request, and then waits for a response.

Socket server instance

The following greetingserver program is a server-side application that uses socket to listen to a specified port.

Compile the above java code and execute the following command to start the service with port number 6066:

Open the client as follows:

I hope this article is helpful to you

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
分享
二维码
< <上一篇
下一篇>>