Java learning road-Socket programming
Overview
The so-called socket (Socket) is an abstraction of endpoints for bidirectional communication between application processes on different hosts in the network. A socket is one end of process communication on the network, and provides a mechanism for application layer processes to exchange data using network protocols. In terms of its status, the socket is connected to the application process and the network protocol stack is the interface for the application to communicate through the network protocol, and the interface for the application to interact with the network protocol root.
Socket is an abstract concept. An application establishes a remote connection through a Socket, and the Socket internally transmits data to the network through the TCP/IP protocol.
The functions of Socket, TCP, and part of the IP are provided by the operating system, and different programming languages only provide simple encapsulation of calls to the operating system.
The knowledge about the computer network part will not be repeated here. This blog only briefly introduces the use of Socket API in the Java language. For specific usage rules, please refer to other materials.
One, ServerSocket
category
The server application by using java.net.ServerSocket
the class to get a port, and listens for client requests.
Construction method
Construction method | Function description |
---|---|
ServerSocket(int port) |
Create a server socket bound to a specific port; |
ServerSocket(int port, int backlog) |
Use the specified backlog to create a server socket and bind it to the specified local port number; |
ServerSocket(int port, int backlog, InetAddress address) |
Create a server with the specified port, listening backlog and local IP address to be bound to; |
ServerSocket() |
Create an unbound server socket. |
Common method
method | Function description |
---|---|
int getLocalPort() |
Returns the port this socket is listening on; |
Socket accept() |
Listen and accept the connection to this socket; |
setSoTimeout(int timeout) |
Enable/disable by specifying the timeout value SO_TIMEOUT , in milliseconds; |
bind(SocketAddress host, int backlog) |
You will be ServerSocket bound to a specific address (IP address and port number). |
Two, Socket
class
ava.net.Socket
The class represents the socket that both the client and server use to communicate with each other. To get a client Socket
object is instantiated by, the server obtains a Socket
subject through accept()
the return value.
Construction method
Construction method | Function description |
---|---|
Socket(String host, int port) |
Create a stream socket and connect it to the specified port number on the specified host; |
Socket(InetAddress host, int port) |
Create a stream socket and connect it to the specified port number of the specified IP address; |
Socket(String host, int port, InetAddress localAddress, int localPort) |
Create a socket and connect it to the specified remote port on the specified remote host; |
Socket(InetAddress host, int port, InetAddress localAddress, int localPort) |
Create a socket and connect it to the specified remote port on the specified remote address; |
Socket() |
By default type SocketImpl is not connected to create a socket |
Common method
method | Function description |
---|---|
connect(SocketAddress host, int timeout) |
Connect this socket to the server and specify a timeout value; |
InetAddress getInetAddress() |
Return the address of the socket connection; |
getPort() |
Returns the remote port to which this socket is connected; |
getLocalPort() |
Returns the local port to which this socket is bound; |
SocketAddress getRemoteSocketAddress() |
Return the address of the endpoint connected by this socket, or null if it is not connected; |
InputStream getInputStream() |
Return the input stream of this socket; |
OutputStream getOutputStream() |
Return the output stream of this socket; |
close() |
Close this socket. |
Three, InetAddress
class
This class represents an Internet Protocol (IP) address.
Common method
method | Function description |
---|---|
InetAddress getByAddress(byte[] addr) |
In the case of a given original IP address, it returns InetAddress the object; |
InetAddress getByAddress(String host, byte[] addr) |
Create based on the provided hostname and IP address InetAddress ; |
InetAddress getByName(String host) |
Determine the IP address of the host given the host name; |
getHostAddress() |
Return the IP address string (in the form of text); |
getHostName() |
Get the hostname of this IP address; |
InetAddress getLocalHost() |
Return to the local host; |
toString() |
Convert this IP address to String. |