Introduction
1, the agreement is equivalent to the mutual communication between the program reached a convention, it provides the structure of packet packets, the mode of exchange, the meaning of inclusion and how the message contained in the analysis.
2, the TCP/IP protocol family has the IP protocol, the TCP protocol and the UDP protocol.
3. The address used by the TCP protocol and the UDP protocol is called the port number and is used to differentiate between different applications on the same host. The TCP and UDP protocols are also called end-to-end transport protocols because they transfer data from one application to another, and the IP protocol simply transfers data from one host to another.
4, in the TCP/IP protocol, there are two parts of the information used to determine a specified program: Internet address and port number: where the Internet address is used by the IP protocol, and the additional port address information by the Transport Protocol (TCP or UDP protocol) to resolve it.
5. The main socket type in the TCP/IP protocol family is now a stream socket (using the TCP protocol) and datagram sockets (using the UDP protocol), where the application can only send information at the maximum length of 65,507 bytes at a time through datagram sockets.
6. A TCP/IP socket is uniquely determined by an Internet address, an end-to-end protocol (TCP protocol or UDP protocol), and a port number.
7. Each port identifies an application on a host, in effect a port identifies a socket on a host. Multiple programs in a host can access the same socket at the same time, and in real-world applications, different programs that access the same socket usually belong to an application, such as multiple copies of a Web service program, but in theory they can belong to different applications.
Basic Sockets
1, write the TCP client program, when instantiating the socket class, be aware that the underlying TCP protocol can only handle the IP protocol, if the first parameter passed is the host name instead of your IP address, the socket class will be resolved to the corresponding address, if for some reason the connection failed, The constructor throws a IOException exception.
2. When the TCP protocol reads and writes data, the Read () method blocks the wait until there is new data readable when there is no readable data. In addition, the TCP protocol does not determine the bounds of the information sent in the read () and write () methods, and the data received or sent may be split into multiple parts by the TCP protocol.
3. The program that writes the TCP server will block at the accept () method to wait for the client's connection request, and once the connection is made, a socket instance for each client connection will be established for data communication.
4. In a UDP program, when you create an Datagrampacket instance, if you do not specify a remote host address and port, the instance is used to receive data (although it can be specified by calling setxxx (), and if a remote host address and Port are specified, the instance is used to send data.
5. The UDP program blocks at the receive () method until it receives a data message or waits for a timeout. Because the UDP protocol is an unreliable protocol, if the datagram is lost during transmission, then the program will always block at the receive () method, which is certainly not possible for the client, in order to avoid this problem, We use the Datagramsocket class's Setsotimeout () method on the client to develop the maximum blocking time for the receive () method and specify the number of times the datagram will be re-sent, and if each block times out and the number of postbacks reaches the set limit, the client is shut down.
6. The UDP server uses the same socket for all communications, which is different from the TCP server, and the TCP server creates a new socket for each accept () method that is successfully returned.
7, in the UDP program, each receive () Call of Datagramsocket can only receive the data sent by calling the Send () method at most, and the different receive () method calls will never return the amount of data sent by the same send () method.
8. In UDP socket programming, if the receive () method is called in a Datagrampscket instance with a buffer size of n, and the first message in the accepted queue is longer than N, the receive () method returns only the first n bytes of the message. The other bytes that are exceeded are automatically discarded, and there is no hint of any message loss. Therefore, the recipient should provide a datagrampacket instance of the cache space that is large enough to hold the message that is the maximum length allowed by the application protocol when the receive () method is called. The maximum amount of data that is run in a datagrampacket instance is 65,507 bytes, which is the maximum data that a UDP data packet can load, so it is always safe to use a data cache array of 65600 bytes.
9, in UDP socket programming, each Datagrampacket instance contains an internal message length value, and when the instance receives a new message, the length value may change (to reflect the number of bytes of the message actually received). If an application calls the receive () method multiple times using the same Datagrampacket instance, it must explicitly reset the internal length of the message to the actual length of the buffer before each call.
10. Another potential problem is rooted in the GetData () method of the Datagrampacket class, which always returns the original size of the buffer, ignoring the internal offset and length information for the actual data.
sending and receiving data
1. A consensus between programs that contains the form and meaning of information exchange is called an agreement, and the protocol used to implement a particular application is called an application protocol.
2. The only constraint of the TCP/IP protocol is that the information must be sent and received in the block, and the length of the block must be a multiple of 8, so we can assume that the information transmitted in the TCP/IP protocol is a sequence of bytes.
3, about the character, for each integer value is smaller than 255 a set of characters, because each character can be encoded as a separate byte, so no additional information is required, and for a large integer that may use more than one byte encoding, there are many ways to encode it, this is the encoding scheme. Coding schemes that encode character sets and characters are called character sets. In network programming, the sender and receiver must agree on how the text string is expressed, and the simplest way is to use the same standard character set.
4. Framing technology solves the problem of how the message receiver locates the end-to-back position of the message. Unlike the UDP protocol, there is no concept of message boundaries in the TCP protocol, so framing is a very important consideration when using TCP sockets.
5, there are two main techniques to enable the message receiver to accurately find the end position of the message: Based on the delimiter and the explicit length, the end of the message is indicated by a unique token, that is, the sender after the transmission of data after the explicit addition of a special character sequence, the special tag can not appear in the transmitted data, of course, The fill technique modifies the delimiter that appears in the message so that the recipient does not recognize it as a delimiter, which appends a fixed-size field value to the field or message to indicate how many bytes it contains.
Advanced
1. After the main thread ends, other threads can continue to execute, and the Java Virtual machine terminates only if all non-daemon threads have finished executing.
2, the server generally executes thousands of client requests every minute, so in order to better analyze the exception, most of the servers will write their activity records to the log. Java can use the Java.util.logging.Logger class to implement related functions, in Java, each logger is identified by a globally unique name, can be getlogger by the gas static method (string Name) to obtain a unique logger by name. By default, each logger has a consolehandler used to print messages to System.err. An important feature of logger is that it is thread-safe, that is, the method that can be called in a different thread that runs in parallel without the need to add additional synchronization measures to the caller, without which the different messages logged by different threads will be garbled and written to the log without a chapter.
3. When implementing TCP server side with thread pool, first create a ServerSocket instance, then create n threads, each thread loops repeatedly and receives the client connection from the (shared) ServerSocket instance. When multiple threads invoke the Accept () method of a ServerSocket instance at the same time, they will block the wait until a new connection is successfully established, then the system chooses a thread for the new connection that was just established, and the other threads continue to block the wait. If no thread is blocked on the accept () method when a client connection is created (that is, all threads are serving other connections), the system arranges the new connection in one queue until the next time the Accept () method is called.
4, the use of thread pool implementation server-side program, the size of the thread pool needs to be adjusted according to the load situation, so that the client connection time is shortest, ideally, there is a scheduling tool, you can expand the size of the thread pool when the system load increases (lower than the upper limit), reduce the size of the thread pool when the load. The executor interface is provided in Java to manage the dispatch thread, which represents an object that executes a runnable instance based on a policy that may contain details such as queueing and scheduling, or how to select the task to perform. When using executor, the task is queued inside the executor instead of in the network system.
5. The Executorservice interface inherits from the executor interface, and when an instance of the Executorservice interface calls the Execute () method, it needs to pass in an instance that implements the Runnable interface, if necessary, It will create a new thread to handle the task, but it will first try to use an existing thread, and if a thread is idle for more than 60 seconds, it will be removed from the thread pool. It is important to note that when a steady state is reached, the cache thread pool service will eventually maintain the appropriate number of threads so that each thread remains busy while the thread is rarely created or destroyed.
6, blocking socket programming, socket I/O can be blocked for a variety of reasons. The data entry method read () and receive () are blocked when no data is readable, and the write () method of the TCP socket may block when there is not enough space to cache the transmitted data. The ServerSocket accept () method and the socket constructor block the wait. When calling an already blocked method, it will use the application to stop and invalidate the thread that is running it.
7. The write () method call blocks the wait until the most one byte is successfully written to the local cache of the TCP implementation, and if the available cache space is smaller than the data to be written, some data must be successfully transferred to the other end of the connection before the write () method call returns. Java does not now provide any method for write () timeouts or other threads to break them, so a protocol that can send large amounts of data on the socket instance may be blocked indefinitely.
8. There are two types of one-to-many services: broadcast and multicast. For a broadcast, all hosts in the (local) network receive a copy of the data, and for multicast, the message is sent only to a multicast address, and the network simply distributes the data to those hosts that indicate that they want to receive data sent to the multicast address. In general, only UDP sockets allow broadcast and multicast. The multicast address range of the IPV4 is the multicast address in 224.0.0.0 to 239.255.255.255,ipv6, which is any address that begins with FF. In addition to the lack of system-exposed multicast addresses, the sender can send data to any address within the exception range. In Java, multicast applications communicate primarily through MulticastSocket instances.
9. In TCP socket communication, one end of the read () method returns 1 indicating that the other end of the communication closed the socket, or more specifically, the output stream associated with the socket was closed.
NIO
1, NiO mainly consists of two parts: Java.nio.channles package introduces selector and Channel abstraction, Java.nio package introduces buffer abstraction. The key point of selector and channel abstraction is to poll a group of clients at a time to find which client needs the service, and buffer to provide more efficient and predictable I/O than the stream abstraction. The channel is not using a stream, it is buffer buffers to send or read and write data.
2. The buffer abstraction represents a finite-capacity data container, which is essentially an array of pointers indicating where the data is stored and where it is read from. There are two main benefits of using buffer: first, the overhead associated with read-write buffer data is exposed to the programmer, which can be controlled directly by the programmer, and secondly, some special buffer mapping operations on Java objects can directly manipulate the resources of the underlying platform. These operations save the overhead of replicating data in different address spaces-a costly operation in modern computer architectures.
3. The powerful function of NIO comes from the non-blocking characteristics of channel. An important feature of the channel abstraction of NIO is the ability to configure its blocking behavior to implement a non-blocking channel. A method is always returned when a non-blocking channel is raised. For example, in a non-blocking serversocketchannel-up with the Accept () method, if there is a connection request waiting, the client Socketchannel is returned, otherwise, the return Null;read () method does not block the wait when no data is readable. Instead, it returns 0. Threads can also do other things while waiting for a connection, reading data, and so on, which enables the asynchronous operation of the thread
4. The select () method of the selector class blocks the wait until a channel is ready for the IO operation, or waits for a timeout, or another thread wakes it (the wakeup () method that invokes the selector). The Select () method returns the number of channels that have become ready since the last time it was called. If the Select () method is called, because a channel becomes ready and returns 1, if the Select () method is called again, it returns 1 again if the other channel is ready. If no action is made on the first ready channel, there are now two ready channels, but only one channel is ready between each select () method call.
5. When we iterate the Selectionkey collection with iterator, we notice the call to the Remove () method at the end of each iteration. Selector does not remove the Selectionkey instance from the selected key set itself. You must remove it yourself when the channel is finished, in case the next time the channel becomes ready, selector can put it in the selected key set again. If you do not remove each processed key, it will remain in the collection the next time the Select () method is called, and there may be a useless operation to invoke it. The selector selector implements the ability to listen on multiple channels in a single thread.
6, buffer is fixed length, can not expand capacity, Bytebuffer is the most commonly used buffer. The size relationship of each index value in the buffer: 0=<mark=<position=<limit=<capacity.
7, the Allocatedirect () method attempts to allocate direct buffers, using a direct buffer, Java will allocate backup storage space for the buffer from the storage space where the platform is capable of direct I/O operations, but there is no guarantee that it will succeed. Therefore, after attempting to allocate a direct buffer, you must call the Isdirect () method to check that the allocation and destruction of direct buffers is usually more system resources than allocating and destroying non-direct buffers.
8. The clear () method of buffer does not alter the data in the buffer, it simply sets the position to 0 and sets the limit to equal to capacity, which prepares the buffer to receive new data from the put operation of the buffer or the read operation of the channel. The flip () method is used to prepare the buffer for the data egress state, which is set to the current value of position, and the value of position is set to zero. The Rewind () method sets position to 0, invalidates the mark value, and the limit value does not change, so that the data in the buffer can be repeatedly transferred. The Compact () method copies the elements between position and limit to the beginning of the buffer to make room for subsequent read ()/put () operations, but data replication is a very resource-intensive operation, so use the compact () method conservatively. If the call to the slice () method creates a new buffer that shares the original buffer subsequence, the array () method is called on the preceding buffer or the entire buffer array is returned.
9, for non-blocking socketchannel, once the Connect () method has been invoked to initiate the connection, the underlying socket may not be connected or not connected, but is connecting. Because of the underlying protocol's working mechanism, the socket may remain in this state all the time, it is necessary to loop the Finishconnect () method to check whether the connection is complete, while waiting for the connection, the thread can do other things, which enables the asynchronous operation of the thread.
10, each selector has a set of channels associated with it, one channel can also register multiple selector instances, so there can be more than one associated Selectionkey instance. Any changes to the set of interest operations associated with key will only take effect the next time the Select () method is called. For Serversocketchannel, accept is the only valid operation, and for Socketchannel, valid operations include read, write, and connect, and for Datagramchannle, only read and write operations are valid. A channel may be registered only once with one selector, so subsequent calls to the register () method simply update the set of interest operations associated with the key.
11. The keyset returned by the Selectedkeys () method is modifiable, and in fact it must be emptied manually between two calls to the Select () method, in other words, the Select () method simply adds a key to the existing selected keyset and does not create a new build.
"Java TCP/IP Socket" Socket programming Knowledge points Summary