The 1th chapter of Visual C + + network development Basic Application
Visual C + + technology is powerful, in the network domain, can develop a lot of network applications. In this chapter, you will learn more about developing basic network applications using Visual C + + technology. This chapter describes the most basic application content, the purpose is to enter the book behind the study to lay a good foundation.
1.1 Get the type and MAC address of the NIC
The type of the NIC can be obtained from the registry. A MAC is an abbreviation for Media Access control, which is also known as a hardware address that defines the location of a network device. In the OSI model, the third layer (the network layer) is responsible for the IP address, and the second layer (the Data Link layer) is responsible for the MAC address. So a host will have an IP address, and each network location will have a MAC address dedicated to it. In this section, you will explain how to develop a MAC address program using Visual C + + technology. Before specific programming, first explain the basic knowledge related to it.
1.1.1 Visual C + + Network Programming Overview (1)
Visual C + + (later VC) network programming refers to the use of MFC Class Library (Microsoft Foundation Class Library) in the VC compiler to write programs to achieve network applications. Users through the VC programming network software can be in the network between different computers to transfer files, images and other information. This chapter introduces users to the fundamentals of network programming based on the Windows operating system, and its development environment is VC. In the VC compiler, using a Windows socket for Network program development is a very important part of network programming.
1. Network Basics
If the user wants to carry on the VC network programming, must first understand the computer network communication basic frame and the working principle. When network communication occurs between two or more computers, both parties to the communication must also follow the same communication principles and data formats.
The next step is to introduce the OSI layer Seven Network model, TCP/IP protocol and C/s programming model to the reader.
(1) OSI seven-layer network model
OSI Network model is a reference model of open system interconnection. Through this reference model, users can very intuitively understand the basic process and principle of network communication. As shown in OSI Reference Model 1-1.
|
Figure 1-1 OSI seven layer network reference Model |
From the OSI network model shown in 1-1, you can see the flow of data from the sender to the receiver and the communication layer and the corresponding communication protocol in the network data.
In fact, at the sending end of the network communication, the communication data of each communication layer is added to the data by the layer protocol. On the contrary, when the data passes through each layer, it will be stripped of the corresponding packet header data by the layer protocol. Users can also understand that each layer in the network model is peering. In the OSI seven-tier network model, each network layer has its own capabilities, as shown in table 1-1.
Table 1-1 functions of each network layer
Protocol Layer Name |
Features Overview |
Physical hardware Layer |
Represents a physical device in a computer network. A common computer card, etc. |
Data Link Layer |
Compress and decompress the transmitted data |
Network layer |
Transfer data for network transmission |
Data transfer Layer |
Network Transmission of information |
Session Layer |
Establishing a connection to a physical network |
Presentation Layer |
To represent the transmitted data in some format |
Application Layer |
Application interface |
(2) TCP/IP protocol
The TCP/IP protocol is actually a protocol cluster, which includes many protocols. such as FTP (File Transfer Protocol), SMTP (Mail Transfer Protocol) and other application layer protocols. The network model of the TCP/IP protocol is only 4 layers, including the data Link layer, network layer, data transmission layer and application layer, as shown in 1-2.
|
Figure 1-2 TCP/IP network protocol model |
In the TCP/IP network programming model, the capabilities of each layer are shown in table 1-2.
Table 1-2 features of the TCP/IP network protocol layers
protocol layer name |
features overview |
data link layer |
< P class= "a" > network hardware devices such as NIC and drivers |
network layer |
IP protocol and other interconnection protocols |
|
|
|
The actual application layer protocol for processing the application |
In the data transfer layer, the TCP and UDP protocols are included. The TCP protocol is based on a reliable connection-oriented communication protocol, which has a re-sending mechanism, that is, when the data is corrupted or lost, the sender will resend the data. The UDP protocol is based on the User Datagram Protocol, which belongs to the unreliable connection communication protocol. For example, when a message is sent using the UDP protocol, it is not known whether the message has reached the receiver, or whether the data has been lost during transmission. But in the instant communication, the UDP protocol plays an important role in some time-demanding network data transmission.
(3) C/S programming model
The C/S programming model is a communication model based on reliable connection. Both sides of the communication must use their respective IP addresses and ports for communication. Otherwise, the communication process will not be implemented. Typically, when a user communicates using the C/S model, either side of its communication is called the client, and the other party is called the server side.
The server side waits for the client connection request to arrive, this process is called the listening process. Typically, the server listener function is performed on a specific IP address and port. The client then makes a connection request to the server, and the server rings the request and the connection succeeds. Otherwise, the client's connection request fails. The C/S programming model 1-3 shows.
|
Figure 1-3 C/s programming model |
Because the client connects to the server by using the server's IP address and listening port number to complete the connection, the server's IP address and port must be fixed. Here, the user is introduced to the port number used by some protocols. For example, the port number used by the HTTP protocol (for the Web browsing Service) for the 80,FTP protocol (for file transfer) is 21.
2. Fundamentals of Network programming
You can use socket classes encapsulated in MFC to write network applications, or you can use Windows API functions for program development. MFC network programming is relatively simple, it is very convenient to use. However, the use of MFC-related class programming will give users a clear understanding of the fundamentals of network communication. Using Windows API functions, in contrast, can make users familiar with the fundamentals of network communication. In the actual programming process, the connection of both sides of communication and data communication are based on socket (socket).
(1) Sockets socket
When you write a network communication program in Windows, you need to use Windows Sockets (Windows Sockets). The API functions associated with Windows Sockets are called Winsock functions.
Both sides of the network communication have their own sockets, and the socket is associated with a specific IP address and port number. Typically, there are two main types of sockets, namely, streaming Sockets (SOCK_STREAM) and datagram Sockets (SOCK_DGRAM). Where streaming sockets are dedicated to applications that use TCP protocol communication, and datagram sockets are dedicated to applications that use the UDP protocol for communication.
(2) Network byte order
Network byte order refers to the format in which data transfer is used in the TCP/IP protocol, and the relative byte order is the host byte order. Network byte order means that the most important bytes in the data are stored first. For example, when data 0x358457 is stored using network byte order, the value in memory will be 0x35, 0x84, 0x57. Because the communication data may be transferred between different machines, the communication data must be organized in the same format. Only formatted communication data can be transferred between different machines.
3. Basic process of network communication
To communicate over the Internet, the user needs at least a pair of sockets, one of which runs on the client side, which we call Clientsocket and the other running on the server side, which we call ServerSocket. According to the characteristics of network communication, sockets can be divided into two types: streaming sockets and datagram sockets. The connection process between sockets can be divided into three steps, namely, server listening, client request, and connection acknowledgement. Specific instructions are shown in 1-4.
|
Figure 1-4 The connection process between sockets |
4. Build the development environment
In the Visual C + + 6.0 environment for the Winsock API programming development, you need to import the following three files in the project, or a compilation error will occur.
The header file of the Winsock.h:winsock API needs to be included in the project.
WSOCK32.lib:WINSOCK API link library file, use it as a non-default link library for the project to include in the project file.
WINSOCK.dll:WINSOCK's dynamic link library, located under the Windows installation directory.
5. Two commonly used data structures
The socket is an abstract representation of the endpoint in the network communication process, created as a handle in the implementation, and contains 5 kinds of information necessary for network communication: The protocol used by the connection, 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.
The data structures commonly used in Winsock programming are sockaddr_in and in_addr.
(1) SOCKADDR_IN structure
Winsock encapsulates the information about the socket through the SOCKADDR_IN structure:
- struct SOCKADDR_IN {
- Short sin_family;
- unsigned short sin_port;
- IN_ADDR sin_addr;
- Char Sin_zero[8];
- };
Each parameter in the above structure is specified as follows.
Sin_family: Refers to the type of address used to identify different devices on the network, and for IP addresses, it is af_inet.
Sin_port: Refers to the port number that the socket corresponds to.
SIN_ADDR: is a structure that encapsulates IP.
Sin_zero: An array to populate the structure with a character embox of 0, which can be the same size for different address types.
(2) IN_ADDR structure
The IN_ADDR structure encapsulates an IP address, either in 4 single-byte numbers or as a two double-byte representation or as a four-byte number. This is defined for ease of use, such as when initializing an IP in a program, you can pass in 4 single-byte integers, and when passing this value between functions, you can convert it to a four-byte integer. The IN_ADDR structure is defined as follows:
- struct IN_ADDR {
- Union {
- struct {U_char s_b1, s_b2, S_b3, S_B4;} S_un_b;
- struct {u_short s_w1, s_w2;} S_un_w;
- U_long s_addr;
- } S_un;
- };
6. Windows Sockets Basics
In the MFC class library, the full functionality of Windows Sockets is almost encapsulated. In the following sections, we will briefly describe the two most common socket-related classes--casyncsocket classes and CSocket classes.
(1) CAsyncSocket class
In the Microsoft Base Class Library, the CAsyncSocket class encapsulates the basic functionality of an asynchronous socket. The steps the user uses to perform network data transfer are as follows.
① calls the constructor to create the socket object.
② If you create a server-side socket, call the function bind () to bind the IP and port, and then call the function listen () to listen for the client's request. If the request arrives, the Call function accept () should respond to the request. If you create a client socket, directly call the function connect () to the server.
③ calls the function functions such as send () for data transfer and processing.
④ Close or destroy the socket object.
(2) CSocket class
The CSocket class is derived from the CAsyncSocket class. This class not only has the basic function of CAsyncSocket class, but also has the function of serialization. In real programming, users can manage data and send data well by using the CSocket class with the CSocketFile class and the CArchive class. The following steps are used by the user to program the network in this class.
① Create the CSocket class object.
② If you create a server-side socket, call the function bind () to bind the IP and port, and then call the function listen () to listen for the client's request. If the request arrives, the Call function accept () should respond to the request. If you create a client socket, directly call the function connect () to the server.
③ creates a CSocketFile class object associated with the CSocket class object.
④ creates a CArchive object associated with the CSocketFile class.
⑤ uses the CArchive class object to transfer data between the client and the server.
⑥ closes or destroys 3 objects of the CSocket class, CSocketFile class, and CArchive class.
1.1.2 The principle of MAC address
Mac means media access control. The MAC address is the MAC address that is burned in the NIC (Network Interface card,nic), also called the hardware address, which is made up of 48 extra-long (6-byte) hexadecimal digits. Where the 0~23 bit is called the organization Unique identifier (organizationally unique Identifier), is the identification of the LAN (LAN) node, and the 24~47 bit is assigned by the manufacturer itself. Where the 40th bit is the multicast address flag bit. The physical address of the network card is usually the EPROM (a flash chip) that is burned into the NIC by the manufacturer of the network card, which stores the address of the computer on which the data was transmitted and the host that received the data.
In the physical transmission of the network at the bottom, through the physical address to identify the host, it is generally the world's only. For example, the physical address of an Ethernet card is a 48bit (bit) integer, such as the 44-45-53-54-00-00 format, which is stored in a machine-readable way into the host interface. Ethernet Address Management (IEEE, Institute of Electrical and Electronics Engineers) divides the Ethernet address (that is, the different combinations of 48 bits) into several separate contiguous address groups, and the manufacturer of the Ethernet network card buys one of them, assigning these unique addresses to the Ethernet card individually at production time.
This shows that the MAC address is just like the ID number on our ID card, with global uniqueness. In the Windows operating environment, select Start → run, then enter "cmd" in the "Run" dialog, open the Command line window, enter "Ipconfig/all" (note that there is a space between ipconfig and/) to get the MAC address of our machine, As shown in 1-5.
|
Figure 1-5 Input Ipconfig/all get MAC address |
1th Basic Application of Visual C + + network development