Visual C + + network programming refers to the use of MFC class libraries (Microsoft Foundation Class Library) in the VC compiler, in order to achieve network applications. The user through the VC programming realization
Network software can transfer files, images and other information between different computers in the network.
Basic knowledge:
If users want to do VC network programming, they must first understand the basic framework and working principle of computer network communication. On two or more computers
Communication between the two parties must follow the same principle of communication. Good data format.
1.OSI seven-layer network model
OSI Network model is a reference model of open system interconnection.
computer that sends information to the computer that receives information
7. Application Layer 7. The application layer represents a physical device in a computer network. Common computer cards, such as the
6. Presentation Layer 6. The presentation layer compresses and decompress the transmitted data
5. Session Layer 5. The session layer transmits data for network transport
4. Data transfer layer 4. Data transfer layer for network transmission of information
3. Network Layer 3. The Network layer establishes a link to the physical network
2. Data link Layer 2. The data link layer represents the transmission of data in a format
1. Physical hardware layer 1. Physical Hardware layer Application interface
The sender transmits data from high to low, and the receiver transmits data from low to high. Each layer of data peer communication.
2.TCP/IP protocol
tcp/ The IP protocol is actually a protocol cluster that contains many protocols. For example, FTP (Text Transfer Protocol), SMTP (Mail Transfer Protocol) and other application layer protocols. The network model for the
TCP/IP protocol has 4 layers:
Network layer: IP protocol and other interconnection protocols
data transfer layer: Provides communication methods for applications, typically TCP,UDP protocol
Application layer: Responsible for processing the actual application layer protocol
3.c/s programming model
c/ The S programming model is a communication model based on reliable connections. 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 by using the C/S model, the other side of the communication is called the client, and the other end is called the server side.
The server side waits for a client connection request to arrive, a process known as a listening process. Typically, the server listener function is performed on a specific IP address and port. The client then sends a connection request like a server, and the server responds to the connection request and the connection succeeds. Otherwise, the client's link request fails.
Note: In actual programming, the user Both the link and the data communication are based on the socket (socket) of the
4.Socket sockets
A network application can be programmed using a socket class encapsulated in MFC, or you can use the WINDOWSAPI function for program development. In comparison, the MFC network programming is relatively simple, the user is also more convenient to use.
When you write a network communication program in Windows, you need to use WindowsSockets (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 specifically used in applications that use TCP protocol communication, and datagram sockets are specifically used in applications that use the UDP protocol for communication.
5. 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 0x357451 is stored using network byte order, the order in which the values are placed in memory will be 0x35,.0x74,0x51. 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.
6.Windows Sockets Introduction
In the MFC class Library, the full functionality of Windows Sockets is almost encapsulated. Introduces two main socket-related classes, namely the CAsyncSocket class and the CSocket class
in the Microsoft Foundation Library, the CAsyncSocket class encapsulates the basic functionality of an asynchronous socket. The steps for network data transfer using this class are as follows:
1). Call the constructor to create a socket object
2). 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.
3). Call the function functions such as send () for data transfer and processing
4). Close or destroy the socket object.
Note: In MFC, there is a variable m_hwnd in all classes that represents the instance handle of the 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 steps for network programming using this class are as follows:
1). Create a CSocket class object
2). 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.
3). Create the CSocketFile class object associated with the CSocket class object
4). Create the CArchive object associated with the CSocket class
5). Use the CArchive class object to transfer data between the client and the server
6). Close or destroy three objects of the CSocket class, the CSocketFile class, and the CArchive class
7.Socket Socket programming
Sockets are a communication bridge designed by the University of Berkeley to communicate data between different hosts in the network. In real life, the network communication software function that people use is based on socket socket as communication bridge. Therefore, sockets in network programming, has a very important role.
1) addressing: Because sockets need to be used in a variety of network protocols, the same addressing method must be used in order to differentiate the network protocols used by the program. For example, in TCPIP protocol communication, the user uses the IP address and port number to determine the communication between the two parties. In other protocols, it is not necessary to use this method to determine the communication parties.
1 struct sockaddr_in{ 2 short sin_family; // Specify address family address format 3 unsigned short sin_port; // port number 4 struct in_addr sin_addr; // IP address 5 char sin_zero[ 8 ]; // reserved, need to be specified as 0 6 }
In this structure, the member sin_family specifies the address family that uses the socket address. This must be set to Af_inet, which indicates that the address family used by the program is TCP/IP
Note: The last member of the structure is not actually used, primarily to set the same size as the first version of the socket address structure. When actually used, set the 8 bytes directly to 0.
The struct member variable sin_addr represents the 32-bit IP address structure. Its structure is defined as follows:
1 structin_addr{2union{//Union function (common piece of memory, whichever is the longest in each member)3 struct{4UnsignedChars_b1,s_b2,s_b3,s_b4;5}s_un_b;//describe an IP address with 4 U_char characters6 struct{7Unsigned Shorts_w1,s_w2;8}s_un_w;//describe an IP address with 2 u_short types9 TenUnsignedLongS_ADDR;//describe an IP address with 1 U_LOGN types One }s_un; A};
Usually we use a u_long type character in network programming to describe an IP address, for example:
1 sockaddr_in addr; 2 addr.sin_addr. S_un. S_ADDR = inet_addr ("192.168.1.0")
In the program, you first define the SOCKADDR_IN structure object addr, and then assign a value to the member s_addr in the IP address structure in_addr. Because the IP addresses described by struct member s_addr are network byte order, the program calls the INET_ADDR () function to convert the string IP to an IP address arranged in network byte order.
2) byte order: In Socket sockets programming, the order in which the data is transmitted is based on the network byte order and host byte order. Typically, if a user sends data over the network, it is necessary to convert the data to be sorted in network byte order, which could cause data corruption. If the user is storing the data received on the network on the local computer, the data needs to be converted to host byte order.
The network byte order stores the most important bytes in the data first, while the host byte order stores the unimportant bytes first.
The values of member s_addr in the IP address structure in_addr are arranged in network byte order.
2.1) byte order conversion function:
Several conversion functions are provided in Winsock between the network byte order and the host byte order. The function is defined as follows:
1U_short htons (U_short hostshort);//converts an IP address of type u_short from host byte order to network byte order2U_long htonl (U_long hostlong);//converts an IP address of type U_long from host byte order to network byte order3U_long Ntohl (U_long netlong)//converts an IP address of type u_long from network byte order to host byte order4U_short Ntohs (u_short netshort)//converts an IP address of type u_short from network byte order to host byte order5UnsignedLongINET_ADDR (Const CharFAR*CP)//converts a string IP to an IP address in network byte order6 Charfar* Inet_ntoa (structIn_addrinch)//converts an IP address in network byte order to a string IP
Instance:
1Sockaddr_in addr;//Define socket address structure variables2IN_ADDR In_add;//the IP address structure variable can be3addr.sin_family = af_inet;//define the address family as TCP/IP4Addr.sin_port = htons ( the);//Specify port number5Addr.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1")//converts a string IP to a network byte-ordered IP6 CharAddress[] = Inet_ntoa (addr.sin_addr. S_un. S_ADDR)//Swap network byte-ordered IP to string IP
In the program, first use INET_ADDR () to convert the string IP "127.0.0.1" to the IP address structure member s_addr in network byte order. Then, use the function Inet_ntoa () to convert the IP value represented by the member to a string IP
8.Socket Correlation function
1) Creating sockets
Creating a Socket object using the CSocket class is created by the constructor of the class. Csocket:csocket ()
Creating a CSocket Class object
1 CSocket sock;
If the user needs to create a socket object pointer, it should be created using the keyword new.
1 New CSocket
2) Binding address information
BOOL Bind (const sockaddr* lpsockaddr,int nsockaddrlen);
The function is to bind the socket object to the server address structure. Returns true if the function call succeeds. Otherwise, returns false. The parameter lpsockaddr specifies the server address structure that will be bound, and the parameter Nsockaddrlen represents the length of the address structure.
On the server side, when the address information binding socket succeeds, you also need to call the function listen () to listen for the client's link request on the specified port.
1 BOOL Listen (int5)
The parameter nconectionbacklog indicates the maximum number of sockets that the socket listens for client requests.
1CSocket sock;//creating a Socket object2Sockaddr_in addr;//Define socket address structure variables3IN_ADDR In_add;//defining IP Address structure variables4addr.sin_family = Af_inet//Specify the address family as TCP/IP5Addr.sin_port = htons ( the)//Specify port number6Addr.sin_addr. S_UN,S_ADDR = inet_addr ("127.0.0.1")//converts a string IP to a network byte-ordered IP7 8Sock. Bind ((sockaddr*) addr,sizeof(addr))//binding sockets and address structures9Sock. Listen (5);//Listening Port
3) Connect the server
After the client creates the socket successfully, it can call the function connect () to send a connection request to the server. The function prototypes are as follows:
1 Bool Connect (constconst sockaddr* lpsockaddr,int nsockaddrlen);
Instance:
1CSocket sock;//creating a Socket object2Sockaddr_in addr;//Define socket address structure variables3IN_ADDR In_add;//defining IP Address structure variables4addr.sin_family = af_inet;//Specify the address family as TCP/IP5Addr.sin_port = htons ( the);//Specify port number6Addr.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1");//converts a string IP to a network byte-ordered IP7 8Sock. Connect ((sockaddr*) addr,sizeof(addr))//connecting to a server
4) Data exchange
Both the server and the client are exchanging data through the function send () and receive (). Function Prototypes:
1 int Send (constvoid* lpbuf,int nbuflen,int0); 2 int Receive (void* lpbuf,int nbuflen,int0);
where function Send () is used to send data for the specified buffer, the function recive () is used to receive data sent by the other, and the data is stored in the specified buffer. The parameter lpbuf represents the data buffer address. The parameter Nbuflenvia represents the size of the data buffer. The parameter nflags represents the flag that the data is sent or received, and in general, the parameter is set to 0. For example, use these two functions to send and receive data.
1 CharBuff[] ='123456';2Sock. Send (&buff,sizeof(Buff),0)3Sock. Receive (&buff,sizeof(Buff),0)4VirtualintReceive (void* Lpbuf,intNbuflen,intnflags =0);5VirtualintReceive (void* Lpbuf,intNbuflen,intnflags =0);
5) Close the socket object
1 void Close (); sock. Close (); // Closing the socket object socket closes the connection between the server and the client.
VC + + Network Programming Learning Notes