This article mainly introduces the class methods and examples in java network programming. Friends who need it can refer to it
Network programming refers to writing programs that run on multiple devices (computers). These devices All connected through the Internet.
The J2SE API in the java.net package contains classes and interfaces that provide low-level communication details. You can use these classes and interfaces directly to focus on solving problems rather than on communication details.
The java.net package provides support for two common network protocols:
TCP: TCP is the abbreviation of Transmission Control Protocol. It guarantees reliable communication between two applications. Commonly used for Internet protocols, known as TCP/IP.
UDP:UDP is the abbreviation of User Datagram Protocol, a connectionless protocol. Provides packets of data to be sent between applications.
This tutorial mainly explains the following two topics.
Socket Programming: This is the most widely used networking concept and it has been explained in great detail
URL Handling: This part will be discussed in another space. 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 to and reading from the Socket object.
The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a mechanism for server programs to listen to clients and establish connections with them.
The following steps occur when establishing a TCP connection using sockets between two computers:
The server instantiates a ServerSocket object that represents the port on the server. communication.
The server calls the accept() method of the ServerSocket class, which will wait until the client connects to the given port on the server.
While 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, a Socket object is created on the client to communicate with the server.
On the server side, the accept() method returns a new socketreference on the server, which is connected to the client's socket.
After the connection is established, communication is carried out using I/O streams. Each socket has an output stream and an input stream. The client's output stream is connected to the server's input stream, and the client's input stream is connected to the server's output stream.
TCP is a two-way communication protocol, so data can be sent through two data streams at the same time. The following is a complete set of useful methods provided by some classes to implement sockets.
Methods of ServerSocket class
The server application obtains a port by using the java.net.ServerSocket class and listens for client requests .
The ServerSocket class has four construction methods:
## Serial number | Method description |
||||||||||||||||||||||||||||||||||
public ServerSocket(int port) throws IOExceptionCreate a server socket bound to a specific port | |||||||||||||||||||||||||||||||||||
public ServerSocket(int port, int backlog) throws IOExceptionUse the specified backlog to create a server socket and bind it to the specified local port number | |||||||||||||||||||||||||||||||||||
public ServerSocket(int port, int backlog, InetAddress address) throws IOExceptionCreate a server with the specified port, listening backlog, and local IP address to bind to | |||||||||||||||||||||||||||||||||||
public ServerSocket() throws IOExceptionCreate an unbound server socket |
Serial number | Method description |
## public int getLocalPort() |
Return this socket The port the word is listening on |
##public Socket accept() throws IOException | Listen and accept Connection to this socket | 3
public void setSoTimeout(int timeout) | Enabled by specifying a timeout value /Disable SO_TIMEOUT in milliseconds | 4
public void bind(SocketAddress host, int backlog) | will ServerSocket binds to a specific address (IP address and port number) |
The java.net.Socket class represents the socket used by both clients and servers to communicate with each other. The client obtains a Socket object through instantiation, and the server obtains a Socket object through the return value of the accept() method.
The Socket class has five construction methods.
Method description | ##1 | ##public Socket(String host, int port) throws UnknownHostException, IOException.||||||||||||||||||||||||||||||||||
2 | public Socket( InetAddress host, int port) throws IOException|||||||||||||||||||||||||||||||||||
3 | public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException.|||||||||||||||||||||||||||||||||||
4 | public Socket(InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException|||||||||||||||||||||||||||||||||||
5 | public Socket()|||||||||||||||||||||||||||||||||||
When the Socket constructor returns, it does not simply instantiate 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 the server have a Socket object, so both the client and the server can call these methods.
Methods of the InetAddress class
|
The above is the detailed content of Detailed explanation of network programming socket. For more information, please follow other related articles on the PHP Chinese website!