To write a network program, you have to use a socket, which is what programmers know. Also, during the interview, we will ask the other party will not socket programming? Generally speaking, many people will say, socket programming is basically listen,accept and send,write, and so on a few basic operations. Yes, as with common file operations, you must know when you write.
For the network programming, we also say that TCP/IP, it seems that other network protocols no longer exist. For TCP/IP, we also know that TCP and UDP, the former can guarantee the correct and reliable data, the latter allows data loss. Finally, we also know that you must know the IP address and port number of the other person before establishing the connection. In addition to this, ordinary programmers will not know too much, many times this knowledge is enough. At most, when a service program is written, multithreading is used to handle concurrent access.
We also know the following facts:
1. A specified port number cannot be shared by multiple programs. For example, if IIS occupies 80 ports, then Apache cannot also use port 80.
2. Many firewalls only allow packets of specific destination ports to pass through.
3. After the service program listen a port and accept a connection request, a new socket is generated to process the request.
So, a problem that puzzled me for a long time arose. If a socket is created and bound to port 80, does that mean that the socket takes up 80 ports? If so, what port does the new socket generated after it accept a request be used (I always thought the system would assign it a free port number by default)? If it is a free port, it must not be 80 port, so the future TCP packet destination port is not 80-the firewall will certainly organize its adoption! 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. My puzzle is, why does the firewall not block such a connection? How does it determine that the connection was generated because of the CONNET80 port? Are there any special flags in the TCP packet? Or the firewall remembers something?
Later, I carefully studied the TCP/IP protocol stack principle, a lot of concepts have a deeper understanding. For example, the TCP and UDP belong to the transport layer, which is erected on the IP layer (network layer). The IP layer is primarily responsible for the delivery of packets between nodes (end to end), where the node is a network device, such as a computer. Because the IP layer is only responsible for sending the data to the node, not the different applications above, so the TCP and UDP protocol on the basis of the addition of port information, the port is identified by a node is an application. In addition to adding port information, the UPD protocol basically does not have any processing of the IP layer data. The TCP protocol also adds more complex transmission control, such as sliding data sending Windows (Slice window), and receiving acknowledgement and retransmission mechanisms to achieve reliable data transmission. Regardless of what the application layer sees as a stable TCP traffic, the following is a packet of IP packets, which need to be reorganized by the TCP protocol.
So, I have reason to suspect that the firewall does not have enough information to judge TCP packets in addition to the IP address and port number. And, we also see that the so-called port, is to distinguish between different applications, in order to be able to correctly forward when different IP packets come.
TCP/IP is just a protocol stack, just like operating system operation mechanism, must be implemented specifically, but also to provide external operating interface. Just as the operating system will provide a standard programming interface, such as the Win32 programming interface, TCP/IP must also provide a programming interface, this is the socket programming interface-This is what it is!
In the socket programming interface, the designer puts forward a very important concept, that is, the socket. This socket is similar to a file handle and is actually stored in the same process handle table as the file handle in the BSD system. This socket is actually a sequence number that represents its position in the handle table. We've seen a lot of this, like file handles, window handles, and so on. These handles, in fact, represent certain objects in the system that are used as parameters in various functions to manipulate specific objects--this is actually a C language problem, in the C + + language, this handle is actually the this pointer, is actually the object pointer.
Now we know that sockets are not necessarily associated with TCP/IP. The socket programming interface is designed to adapt to other network protocols as well. Therefore, the appearance of the socket is only more convenient to use the TCP/IP protocol stack, it is the TCP/IP abstraction, forming a few of the most basic function interface. such as Create,listen,accept,connect,read and write and so on.
Now we understand that if a program creates a socket and lets it listen on port 80, it actually declares its possession of port 80 to the TCP/IP protocol stack. Later, all TCP packets that target port 80 are forwarded to the program (the program here, because the socket programming interface is used, so the socket layer is processed first). The so-called accept function, in fact, the abstract is the TCP connection establishment process. The new socket returned by the Accept function actually refers to the connection created this time, and one connection consists of two pieces of information, one is the source IP and the source port, and the other is the host IP and the host port. Therefore, accept can produce a number of different sockets, and these sockets contain the host IP and the host port is constant, the change is only the source IP and the source port. In this case, the socket port can be 80, and the socket layer can be based on the source/host to accurately identify the IP packet and socket attribution, thus completing the operation of the TCP/IP protocol encapsulation! At the same time, the rules for the handling of IP packets on the fire wall are clear, and there are no complex scenarios that are envisaged earlier.
It's important to understand that the socket is just an abstraction of the TCP/IP stack operation, not a simple mapping relationship!
1. TCP connection
Mobile phone can use the network function is because the bottom of the mobile phone implementation of TCP/IP protocol, you can make the mobile phone terminal through the wireless network to establish a TCP connection. TCP protocol can provide an interface to the upper network, so that the transmission of the upper network data is based on the "No Difference" network.
Setting up a TCP connection requires a "three-time handshake":
First handshake: The client sends a SYN packet (SYN=J) to the server and enters the Syn_send state, waiting for the server to confirm;
Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=J+1), and also send itself a SYN packet (syn=k), that is, the Syn+ack packet, when the server enters the SYN_RECV state;
Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and the server enter the established state, and the handshake is completed three times.
The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively closes the connection. When disconnected, both the server and the client can initiate a request to disconnect the TCP connection, and the disconnection process requires a "four-time handshake".
Note: Anack of 1 indicates that the confirmation number is valid, and 0 indicates that the message does not contain a confirmation message, ignoring the confirmation number field
SYN: The first packet of a TCP connection, a very small packet. SYN attacks include a large number of such packages that cannot be processed effectively because they appear to come from a site that does not actually exist.
The "Four-time handshake" experienced by disconnecting
When a party disconnects, a fin packet is sent to the other party, and the other party sends an ACK acknowledgement packet after receiving the FIN packet, so the two-way connection usually needs to send a pair of fin and ack four handshake from each TCP endpoint.
2. HTTP connection
The HTTP protocol, the Hypertext Transfer Protocol (hypertext Transfer Protocol), is the foundation of Web networking and one of the most commonly used protocols for mobile networking, anapplication built on the TCP protocol .
The most notable feature of an HTTP connection is that each request sent by the client requires a server loopback response, and the connection is actively released after the request has ended. the process from establishing a connection to closing a connection is called a "one-time connection."
1) in HTTP 1.0, each request from the client requires a separate connection to be established, and the connection is automatically freed after the request is processed.
2) in HTTP 1.1, multiple requests can be processed in a single connection, and multiple requests can overlap, without waiting for a request to end before sending the next request.
Because HTTP is actively releasing the connection after each request ends, the HTTP connection is a "short connection", which requires constant connection requests to the server to maintain the client program's online status. As a general practice, there is no need to obtain any data immediately, and the client will keep a "keep-connected" request to the server at regular intervals, and the server responds to the client after receiving the request, indicating that the client is "online". If the server can not receive the client's request for a long time, it is considered that the client "offline", if the client cannot receive a reply from the server for a long time, it is considered that the network has been disconnected.
3. Socket principle
3.1 Socket (socket) concept
Socket (socket) is the cornerstone of communication and is the basic operating unit of network communication supporting TCP/IP protocol. It is an abstract representation of the endpoint in the network communication process and contains five kinds of information that must be used for network communication: the protocol that the connection uses, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol port of the remote process.
When the application layer communicates data through the transport layer, TCP encounters a problem that provides concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may require data to be transmitted over the same TCP protocol port. To differentiate between different application processes and connections, many computer operating systems provide a socket (socket) interface for applications interacting with the TCP/IP protocol. Application layer can and transport layer through socket interface, distinguish from different application process or network connection communication, realize the concurrent service of data transmission
transferred from: http://www.cnblogs.com/riacool/archive/2010/12/14/1905404.html1, the service End void Cadlg::onbutton1 () {CSocket jin,she; Jin. Create (6666); Jin. Listen (5); Jin. Accept (She); client void Cadlg::onbutton1 () {CSocket Jin; Jin. Create (); Jin. Connect ("127.0.0.1", 6666);} Service-side of Jin. Accept (She); Is in a blocked state while client Jin. Connect ("125.74.66.77", 6666), wake-up service-side blocking 2,#define SERVER_MESSAGE wm_user+100//server-side message designator # define Client_ MSG wm_user+101//Client message designator
3.afx_msg LRESULT onservermessage (WPARAM wparam,lparam LPARAM) and On_message (ser_message,onservermessage):<span style= "FONT-SIZE:18PX;" >on_message (Ser_message,onservermessage) This is just the time to fill in the Message mapping table, the use of a macro, that is, to make ser_message messages, and onservermessage functions associated. </span>
<span style= "FONT-SIZE:18PX;" > is when you trigger this ser_message message and then call Onservermessage (WPARAM Wparam,lparam LPARAM) to handle the function. </span>
4. sock_addr.sin_addr. S_un. S_ADDR=INET_ADDR (Ipaddresstemp.getbuffer (0)); If you are communicating on your own computer, the IP must be native: Inet_addr ("192.168.1.106") or: Set the circuit of the cost to the machine 127.0.0.1
5. typedef clist<socket,socket&> Socket_array; Explanation:
In C + +, CList is a template type name in the above type definition, which requires two placeholders: socket and socket&, which are used when instantiating objects of the class. For example: Declare a template class object statement is Clist<socket,socket&> ob_client, where the Socket is a defined type name, Object ob_client after instantiation, All the places defined as sockets and socket& in class CList will be replaced by sockets and socket&, respectively.
As shown in: Tp-linker input 192.168.1.1, forwarding rules--Virtual server, if you need to be able to use your own TCP/IP programming in different wireless networks, then you need to bind your port number and router to your IP number, Then other users can communicate with you via this port number and IP address.
MFC socket Programming (light out + depth: Server and client port issues)