Chapter 1 - Getting Started with Interconnection
First, we will first learn the basic interconnect rules. Friends of the Internet will understand more or less nouns such as: Networks, Servers, client (Clients), do not know, it doesn't matter, anyway, it will be said. When you begin with Winsock, you can do not have to understand a very deep theoretical knowledge, but this does not mean that you will not use them in high-level network programming. .
Network and protocol
You may have your own definition of the interconnection network. However, it is basically considered that the internet is to connect a large number of computers that do not know each other, exchange data (a bit like a marriage center ...). Some types of networks we often have encountered, such as: LANS (Local Area Network), WANS (Wide Area Network WAN), of course, there is also an Internet. Make sure that these networks can live and steady. The condition is the protagonist today (he is still the protagonist) ----- Agreement.
The protocol The protocol protocol is a rule group describing the data transfer format of interconnect network data exchange.
The above definition is really okay, in short, the agreement is to describe communication between the network. You can imagine it: one of the Chinese characters (equivalent to electronic signal electronic signals), according to habits, grammar rules, etc., organized a meaningful sentence. (Equivalent to the agreement)
2. Ethernet ethernet
The network is based on several protocol layers, and each layer has its own tasks during the network communication. The most famous of the protocol model is the OSI (open internet) model, but actually used TCP / IP (Transmission Control Protocol / Internet Protocol) in the Ethernet. In Ethernet, copper shaft twisted pair (UTP) or fiber is connected between the computer. We can often see the network cable is the copper shaft twisted pair. The cable has also begun to spread slowly (some advertisements will say: Community optical cable 100-to-rise) These hardware is used for networking, first we have to know Ethernet .
Mac
The bottom of the Ethernet is the hardware level, called the media access layer (? Media access control, directly saying how good the data link layer), or simply referred to as Mac. This layer is basically a hardware device, including some network interfaces, and converting the original data into an electrical signal and send it to the appropriate destination.
These packets will eventually reach their destination as they wish, what is the destination? House number? Well, almost, different layer addresses in the Ethernet are also different. Our familiar IP address is not on this layer (which layer will be explained later.) In the minimum layer's MAC level, the address is represented by the MAC address. This is also the NIC MAC address we often hear.
The MAC number Mac Number meets the network interface unit to become a 48-bit identifier. Each network hardware device has only a unique MAC address, and there is no conflict between the manufacturers) and registered in the authority IEEE. The MAC address representation is accustomed to 16-en-numerical use of ':' separation.
Send a packet to another network interface, and the Package needs a destination MAC address, which is a very simple method in the LAN - Broadcasting. That is to send the packet to each of the reachable network interfaces. Those hardware gets the MAC address after the packet, and the MAC address starts to be aligned with its own MAC address, and it is received, not to throw it. This method is very good, it is easy to achieve, and efficient. However, the shortcomings of this method in large networks are undoubted: You must not want your things to send it! A router is used in the WAN (this also discussed later). Now everyone knows that in the bottom, the MAC address is the basic address. Of course, the package needs CRC and error detection. Network layer IP (ICMP)
The hardware level is a layer of IP (Internet Protocol INTO) layer. It is the Internet layer, the same as the upper layer, this layer also has its own IP definition method.
The address specification that Number, Number, is IPv4. A 32-bit value, the middle is divided by ".". 127.0.0.1 IP address is not coded by hardware. It is only marked in the software layer.
IP address should be unfamiliar with everyone. As long as you tap ipconfig in the CMD window you can learn your IP address (of course your computer is on the network). Internet uses it to accurately identify computers on each network. you can say so. As long as a network device uses software, then it will have an IP address. Since IP is the address of a software layer, it always returns to the MAC address, this is the ARP (Address Resolution Protocol) protocol, is very familiar? ARP spoof ... Don't say such a boring thing here, huh, huh. When IP does not match the MAC address, the host sends an inquiry package to the network, one computer in the network, discovers yourself to know this IP address, naturally tell the MAC address of this IP. But no one on the network, I have to send it to the gateway, so you can go to a broader cyberspace to find yourself to know the MAC address. The process of converting the IP and MAC address is in the data link layer (MAC layer).
The IP protocol is added to the packet. There is a TTL jump in the advanced version (we will have a TTL value in ping, we will have a TTL value according to this value, such as 64> Value <128 Estimate the other party is WinNT XX, and the middle How many network devices are skipped, head papers, queues, or what other features. These we don't discuss now ...
TCP
The next layer is the focus of our work (TCP or UDP layers) because it is closely related to various network software sessions. This layer adds the port number to the package.
The port number Port Numberip indicates a clear network device. The port number indicates which service is running on that network device to accept the package. The port number is 16 digits, so a computer has 65535 ports, each program has its own port ("listening"). Our habitual representation is IP: Port 127.0.0.1:80 connection, all ports, 80 ports are basically the port of the web service.
Many ports are WKP (well-known ports), which is the default port, such as the 80-port mentioned above, FTP is port 21, Email (25 SMTP) and (110 POP) are familiar with 135 under Windows 139, 3389 ....... You can naturally use other ports, but it is recommended to use 1024 ports in Windows. The network (IP) layer does not have successful transmission, the transport layer is intended, the transport layer is to ensure that the data can be accurate, how to track the amount of data permitted to make the data segment transmission and reception synchronization, according to the received The amount of data determined by the data is determined and the data is sent. When receiving, the contact is removed, and the virtual connection is established, and the data is detected. In order to provide reliable transmission, the TCP sequences the sequence number of the packet in a specific order before sending new data, and requires the confirmation message to the target machine. TCP is always used to send large quantities of data. TCP is used when the application is to make a confirmation after receiving the data. Due to TCP needs time tracking, this requires additional overhead, making TCP formats are more complicated. UDP can not care if the package will be lost, it only cares about the time, for the audio communication, a few words don't see it ... In contrast, the role of TCP is big, as if there is no one in QQ, no one likes UDP.
Application layer (software Software)
Finally, in the transport layer, the application layer is the application layer. As an initiator, the software you write cannot directly access the TCP layer, and you must pass the Winsock API, on the application level, the system provides a very convenient operation interface, due to The existence of these APIs, we don't have to care for the package, the package size, loss of data, and resend losing packages.
3. Schematic
The above schematic is clearly demonstrated in the connection between the various levels of the Ethernet. All data started by the application layer, and the layer package is last sent to the network. No matter what the data is format (http, ftp ...), it will follow this table. The user's data will first add a TCP header (including the source and destination port), then add the IP header (add source and destination IP address), and finally add the Ethernet head (including the source and destination MAC address) at the data link layer (including the source and destination MAC address) Well, our data can finally be available ..., naturally we hope to minimize these heads, and the winsock helps you do everything ...
4. Some materials
RFC data Google On 'NetWorking Basics' Google ON 'TCP / IP' Winsock Programming FAQ