First, the network API
InetAddress is used to identify hardware resources on the network, primarily IP addresses
URL Uniform Resource Locator, which can read or write directly to data on the network via a URL sockets uses the TCP protocol to implement the network communication socket-related class datagram uses the UDP protocol, saves the data in the user datagram, communicates through the network.
Second, address
A, inetaddress IP address
(1) static method
- Getlocalhost () inetaddress
- Getbyname (String host) inetaddress
- Getallbyname (String host) inetaddress[]
- Getbyaddress (byte[] addr) inetaddress
- Getbyaddress (String host, byte[] addr) inetaddress
(2) Member method
- GetAddress () byte[]
- Getcanonicalhostname () String
- Gethostaddress () String
- GetHostName () String
- Isanylocaladdress () Boolean
- Ismulticastaddress () Boolean
- isreachable (int timeout) Boolean
B, inetsocketaddress socket address
(1) Construction method
- Inetsocketaddress (inetaddress addr, int port) creates a socket address based on the IP address and port number.
- inetsocketaddress (int port) creates a socket address where the IP address is a wildcard address and the port number is the specified value.
- Inetsocketaddress (String hostname, int port) creates a socket address based on host name and port number.
(2) Member method
GetAddress () inetaddress gets inetaddress.
GetHostName () String gets hostname.
Getport () int gets the port number.
(4) static method
Createunresolved (String host, int port) inetsocketaddress creates an unresolved socket address based on the host name and port number.
Third, Socket
(1) Construction method
- Socket () creates an SOCKETIMPL socket from the system default type
- Socket (inetaddress address, int port) creates a stream socket and connects it to the specified IP address Port number.
- The socket (inetaddress address, int port, inetaddress localaddr, int localport) creates a socket and connects it to the specified remote port on the specified remote address.
- Socket (proxy) creates an disconnected socket and specify the proxy type, if any, that the agent should be used regardless of other settings.
- The socket (String host, int port) creates a stream socket and connects it Receives the specified port number on the specified host.
- A socket (String host, int port, inetaddress localaddr, int localport) creates a socket and connects it to the specified remote port on the specified remote host.
(2) Member method
1) associated Address
- Bind (socketaddress bindpoint) void binds the socket to a local address.
- Connect (socketaddress endpoint) void connects this socket to the server.
- Connect (socketaddress endpoint, int timeout) void connects this socket to the server and specifies a timeout value.
- Isbound () Boolean returns the binding state of the socket.
- IsConnected () Boolean Returns the connection state of the socket.
2) Get the IP address
- Getinetaddress () inetaddress returns the address of the socket connection.
- Getlocaladdress () InetAddress Gets the local address of the socket binding.
- Getport () int returns the remote port that this socket is connected to.
- Getlocalport () int returns the local port to which this socket is bound.
- Getlocalsocketaddress () socketaddress returns the address of the endpoint bound by this socket and returns NULL if it is not already bound.
- Getremotesocketaddress () socketaddress returns the address of the endpoint of this socket connection, or null if not connected.
3) Get the data
- getInputStream () InputStream returns the input stream for this socket.
- Getoutputstream () OutputStream returns the output stream for this socket.
- Getchannel () Socketchannel returns the unique Socketchannel object, if any, associated with this datagram socket.
4) Set Buffer
- setreceivebuffersize (int size) void sets the SO_RCVBUF option for this Socket to the specified value.
- setsendbuffersize (int size) void sets the SO_SNDBUF option for this Socket to the specified value.
5) Reusing ports
- Setreuseaddress (Boolean on) void Enables/disables the SO_REUSEADDR socket option.
- Getreuseaddress () Boolean tests whether SO_REUSEADDR is enabled.
6) Close
- Close () void closes this socket.
- IsClosed () Boolean returns the closed state of the socket.
Iv. ServerSocket
(1) Construction method
Java Socket Programming Summary