I believe many people are interested in network programming. Next we will introduce it in network programming.
Winsock API, the most widely used programming interface.
To use Winsock API programming, You should know some basic knowledge about TCP/IP.
You can directly use the Winsock API to write network applications. However, you must write excellent network
The application must have some knowledge about the TCP/IP protocol.
1. Relationship between TCP/IP protocol and Winsock network programming interface.
Before starting, let's talk about the relationship between Winsock and TCP/IP.
I have met many people who asked me: how to program using Winsock protocol? Actually, this is a bit
Error: Winsock is not a network protocol. It is just a network programming interface
He is not a protocol, but he can access many kinds of network protocols. You can treat him
Some protocol encapsulation. The current Winsock has basically implemented nothing to do with the protocol. You can
Use Winsock to call multiple protocols.
So what is the relationship between Winsock and TCP/IP? In fact, Winsock is
An encapsulation of TCP/IP protocol. You can call the winsock interface function.
For example, if you want to use the TCP/IP protocol to send data, you can use
The winsock interface function send () to call the data sending function of TCP/IP.
How to send data, Winsock has encapsulated this function for you.
2. TCP/IP Protocol Introduction
This section describes the principles of TCP/IP. the TCP/IP protocol has a wide range.
It is a layer-4 protocol that includes various definitions of hardware and software requirements.
Software Knowledge. The exact TCP/IP protocol should be tcp/udp/ip protocol.
User Data Protocol (udp)
It does not guarantee reliable data transmission.
Tcp Protocol (Transmission Control Protocol ).
Stream transmission protocol. It provides reliable, ordered, bidirectional, and connection-oriented transmission.
3. Protect message boundaries and streams
So what is to protect the message boundary and stream?
Message boundary protection means that the transmission protocol treats data as an independent message on the Internet.
Transmission. The receiving end can only receive independent messages. That is to say, there is a message boundary to protect and receive messages.
The sender can only receive one packet sent by the sender at a time.
Streaming does not protect the boundaries of message protection. If the sender continuously sends data,
The acceptor may receive two or more packets in one receiving action.
For example, we send three data packets in a row with a size of 2 kb,
4 k, 8 k, all three packets have reached the network stack of the acceptor.
Using UDP, no matter how large the buffer we use to receive data, we must have
Only when three Receiving actions are completed can all data packets be received.
As long as we set the size of the received buffer to more than 14 K, we can set all
The data packet is received only once.
This is because the UDP Protocol protects the message boundary so that every message is independent.
Streaming transfers data as a series of data streams. He does not consider data as a message.
Therefore, when using tcp communication, it is unclear that tcp is stream-based.
Transmission: when data is continuously sent, they often know that tcp packet loss occurs. Otherwise,
Because when they use a large enough buffer, they may receive two
To more data packets, and many people tend to ignore this point, only the first
But other received data packets are ignored.
You must pay attention to this when programming the network of the class.
4. Simple Winsock programming process
Next we will introduce the Winsock programming method on the Win32 platform.
Communication must have a server and a client.
Let's briefly introduce the general process of tcp server.
For any Winsock-based programming, we must first initialize the Winsock DLL library.
Int WSAStarup (WORD wVersionRequested, LPWSADATA lpWsAData ).
WVersionRequested is the required version of Winsock.
You can call this function to initialize Winsock. Then you must create
Socket (socket ).
SOCKET socket (int af, int type, int protocol );
Sockets can be said to be the core of Winsock communication. All data transmission of Winsock communication,
It is done through sockets. Sockets contain two types of information: IP address and
Port number. With these two information, we can determine any Port number in the network.
Communication nodes.
When we call the socket () interface function to create a socket, we must
You can bind a function to establish a connection with the address you need to communicate.
This is the current contact.
Int bind (SOCKET s, const struct sockaddr FAR * name, int namelen );
Struct sockaddr_in
{
Short sin_family;
U_short sin_prot;
Struct in_addr sin_addr;
Char sin_sero [8];
}
It contains the local address that we need to establish a connection, including the address family, ip address and port information.
In the. sin_family field, we must set it to AF_INET. This tells Winsock
The IP address family is used. sin_prot is the port number for communication. sin_addr
It is the IP address information we want to use for communication.
Here, you must mention the 'big-endian 'Small Header (little-endian )'.
Because different computers process different data in different ways, Intel 86 Processes
Indicates the multi-byte number in the 'small head' situation, that is, to put the low byte in front,
Put the high byte behind, while the Internet standards are the opposite, so we must put the host
The order in which bytes are converted to network bytes. The Winsock API provides several functions.
Function that converts host bytes into network bytes;
U_long htonl (u_long hostlong );
U_short htons (u_short hostshort );
Function that converts network bytes into host bytes;
U_long ntohl (u_long netlong );
U_short ntohs (u_short netshort );
In this way, when we set the IP address and port, the host bytes must be converted to the network
Bytes before bind () function can be used to bind the socket and address.
After binding, the server must establish a listener queue to receive
Connection Request.
Int listen (SOCKET s, int backlog );
This function allows us to convert the socket to the listening mode.
If the client has a connection request, we must also use
Int accept (SOCKET s, struct sockaddr FAR * addr, int FAR * addrlen );
To accept client requests.
Now we have basically completed the establishment of a server,
The process for creating a client is to initialize WinSock and then create a socket.
, And then use
Int connect (SOCKET s, const struct sockaddr FAR * name, int namelen );
To connect to the server.
The following is a simple example of creating a server and a client:
Server creation:
WSADATA wsd;
SOCKET sListen;
SOCKET sclient;
Udint port = 800;
Int iAddrSize;
Struct sockaddr_in local, client;
WSAStartup (0x11, & wsd );
SListen = socket (AF_INET, SOCK_STREAM, IPPOTO_IP );
Local. sin_family = AF_INET;
Local. sin_addr = htonl (INADDR_ANY );
Local. sin_port = htons (port );
Bind (sListen, (struct sockaddr *) & local, sizeof (local ));
Listen (sListen, 5 );
SClient = accept (sListen, (struct sockaddr *) & client, & iAddrSize );
Client creation:
WSADATA wsd;
SOCKET sClient;
Udint port = 800;
Char szIp [] = "127.0.0.1 ";
Int iAddrSize;
Struct sockaddr_in server;
WSAStartup (0x11, & wsd );
SClient = socket (AF_INET, SOCK_STREAM, IPPOTO_IP );
Server. sin_family = AF_INET;
Server. sin_addr = inet_addr (szIp );
Server. sin_port = htons (port );
Connect (sClient, (struct sockaddr *) & server, sizeof (server ));
After a connection is established between the server and the client
Available
Int send (SOCKET s, const char FAR * buf, int len, int flags );
Int recv (SOCKET s, char FAR * buf, int len, int flags );
Function to receive and send data, because the TCP connection is bidirectional.
When you want to disable the communication link, either party can call
Int shutdown (SOCKET s, int how );
To disable the specified function of the socket. Call again
Int closesocket (SOCKET s );
To close the socket handle. Such a communication process is complete.
Note: The above Code does not check function return values. If you are programming on the network, you must
Check the call results of any Winsock API function.
It may not be successful. If the returned value type of the function described above is int
If the usage fails, SOCKET_ERROR is returned.
5. Five models of Winsock programming
The above is just the simplest winsock communication method, but in reality many networks
Communication is difficult to solve.
For example, Winsock provides two socket modes: Lock and non-lock.
Many functions, such as accpet, send, and recv, are used when configuring sockets,
If no data needs to be processed, none of these functions will be returned, that is, your application
The order will block the calls to those functions. If the non-blocking mode is used, call these functions.
No matter whether the data arrives or not, it will return, so it is possible that we are not blocking
In the mode, if you call these functions in most cases, failure is returned, so we need
To handle many unexpected errors.
This is clearly not what we want to see. We can use the Winsock communication model.
To avoid these situations.
Winsock provides five socket I/O models to solve these problems.
Select, WSAAsyncSelect (asynchronous selection ),
WSAEventSelect (Event Selection), overlapped (overlap), completion
Port (complete port ).
Here we will introduce the select and WSAASyncSelect models in detail.
The select model is the most common I/O model.
Use
Int select (int nfds, fd_set FAR * readfds, fd_set FAR * writefds, fd_set FAR * limit TFDs,
Const struct timeval FAR * timeout );
Function to check whether the socket you want to call has data to be processed.
Select contains three socket queues, which represent the following:
Readfds, check readability, writefds, check writability, limit TFDs, and exceptional data.
Timeout is the return time of the select function.
For example, if we want to check whether a socket has data to receive, we can set
Add the receiving handle to the readability check queue and call select. If the socket does not
If data needs to be received, the select function will delete the socket from the readability check queue.
So we only need to check whether the socket handle is still in the readable queue.
You can see whether data needs to be received.
Winsock provides some macros to operate socket queue fd_set.
FD_CLR (s, * set) deletes the handle s from the queue set.
FD_ISSET (s, * set) checks whether the handle s exists in the queue set.
FD_SET (s, * set) adds the handle s to the queue set.
FD_ZERO (* set) initializes the set queue to an empty queue.
WSAAsyncSelect (asynchronous selection) model:
The WSAASyncSelect model establishes a connection between a window and a socket handle
When a network event occurs, a message is sent to the window.
Receives and sends data in the message response function of the window.
Int WSAAsyncSelect (SOCKET s, HWND hWnd, unsigned int wMsg, long lEvent );
This function can establish a connection between the socket handle and the window,
WMsg is a custom message.
LEvent is a network event, including FD_READ, FD_WRITE, and FD_ACCEPT.
, FD_CONNECT, FD_CLOSE.
Several events.
For example, I need to receive network events of FD_READ, FD_WRITE, and FD_CLOSE.
To call
WSAAsyncSelect (s, hWnd, WM_SOCKET, FD_READ | FD_WRITE | FD_CLOSE );
In this way, when there are FD_READ, FD_WRITE or FD_CLOSE network events, the window
HWnd will receive the WM_SOCKET message. The lParam of the Message Parameter indicates the event.
Occurred.
In fact, we should have seen this model, because the CSocket class of MFC uses this model.
Type.
The above describes some Winsock programming methods.
From: http://hi.baidu.com/ailiss/blog/item/75ef0c6deff49afa43169476.html