ToDifferentiate network communication and connection between different application processesThere are three main parameters:Destination IP address of the communication,Transport Layer Protocol used (TCP or UDP)AndPort number used.
The original intention of socket is "socket ". By combining these three parameters and binding them with a "socket" socket, the application layer can distinguish communications from processes or network connections of different applications through the socket interface with the transport layer, implements concurrent data transmission services.
What is the socket port number generated by accept?
To write a network program, you must use socket. This is what programmers know. In addition, during the interview, we will also ask if the other party will be able to program the socket? In general, many people will say that socket programming is basically listen, accept, send, write, and other basic operations. Yes, just like common file operations, as long as they have been written.
For network programming, we must also call it TCP/IP. It seems that other network protocols no longer exist. For TCP/IP, we also know TCP and UDP. The former ensures data correctness and reliability, while the latter allows data loss. Finally, we also know that we must know the IP address and port number of the other party before establishing a connection. In addition, ordinary programmers will not know too much, and many times this knowledge is enough.The maximum number of concurrent accesses is processed by multiple threads when writing a service program..
We also know the following facts:
1.A specified port number cannot be shared by multiple applications.. For example, if IIS occupies port 80, Apache cannot use port 80;
2.Many firewalls only allow data packets on specific ports to pass through.
3. After the service program accesses a connection request on a port of listen, a new socket is generated to process the request.
As a result, I am confused for a long time. If a socket is created and bound to port 80, does it mean that the socket occupies port 80?
If this is the case, after the accept request, what port is used for the generated new socket (I always thought the system would assign it an idle port number by default )?
If it is an idle port, it must not be port 80, so the destination port of the TCP packet in the future will not be port 80-the firewall will definitely stop it from passing through!
In fact, we can see that the firewall does not block such a connection, and this is the most common connection request and processing method. What I don't understand is, why does the firewall not block such a connection? How does it determine that the connection is generated because of port connect80? Is there any special mark in the TCP packet? Or what does the firewall remember?
Later, I carefully studied the protocol stack principles of TCP/IP and gained a deeper understanding of many concepts. For example, TCP and UDP belong to the same transport layer and are jointly deployed on the IP layer (Network Layer. The IP layer is mainly responsible for transmitting data packets between nodes (end to end). The node here is a network device, such as a computer.Because the IP layer is only responsible for sending data to the node, but cannot distinguish the preceding applications, TCP and UDP are added with port information, the port identifies an application on a node.. In addition to adding port information, UDP basically does not process data at the IP layer. The TCP protocol also adds more complex transmission control, such as a sliding data transmission window (slice window) and receiving confirmation and re-transmission mechanisms to achieve reliable data transmission. No matter what a stable TCP data stream is seen at the application layer, all IP data packets are transmitted below, and data is reorganized by the TCP protocol.
Therefore, I have reason to suspect that the firewall does not have enough information to determine more information about TCP packets, except for IP addresses and port numbers. Also, we can see that,The so-called port is used to distinguish different applications, so that the ports can be correctly forwarded when different IP packets arrive..
TCP/IP is just a protocol stack. Like the operating mechanism of the operating system, it must be implemented in detail and provide external operation interfaces. Just as the operating system provides standard programming interfaces, such as Win32 programming interfaces, TCP/IP must also provide programming interfaces externally. This is the socket programming interface!
In the socket programming interface, the designer puts forward a very important concept, that is, socket. This socket is very similar to the file handle. In fact, it is stored in the same BSD system as the file handle.Process Handle.This socket is actually a serial number, indicating its position in the handle table. We have seen many of them, such as file handles and window handles.These handles actually represent some specific objects in the system. They are used to pass in as parameters in various functions to operate on specific objects.-- This is actually a problem with the C language. In the C ++ language, this handle is actually a this pointer, but actually an object pointer.
Now we know that socket and TCP/IP are not necessarily related. The socket programming interface is designed to adapt to other network protocols. Therefore, the emergence of socket is only more convenient to use the TCP/IP protocol stack, which abstracts TCP/IP and forms several basic function interfaces. For example, create, listen, accept, connect, read, and write.
Now we understand that if a program creates a socket and listens to port 80, it declares its possession of port 80 to the TCP/IP protocol stack. In the future, all TCP data packets destined for port 80 will be forwarded to this program (the program here, because the socket programming interface is used, is first processed by the socekt Layer ). The so-called accept function abstracts the TCP connection establishment process. The new socket returned by the accept function actually refers to the connection created this time. A connection includes two parts: the source IP address and the source port, and the source IP address and the destination port.In this case, these socket ports can all be 80!At the same time, the firewall's rules for handling IP packets are clear and clear, and there is no complicated situation as previously imagined.
It is important to understand that socket is just an abstraction of TCP/IP stack operations, rather than a simple ing relationship!
Article Source: http://hi.baidu.com/webeta/blog/item/394d90efd8bbaeedce1b3eb8.html
Yesterday, I chatted with my friends about network programming. For socket, here is my personal understanding :)
You can create a socket in a program, which can be divided into two types: Common socket and original socket.
I. Common socket is a programming interface (an API) for the operation on the transport layer in the TCP/IP protocol stack ).
There is a connection-oriented streaming socket (sock_stream), which is an application for the TCP method;
Whether a socket is connected to UDP (sock_dgram.
For a common socket, I used to have a fuzzy problem.MultithreadingNext, after the server listens to a port (assuming 8080), a new socket will be generated for each connection to an accept client. So what are the ports of these newly generated sockets? The program is definitely not specified, so there should be two possibilities: 1: generate a random port. 2: port 8080. The first assumption is that the firewall is very likely to block the packets of these random ports. The second assumption is that the server port is still 8080. However, this overwrites my original understanding that "a port is occupied by a program, and other programs cannot use this port ". I think the most likely difference is that the same port cannot be used between the program and the program, but different sockets inside the program can still use the same port. Therefore, in order to enable "packets of the same port (8080) sent from the client to the server and of different threads (that is, different socket connections) to be separated and combined ", there must be a distinguished packet from different connections, that is, the source port in the packet header of the transport layer, that is, the port of the client in a socket connection. To sum up, in this case, the source port (client) in the packet header of the transport layer will be the same (server side) as the socket generated ).
2. The original socket is built on the network layer,So we can build our own protocol on the transport layer..
If you are creating an sniffer (network sniffer) by yourself, the listening packet is a common socket package (TCP or UDP) from the same network segment ), therefore, in the program, we need to write the data structure (IP header and TCP or UDP header) and bind the data.
If both the client and server are written by themselves using the original socket, you can control the Protocol yourself, such as some network applications (such as MSN and Skype), you can rewrite the protocol at the network layer.
Article Source: http://hi.baidu.com/webeta/blog/item/8d1ffbf2356f9a11b07ec5b9.html