Document directory
- I. initial deployment of Winsock
- Ii. error message
- 3. Select a protocol
- 4. Establish a socket
- V. Server API functions
- Vi. Client API functions
- TCP status
- Connect
- Send and WSASend
- WSASendDisconnect
I. initial deployment of Winsock
If no Initialization is performed, all Winsock function operations fail (et_error is reversed), and the error code is WSANOTINITIALISED.
Initial functions:
Int WSAStartup (WORD version, LPWSADATA pWSADATA)
You can use a macro to create a version number: MAKEWORD (x, y)
WSADATA structure:
{
WORD version
High Version of WORD
Char [] Description
Char [] system status
Unsigned short iMaxSockets (compatible with earlier versions)
Unsigned short iMaxUdpDg (compatible with earlier versions)
Char Far * lpVendorInfo is also compatible and retained
}
This is the running status of the connection on my machine.
WSAStartup (MAKEWORD (2, 2), & wsaData );
| WVersion |
514 |
Unsigned short |
| WHighVersion |
514 |
Unsigned short |
| SzDescription |
0x0012fd18 "WinSock 2.0" |
Char [257] |
| SzSystemStatus |
0x0012fe19 "Running" |
Char [129] |
| IMaxSockets |
0 |
Unsigned short |
| IMaxUdpDg |
0 |
Unsigned short |
| LpVendorInfo |
0 xcccccccc <incorrect pointer> |
Char * |
514 is 0x202, which is also our version number. The last three items are ignored.
The following table shows the supported winsock versions of each platform.
Platform |
Winsock Version |
Windows 95 |
1.1 (2.2) |
Windows 98 |
2.2 |
Windows Me |
2.2 |
Windows NT 4.0 |
2.2 |
Windows 2000 |
2.2 |
Windows XP |
2.2 |
Windows CE |
1.1 |
Int WSACleanup ():
Terminate the use of the Winsock function.
Ii. error message
Use int WSAGetLastError (void) to detect the error code when the Winsock function returns SOCKET_ERROR. The error name corresponding to the error code can be found in winsock. h or winsock2.h.
H_errno is the specified macro.
3. Select a protocol
Here is a brief introduction to establishing the most basic Winsock through the Internet Protocol (IP) Protocol. The main reason why a large part of winsock programs use it is that it is widely used. Winsock can also use other protocols, such as IPX.
In terms of design, IP addresses are connection protocols, but not data transmission protocols. We can use Two higher-level protocols-Transmision Control Protocol (TCP) or User datasync Protocol (UDP). They all use IP addresses. We will talk about TCP/IP and UDP/IP together. If you want to use IPv4 (IP version 4), you must know how to use IPv4
Use IPv4
In IPv4, the computer allocates a 32-bit address. to connect to the client through TCP or UDP, you must know the IP address and port of the host. Similarly, to listen to client requests, the host must specify an IP address and port. In Winsock, the program indicates that the IP address and service port information are in the SOCKADDR_IN structure. His statement is as follows:
Struct sockaddr_in
{
Short sin_family;
U_short sin_port;
Struct in_addr sin_addr;
Char sin_zero [8];
};
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;
Af_inet is required for sin_family to tell Winsock that we use an IP address.
Sin_port indicates the TCP or UDP port we choose as our communication port. By the way, some port numbers are reserved for some services, such as FTP and HTTP.
Sin_addr uses 4 bytes to store IPv4 addresses, just like the unsigned long integer (DWORD). IP addresses are generally in A. B. C. d format on the Internet.
Sin_zero only makes the addr_in and sockaddr have the same structure size.
The following is a very useful function to convert an IP address in the. B. C. d format to an unsigned long integer.
Unsigned long inet_addr (const char far * CP );
Byte order
Different computers process numbers in two ways: Big-Endian and little-Endian (
For example, 0x12345678 is saved in the (0x78 0x56 0x34 0x12) mode,
Big-endian format data, for example, 0X12345678 is saved in the (0X12 0X34 0X56 0X78) mode. This depends on how they are designed, for example, Intel's x86 processor uses the little-endian type for multiple bytes. IP addresses and ports are stored in multiple bytes on the computer. They are in the host-byte order. However, when the IP addresses and ports pass through the network, they must be converted to big-endian, that is, the network-byte sequence.
There are a series of functions to complete the conversion between the two. For example
Host-byte to network-byte
U_long htonl (u_long hostlong );
Int WSAHtonl (SOCKET s, u_long hostlong, u_long FAR * lpnetlong );
U_short htons (u_short hostshort );
Int WSAHtons (SOCKET s, u_short hostshort, u_short FAR * lpnetshort );
Network-byte to host-byte
U_long ntohl (u_long netlong );
Int WSANtohl (SOCKET s, u_long netlong, u_long FAR * lphostlong );
U_short ntohs (u_short netshort );
Int WSANtohs (SOCKET s, u_short netshort, u_short FAR * lphostshort );
Example:
SOCKADDR_IN addr;
INT port = 8080;
Addr. sin_family = AF_INET;
Addr. sin_addr.s_addr = inet_addr ("216.239.57.99 ");
Addr. sin_port = htons (port );
4. Establish a socket
Use api socket socket (int af, int type, int protocol );
The first parameter is the Protocol address type. For example, if IPv4 is used, af is AF_INET,
The second parameter is the socket type of the Protocol. When you use TCP/IP, type = SOCK_STREAM, And when you use UDP/IP, type = SOCK_DGRAM,
The third parameter is the protocol (unknown). If TCP is used, IPPROTO_TCP is used. If UDP is used, IPPROTO_UDP is used.
V. Server API functions
A server is a process that waits for an indefinite number of client connections to respond to client requests. A server must have a name for the client to locate. In TCP/IP, the name is an IP address and port.
Step 1: Use socket or WSASocket to create a socket and bind it
Step 2: The socket enters the listening mode. (Listen)
Finally, the client responds to the request when sending a request. (Accept or WSAAccept)
Binding)
Int bind (SOCKET s, const struct sockaddr FAR * name, int namelen );
The first parameter is the socket to be bound;
The second parameter indicates the protocol you are using.
The third parameter indicates the length of the Protocol address structure.
Example:
SOCKET s;
SOCKADDR_IN addr;
Int port = 5555;
S = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP );
Addr. sin_family = AF_INET;
Addr. sin_port = htons (port );
Addr. sin_addr.s_addr = htonl (INADDR_ANY );
Bind (s, (SOCKADDR *) & addr, sizeof (addr ));
Listening)
Switch the socket to the listening mode. The bind is only bound, and the listen notifies the socket to enter the connection waiting for entry.
Int listen (SOCKET s, int backlog );
The first parameter is the bound socket.
The second parameter is the maximum queue length. For example, if the number is set to two and three customers are connected, the first two are connected to the queue, the third one will receive the WSAECONNREFUSED error message. Note that the server has an Accept connection, and the connection will be removed from the queue.
If you directly listen without bind, you will receive the WSAEINVAL error message.
Accepting Connectino)
SOCKET accept (SOCKET s, struct sockaddr FAR * addr, int FAR * addrlen );
The second parameter is the client address you receive.
The third parameter addrlen indicates the length of the addr.
Vi. Client API functions
Step 1: Create a socket
Step 2: Set the SOCKADDR address of the object to be connected
Step 3: connect with connect or WSAConnect.
| TCP status At first, each socket is in the CLOSED state. When the client initiates a connection, it sends a SYN packet to the server and the client enters the SYN_SENT state. The server receives the SYN Packet and returns a SYN-ACK packet. After receiving the packet, the client returns an ACK packet. The client changes to the ESTABLISHED State. If the SYN-ACK packet is not received for a long time, the client times out and enters the CLOSED state. When the server binds and listens to a port, the socket status is LISTEN. When the client attempts to establish a connection, the server receives a SYN Packet and returns the SYN-ACK packet. The server status changes to SYN_RCVD. When the client sends an ACK packet, the server socket status changes to ESTABLISHED. When a program is in the ESTABLISHED State, there are two ways to close it: active and passive. If you want to disable it, send a FIN package. When your program closesocket or shutdown (Mark), your program sends a FIN packet to the peer, and your socket changes to the FIN_WAIT_1 state. Peer reports an ACK packet and your socket enters the FIN_WAIT_2 state. If the peer is also shutting down the connection, it will send a FIN package to your computer. You feed back an ACK package and convert it to the TIME_WAIT status. TIME_WAIT status and 2MSL wait status. MSL indicates the Maximum Segment Life Cycle (Maximum Segment life time) indicates the time when a packet exists between the network and when it is discarded. Each IP packet has a TTL (time_to_live). When it is reduced to 0, the packet is discarded. Each vro reduces the TTL value by one and transmits the packet. When a program enters the TIME_WAIT state, it has two MSL times. This allows TCP to resend the last ACK. In case the last ACK is lost, the FIN is re-transmitted. After 2MSL waits for completion, the socket enters the CLOSED state. Passive close: when the program receives a FIN packet from the peer and returns an ACK packet, the socket of the program is transferred to the CLOSE_WAIT status. Because the peer has been disabled, no messages can be sent. But the program can. To close the connection, the program has been sent to its own FIN to change the TCP socket state of the program to the LAST_ACK state. When the program receives the ACK packet from the peer, the program enters the CLOSED state. |
Connect
Int connect (SOCKET s, const struct sockaddr FAR * name, int namelen );
The second parameter is your connection name.
The third parameter is the length of the name parameter you added.
If the connection fails, the system returns the WSAECONNREFUSED error.
Send and WSASend
Int send (SOCKET s, const char FAR * buf, int len, int flags );
The second parameter is the data to be sent.
The third parameter is the length of the sent data.
The fourth parameter can be 0, MSG_DONTROUTE, or MSG_OOB. These parameters can be connected by or.
Normal Return: Sent bytes.
Int WSASend (
SOCKET s,
LPWSABUF lpBuffers,
DWORD dwBufferCount,
LPDWORD lpNumberOfBytesSent,
DWORD dwFlags,
LPWSAOVERLAPPED lpOverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
The last two parameters are used for overlapping I/O. Overlapping I/O is an asynchronous I/O model.
WSASendDisconnect
Int WSASendDisconnect (SOCKET s, LPWSABUF lpOutboundDisconnectData );