MSDN winsocket 教程:https://docs.microsoft.com/zh-cn/windows/win32/api/_winsock/
TCP通信原理:
服务端代码及注释
```cpp
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
#define DEFAULT_BUFLEN 512//5.1
#define DEFAULT_PORT "27015"//2.1getaddrinfo
int __cdecl main(void)
{
WSADATA wsaData;//1.1创建一个名为wsaData 的WSADATA对象。wsaData(使用版本,最高版本....struct结构体)
//调用WSAStartup并将其值返回为整数并检查错误
int iResult;//1.1
SOCKET ListenSocket = INVALID_SOCKET;//2.2监听invalid(病人) 为服务器创建一个名为ListenSocket 的SOCKET对象,以侦听客户端连接
SOCKET ClientSocket = INVALID_SOCKET;//5.1创建一个名为ClientSocket 的临时SOCKET对象,以接受来自客户端的连接
struct addrinfo *result = NULL;//2.1
struct addrinfo hints;//2.1定义一个名为hints的addrinfo结构体:在hints参数中指定的地址系列,套接字类型和协议
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// 1.2初始化 Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);//WSAStartup函数启动使用WS2_32.DLL,Winsock的2.2版
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));//2.1获取addrinfo函数 请求了IPv4的TCP流套接字
hints.ai_family = AF_INET;//地址族当前支持的值为AF_INET或AF_INET6,它们是IPv4和IPv6的Internet地址族格式
hints.ai_socktype = SOCK_STREAM;//stream 提供带OOB数据传输机制的顺序,可靠,双向,基于连接的字节流。
hints.ai_protocol = IPPROTO_TCP;//传输控制协议(TCP)这是一个可能的值时ai_family构件是AF_INET或AF_INET6和ai_socktype构件是SOCK_STREAM。
hints.ai_flags = AI_PASSIVE;//套接字地址将在对bind函数的调用中使用。
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); //(NODE,port,hints指针,result指针)
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);//获得addrinfo协议失败
WSACleanup();
return 1;
}
//以上2.1
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);//调用套接字函数,并将其值返回到ListenSocket变量
if (ListenSocket == INVALID_SOCKET)//2.4检查是否有错误,以确保该套接字是有效的套接字 INVALID_SOCKET为一个嵌套字对象
{
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// 3.1绑定嵌套字
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());//如果没有错误发生,则 绑定返回零
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);//3.2释放该分配的内存
//4.1监听嵌套字
iResult = listen(ListenSocket, SOMAXCONN);//backlog参数设置为SOMAXCONN,允许在队列中允许最大合理数量的挂起连接
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// 接受连接 单线程连接,如何进行多线程连接呢,listen()函数要进行循环
ClientSocket = accept(ListenSocket, NULL, NULL);//如果没有发生错误, accept将返回SOCKET类型的值
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
else
wprintf(L"Client connected.\n");
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection 5.接收和发送数据
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);//接收数据
if (iResult > 0) {//判断数据接收
printf("Bytes received: %d\n", iResult);//数据接收
// Echo the buffer back to the sender
iSendResult = send(ClientSocket, recvbuf, iResult, 0);//发送数据
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iResult == 0)
printf("Connection closing...\n");//正在连接
else {
printf("recv failed with error: %d\n", WSAGetLastError());//连接失败
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the connection since we're done 6.断开服务器
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);//关闭socket
WSACleanup();//WSACleanup函数终止使用Winsock 2 DLL(的WS2_32.DLL)
system("pause");
return 0;
}
客户端代码如下
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;//1.1创建嵌套字
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
const char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Validate the parameters验证参数
if (argc != 2) {
printf("usage: %s server-name\n", argv[0]);
return 1;
}
// Initialize Winsock初始化winsocket
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);//iresult返回的值是多少0
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
//1.1获取addrinfo函数 请求了IPv4的TCP流套接字
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);//和hints变量长度和地址有关IPV4
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
// Send an initial buffer发送初始缓冲区
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while (iResult > 0);
// cleanup
closesocket(ConnectSocket);
WSACleanup();
system("pause");
return 0;
}
运行过程:
先运行server01,用cmd命令运行client01.exe 127.0.0.1