Java Network Programming
The server and client program only need to care about what data is sent to the other party, instead of how to transmit the data to the other party. The task of data transmission is completed by the computer network.
The prerequisite for smooth communication between the two processes is that their hosts are connected to the computer network.
The network protocol is the language for communication between hosts in the network.
The standard language on the network for interconnection between different networks-TCP/IP protocol.
The OSI reference model divides the network into seven layers: physical layer, data link layer, network layer, transmission layer, Session Layer, presentation layer, and application layer. The same layer between different hosts is called the peer layer.
Physical Layer: provides physical connections for the previous layer and specifies the mechanical and electrical properties between communication nodes. Data is transmitted as the original bit stream.
Data link layer: the data link layer is responsible for transmitting frame-based data without errors on the line between two adjacent nodes. A typical device is a vswitch.
Network Layer: select an appropriate route and exchange node to ensure that data is transmitted to the target host in a timely manner. A typical device is a router.
Transport Layer: the optimal use of network resources based on the characteristics of the Communication Subnet provides the ability to establish, maintain, and cancel transmission connections for the Session Layer of the two end systems, and transmit data in a reliable or unreliable manner. The message transmission unit is message.
Session Layer: manages the sessions between processes, that is, establishes, manages, and terminates sessions between processes. The data transmission unit is message.
Presentation Layer: converts upper-layer data to ensure that the application layer data of one host can be understood by the application layer of another host. The data transmission unit is message.
Application Layer: determines the actual purpose of inter-process communication to meet users' actual needs.
Each layer of the sender adds an information header to the data transmitted at the previous layer. The receiver removes the information header from each layer.
Peer-to-peer communication must follow certain rules, such as the content and method of communication. Such rules are called network protocols.
Typical protocols at various layers of the OSI model: shup8
The TCP/IP Reference Model consists of four layers: application layer, transmission layer, Network Interconnection layer, and host-network layer.
TCP/IP Reference Model Layer Protocol: Book P9
TCP protocol: connection-oriented and reliable protocol.
UDP protocol: unreliable and connectionless protocol.
IP protocol: IP address (IP Address URL + IP host address), network mask and IP address are binary and operated. The result is the IP Address URL.
The DNS protocol uses a DNS server to convert a domain name to an IP address.
TCP uses ports to differentiate processes. The two endpoints of the TCP connection are identified by ports.
Each process has a unique address, so TCP can ensure that data is successfully delivered to a specific process.
The port number ranges from 0 to 65535. The operating system is retained.
The server port number must be specified. The client port number is generally dynamically allocated by the system.
Both TCP and UDP use ports to identify processes. The value ranges independently. TCP and UDP ports with the same values are allowed.
In customer/Server mode, a server process serves multiple customer processes at the same time.
Common services and ports, book 17 pages.
The transport layer provides a Socket interface to the application layer. The Socket encapsulates the details of lower-layer data transmission. The application layer uses Socket to establish a connection with the remote host and transmit data.
Java has three Socket classes: Socket, ServerSocket, and DatagramSocket.
The Socket and ServerSocket are established on the TCP protocol, and the initramsocket is established on the UDP protocol.
Chapter 4 Socket usage
In customer/server communication mode, the client needs to create a Socket to connect to the server. The server receives the client connection request and creates a Socket to connect to the client.
Wait for the timeout time for establishing the connection. By default, the wait time will keep waiting. You can set the wait time in milliseconds through the Socket construction method without parameters. If the value is 0, the wait time will never expire.
Set the server address, InetAddress
Set the client address. The Socket object includes both the Server IP address and port, as well as the client address and port. for clients with multiple IP addresses, you can specify which IP address to use.
Exception when the client connects to the server: UnknownHostException (the host name or IP address cannot be recognized), ConnectionException (the server does not listen to the specified port or reject the connection), SocketTimeoutException (connection timeout ), bindException (unable to bind the Socket object to the Client IP address or port). For details, see ConnectTester class.
GetInputStream and getOutputStream: If the Socket is not connected or closed, or the shutdownInput (shutdownOutput) method has been called, the input and output streams are closed.
When the communication ends, close the Socket in time. We strongly recommend that you always execute the code in finally.
Socket status: isClose (true is returned if it has been connected to the remote host and has not been closed), isConnected (true is returned if it has been connected to the remote host ), isBound (returns true if it has been bound to a local port)
How to close the Socket and Stop Communication: 33 pp.
(1) accept stream: specifies special characters
(2) Agreed Data Length
(3) After sending data, the sender closes the Socket. Then, the receiving end calls the read method of the input stream and the returned value is-1.
(4) close the input or output stream, and finally call the close method of Socket to close the Socket connection.
IsInputShutdown and isOutputShutdown are used to detect the input/output stream status.
Termination method of both parties:
(1) ends naturally
(2) early termination of the server: After the server end suddenly ends, the client will still send full data and the end ends naturally, because the underlying Socket on the server is not immediately released, the operating system detects that data is still sent to this port, which will continue to take up a broken time.
(3) Suddenly terminate the client: A Connection reset exception is thrown.
(4) close or semi-close: the server throws a Connection reset exception.
Socket options:
TCP_NODELAY: whether to send data immediately. The Negale algorithm is used to send large batches of data and the receiving end can respond in a timely manner. If a small amount of data is sent and the receiving end does not respond in time, the performance is seriously reduced, this algorithm is used to buffer the sent data to a certain size and then send the data at a time. Wait for the receiving end to respond and then send the next batch of data.
The default value is false.
SO_RESUSEADDR: After the Socket close method is called to close the connection, the port will not be opened immediately. In this case, binding other processes to the port will fail. Setting SO_RESUSEADDR to true will allow other processes to reuse the port. It must be called before it is bound to a port. This problem usually exists on the server side. The new process does not receive the previous data.
SO_TIMEOUT: the timeout time for receiving data, in milliseconds. The default value is 0, indicating waiting forever. You need to set it before receiving data.
SO_LINGER: whether the Socket connection is closed immediately
SO_SNFBUF: size of the output data buffer. Large data volumes are recommended. Small Data is recommended for frequent transmission.
SO_RCVBUF: input data buffer size
SO_KEEPALIVE: when it is true, the underlying implementation will monitor the connection validity. The client uses
OOBINLINE: true indicates that one byte of emergency data can be sent.
Service type:
IP specifies the service type in 4:
1. Low Cost: Low sending cost
2. High Reliability: ensures reliable delivery of Bao data to the destination
3. high throughput: a large amount of data is sent at a time.
4. Minimum latency: fast data transmission and fastest data delivery to the destination
Set the service type: setTrafficClass (int trafficClass );
Bottom cost: 0x02
High reliability: 0x04
Maximum throughput: 0x08
Minimum latency: 0x10
The relative importance of connection time, latency, and bandwidth: setexcepcepreference
Chapter 2 ServerSocket usage
The server creates a ServerSocket to listen to a specific port, and the ServerSocket is responsible for receiving client connection requests.
ServerSocket has three parameters: port, backlog, and bindAddr.
Port bound: BindException will be thrown if binding fails. It may be because the port is occupied or cannot be used by this user (for example, 1 ~ 1023 may only be bound to the root user). The parameter 0 indicates that the operating system is allocated and is called an anonymous port.
Length of the Client Connection Request queue: the client connection request is managed by the operating system's first-in-first-out queue. ServerSocket uses the accept method to retrieve the queue request. The backlog is used to change the length of the Request queue, however, if the value is greater than the operating system limit or less than or equal to 0, the operating system value is still used.
Set the bound IP Address: bindAddr parameter. You can specify the IP address to bind when multiple IP addresses exist.
Constructor without parameters: the bind method must be called after the ServerSocket object is created. Some parameters can be set before bind, and some parameters must be called before bind.
The accept method is blocked and is returned only after the client is connected.
ServerSocket does not need to be closed. It is released at the end of the program and can be called.
ServerSocket isClosed and isBound are similar to Socket.
Get the server's bound IP address and port: getInetAddress and getLocalPort
Anonymous port used by FTP
ServerSocket options:
1. SO_TIMEOUT: The time for accept to wait for the client connection to time out. 0 indicates that the client never times out and waits for the client connection.
2. SO_REUSEADDR: similar to Socket
3. SO_RCVBUF: the size of the buffer for receiving data, which is related to the operating system.
The relative importance of connection time, latency, and bandwidth: similar to Socket
Multi-thread server (important)
The server in the preceding example cannot respond to multiple client connection requests at the same time.
Notes on using the thread pool (pp)
How to properly shut down the server (pages 7-80)
Chapter 2 non-blocking communication
ServerSocket and Socket may be blocked during running. If the server needs to communicate with multiple clients, multiple threads are required. Each thread may be blocked for a long time. With the non-blocking mechanism of JDK, the server only needs to create one thread to complete communication with multiple clients at the same time.
Thread blocking: Stop the CPU, pause the running, and wait until the blocking reason is eliminated to resume the running; or by other thread terminals, the thread will exit the blocking status and throw InterruptedException.
Cause of thread blocking:
(1) The Thread executes Thread. sleep.
(2) The thread needs to execute a synchronization code, and the thread enters the blocking status because the synchronization lock cannot be obtained.
(3) The thread executes the wait method of an object.
(4) When I/O operations or remote communication are performed, wait for the resource to enter the blocking status.
Possible client blocking:
(1) request to establish a connection with the server
(2) The thread reads data from the Socket input stream
(3) The thread writes a batch of data to the Socket output stream.
(4) The setSoLinger method is called to set the delay time for closing the Socket.
Possible server blocking:
(1) The thread executes the ServerSocket accept method and waits for the client to connect.
(2) The thread reads data from the Socket input stream
(3) The thread writes a batch of data to the Socket output stream.
Blocking IO and non-blocking IO: the possible blocking is called blocking IO, otherwise it is not blocking IO.
Restrictions on the server to respond to client requests using multiple threads:
(1) the virtual opportunity allocates an independent stack space for each thread. The more threads there are, the higher the system management pressure. Simply adding a thread does not continuously improve the performance. The number of threads should be reasonable, not the more the better.
(2) A lot of time is wasted on blocking IO.
Non-blocking idea:
The server program only needs one thread to receive connections from customers, receive data from customers, and send response data to customers. The Round-Robin method is used. When an operation is ready, this operation is executed. Otherwise, check whether other operations are ready for execution. The so-called non-blocking means that if the operation is not ready, return immediately without waiting until the operation is ready. At this time, the while loop is blocked and generally has a specific exit condition.
Non-blocking communication class of Java. nio package:
ServerSocketChannel and SocketChannel: supports blocking and non-blocking, instead of ServerSocket and Socket
Selector: monitors connection readiness events for the ServerSocketChannel, and monitors connection readiness, read readiness, and write readiness events for the SocketChannel.
SelectionKey: registers the event handle. When a SelectionKey object is located in the selected-keys set of the Selector object, it indicates that the event related to this SelectionKey object has occurred.
ByteBuffer: byte buffer. Both read and write put data into the buffer. Charset conversion is required.
Buffer: 88 pages
Channel: 90 pages of books
SelectableChannel: 92 pages
ServerSocketChannel: page 93
SocketChannel: page 93
Selector class: 96 pages
SelectionKey class: Page 97
Summary of blocking non-blocking mode: Book 127 page
Chapter 2 creating non-blocking HTTP servers
See source code
Chapter 4 client protocol processing framework
The general protocol processing framework encapsulates Socket, and Java abstracts the communication process of the client program, including URL, URLConnection, URLStreamHandler, and ContentHandler. The last three must be implemented by yourself. JDK has corresponding implementations for some common protocols to simplify Socket operations. For example, JDK implements a protocol processing framework for the HTTP protocol. Users can implement their own protocols.
Chapter 2 UDP-based datagram and Socket
TCP is a reliable data transmission protocol for the network transmission layer. If the data is lost or damaged during transmission, TCP will ensure that the data is sent again: if the order in which the data arrives at the receiver is disrupted, TCP will restore the correct data order in the receiver. The Application Layer does not have to worry about receiving out-of-order or incorrect data.
The reliability of TCP has a cost, which is the reduction of transmission speed. It takes a long time to establish and destroy a TCP connection. If the communication actually takes a short time and the data to be transmitted is very small, the cost for establishing and destroying a TCP connection is relatively high.
UDP: Unreliable transmission and fast transmission. The transmission unit is called UDP datagram. It cannot guarantee that the datagram reaches the destination or the sending order. The application layer protocol can be used to ensure reliability. It is often used for instant communication, audio and video, NFS, and TFTP.
UDP is a non-connection protocol, and no connection is required for data exchange. Mongoramsocket is responsible for receiving and sending UDP datagram, while mongorampacket indicates UDP datagram.
DatagramPacket class:
The constructor used to send data needs to set the destination address for reaching the datagram. The constructor used to receive data does not need to set the address. Generally, the server does not need an address. The client needs to specify the destination for sending the datagram.
Datagram size: the length parameter determines the length of the datagram. If the received datagram is greater than this value, the excess part is discarded. Selection Principle: The poorer the network, the smaller the data retribution, and the larger the datagram length, 8 KB is a good compromise.
Data format: Only data in bytes can be stored in a datagram.
Reuse mongorampacket: see the code for details.
DatagramSocket class:
Receive and send datagram: send and receive methods.
Management connection: Only data is exchanged with a specific DatagramSocket. Connect Method.
Disable DatagramSocket:
Option:
SO_TIMEOUT: The wait time-out time for receiving data reports. Unit: milliseconds. The default value is 0, and the timeout never expires.
SO_RCVBUF: size of the buffer for receiving data
SO_SNDBUF: size of the buffer for sending data
SO_REUSEADDR: whether to allow reuse of binding addresses. Whether multiple initramsockets can be bound to the same IP address and port will be copied to all initramsockets.
SO_BROADCAST: whether to allow sending and receiving data to the network broadcast address.