//服务端
/*
。在程序清单7-8 中,我们向大家展示了如何构建一个简单
的服务器应用,令其采用前述的方法,通过完成例程,来实现对一个套接字请求的管理。该程序的编
码主要按下述步骤进行:
① 新建一个套接字,开始在指定端口上,监听一个进入的连接。
② 接受一个进入的连接请求。
③ 为接受的套接字创建一个WSAOVERLAPPED 结构。
④ 在套接字上投递一个异步WSARecv 请求,方法是将WSAOVERLAPPED 指定成为参数,同时提供
一个完成例程。
⑤ 在将fAlertable 参数设为TRUE 的前提下,调用WSAWaitForMultipleEvents,并等待一个重
叠I/O 请求完成。重叠请求完成后,完成例程会自动执行,而且WSAWaitForMultipleEvents 会返回一
个WSA_IO_COMPLETION。在完成例程内,可随一个完成例程一道,投递另一个重叠WSARecv 请求。
⑥ 检查WSAWaitForMultipleEvents 是否返回WSA_IO_COMPLETION。
⑦ 重复第⑤步和第⑥步。
程序清单7-8 采用完成例程的简单重叠I/O 处理示例
// 说明:该例介绍了如何使用重叠I/O 模型开发简单的回显服务器应用,该例也是简单的控制
台程序,
*/
// 它会在连接从服务器建立和移除时打印消息。该应用会在5150 端口监听和接收进入的TCP 连
//接,
// 收到客户端发送的数据时,会把收到的数据按照原来的格式发送回去
// 编译:cl -o overlap overlap.cpp ws2_32.lib
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#define PORT 5150
#define DATA_BUFSIZE 8192
typedef struct _SOCKET_INFORMATION {
CHAR Buffer[DATA_BUFSIZE];
WSABUF DataBuf;
SOCKET Socket;
WSAOVERLAPPED Overlapped;
DWORD BytesSEND;
DWORD BytesRECV;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
DWORD EventTotal = 0; //事件对象总数
DWORD WINAPI ProcessIO(LPVOID lpParameter);
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
LPSOCKET_INFORMATION SocketArray[WSA_MAXIMUM_WAIT_EVENTS];
CRITICAL_SECTION CriticalSection;
int main(void)
{
WSADATA wsaData;
SOCKET ListenSocket, AcceptSocket;
SOCKADDR_IN InternetAddr;
DWORD Flags;
DWORD ThreadId;
DWORD RecvBytes;
INT Ret;
InitializeCriticalSection(&CriticalSection); //初始化临界区
printf("主函数开始执行\n") ;
if ((Ret = WSAStartup(0x0202,&wsaData)) != 0)
{
printf("WSAStartup failed with error %d\n", Ret);
WSACleanup();
return 0 ;
}
if ((ListenSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0,
WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) //创建监听端口
{
printf("Failed to get a socket %d\n", WSAGetLastError());
return 0 ;
}
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);
if (bind(ListenSocket, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr)) ==
SOCKET_ERROR) //绑定端口
{
printf("bind() failed with error %d\n", WSAGetLastError());
return 0 ;
}
if (listen(ListenSocket, 5))
{
printf("listen() failed with error %d\n", WSAGetLastError());
return 0 ;
}
// 建立连接的监听套接字
if ((AcceptSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0,
WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) //这里创建了一个异步的
{
printf("Failed to get a socket %d\n", WSAGetLastError());
return 0 ;
}
if ((EventArray[0] = WSACreateEvent()) == WSA_INVALID_EVENT) //第一个事件对象
{
printf("WSACreateEvent failed with error %d\n", WSAGetLastError());
return 0 ;
}
// 创建服务重叠请求的线程
printf("创建线程\n") ;
if (CreateThread(NULL, 0, ProcessIO, NULL, 0, &ThreadId) == NULL) //为什么不叫ThreadIO,也叫ProcessIO咧?
{
printf("CreateThread failed with error %d\n", GetLastError());
return 0 ;
}
EventTotal = 1; //突然间冒出来
int i = 0 ;
while(TRUE)
{
// 接收进入的连接
printf("Main第%d次循环\n",i+1) ;
i++ ;
printf("等待连接,兼进入临界区\n") ;
if ((AcceptSocket = accept(ListenSocket, NULL, NULL)) == INVALID_SOCKET)
{
printf("accept failed with error %d\n", WSAGetLastError());
return 0 ;
}
EnterCriticalSection(&CriticalSection); //临界区??
// 创建同接收的套接字相关的套接字信息结构体
if ((SocketArray[EventTotal] = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR,
sizeof(SOCKET_INFORMATION))) == NULL)
{
printf("GlobalAlloc() failed with error %d\n", GetLastError());
return 0 ;
}
// 填充接收套接字的细节
SocketArray[EventTotal]->Socket = AcceptSocket; //新套接字的?
ZeroMemory(&(SocketArray[EventTotal]->Overlapped), sizeof(OVERLAPPED));
SocketArray[EventTotal]->BytesSEND = 0;
SocketArray[EventTotal]->BytesRECV = 0;
SocketArray[EventTotal]->DataBuf.len = DATA_BUFSIZE;
SocketArray[EventTotal]->DataBuf.buf = SocketArray[EventTotal]->Buffer;
if ((SocketArray[EventTotal]->Overlapped.hEvent = EventArray[EventTotal] =
WSACreateEvent()) == WSA_INVALID_EVENT) //将第几个事件对象赋给
{
printf("WSACreateEvent() failed with error %d\n", WSAGetLastError());
return 0;
}
// 发布WSARecv 请求开始在套接字上接收数据
Flags = 0;
if (WSARecv(SocketArray[EventTotal]->Socket,
&(SocketArray[EventTotal]->DataBuf), 1, &RecvBytes, &Flags,
&(SocketArray[EventTotal]->Overlapped), NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSARecv() failed with error %d\n", WSAGetLastError());
return 0;
}
}
EventTotal++;
printf("离开临界区\n") ;
LeaveCriticalSection(&CriticalSection);
// 信号通知事件数组中的第一个事件,让工作线程服务事件数组中的其它事件
if (WSASetEvent(EventArray[0]) == FALSE)
{
printf("WSASetEvent failed with error %d\n", WSAGetLastError());
return 0 ;
}
}//while
return 0 ;
}
DWORD WINAPI ProcessIO(LPVOID lpParameter)
{
DWORD Index;
DWORD Flags;
LPSOCKET_INFORMATION SI;
DWORD BytesTransferred;
DWORD i;
DWORD RecvBytes, SendBytes;
printf("进入ProcessIO\n") ;
int j = 0 ;
// 处理异步WSASend、WSARecv 请求
while(TRUE)
{
printf("ProcessIO第%d次循环\n",j+1) ;
j++ ;
if ((Index = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE,
WSA_INFINITE, FALSE)) == WSA_WAIT_FAILED)
{
printf("WSAWaitForMultipleEvents failed %d\n", WSAGetLastError());
return 0;
}
// 如果触发的事件为零,然后会在我们的监听套接字上进行连接尝试
if ((Index - WSA_WAIT_EVENT_0) == 0) //如果是0的话,就直接跳到下一个循环?什么回事?
{
WSAResetEvent(EventArray[0]);
continue;
}
SI = SocketArray[Index - WSA_WAIT_EVENT_0];
WSAResetEvent(EventArray[Index - WSA_WAIT_EVENT_0]);
if (WSAGetOverlappedResult(SI->Socket, &(SI->Overlapped), &BytesTransferred,
FALSE, &Flags) == FALSE || BytesTransferred == 0)
{
printf("Closing socket %d\n", SI->Socket);
if (closesocket(SI->Socket) == SOCKET_ERROR)
{
printf("closesocket() failed with error %d\n", WSAGetLastError());
}
GlobalFree(SI);
WSACloseEvent(EventArray[Index - WSA_WAIT_EVENT_0]);
// 通过删除事件句柄和套接字信息结构体来清除SocketArray 和EventArray
EnterCriticalSection(&CriticalSection); //临界区。。
if ((Index - WSA_WAIT_EVENT_0) + 1 != EventTotal)
for (i = Index - WSA_WAIT_EVENT_0; i < EventTotal; i++)
{
EventArray[i] = EventArray[i + 1];
SocketArray[i] = SocketArray[i + 1];
}
EventTotal--;
LeaveCriticalSection(&CriticalSection); //离开临界区
continue;
}
// 查看BytesRECV 域是否为0,然后这意味着WSARecv 调用刚刚完成,因此可以用从已完
//成
// 的WSARecv 调用中获得的BytesTransferred 值来更新 BytesRECV 域
if (SI->BytesRECV == 0)
{
SI->BytesRECV = BytesTransferred;
SI->BytesSEND = 0;
}
else
{
SI->BytesSEND += BytesTransferred;
}
if (SI->BytesRECV > SI->BytesSEND)
{
// 发布另一个WSASend()请求,因为WSASend()不能确保发送所以请求的字节,继续
//发布
// WSASend()调用直到所以收到的字节被发送
ZeroMemory(&(SI->Overlapped), sizeof(WSAOVERLAPPED));
SI->Overlapped.hEvent = EventArray[Index - WSA_WAIT_EVENT_0];
SI->DataBuf.buf = SI->Buffer + SI->BytesSEND;
SI->DataBuf.len = SI->BytesRECV - SI->BytesSEND;
if (WSASend(SI->Socket, &(SI->DataBuf), 1, &SendBytes, 0,
&(SI->Overlapped), NULL) == SOCKET_ERROR) //这里发出去
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSASend() failed with error %d\n", WSAGetLastError());
return 0;
}
}
}
else
{
SI->BytesRECV = 0;
// 现在不在需要发布WSARecv 请求来发送其余的数据
Flags = 0;
ZeroMemory(&(SI->Overlapped), sizeof(WSAOVERLAPPED));
SI->Overlapped.hEvent = EventArray[Index - WSA_WAIT_EVENT_0];
SI->DataBuf.len = DATA_BUFSIZE;
SI->DataBuf.buf = SI->Buffer;
if (WSARecv(SI->Socket, &(SI->DataBuf), 1, &RecvBytes, &Flags,
&(SI->Overlapped), NULL) == SOCKET_ERROR) //接收事件
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSARecv() failed with error %d\n", WSAGetLastError());
return 0;
}
}
else
{
// printf("接收: %s",SI->DataBuf ) ; 一次是接收不完的
}
}
}
printf("离开ProcessIO\n") ;
}
//客户端
/*
程序清单6-2 是客户端代码,客户端建立一个套接字,并对投入应用的服务器名进行解析,然后
与服务器建立连接。连接一旦建成,就可发送大量的消息了。每次发送数据之后,客户端都会等待服
务器发回的回应。客户端把得自套接字的数据打印出来。
回应客户端和服务器不能完全说明TCP 协议的流式传输。这是因为读取操作是在写操作之后进行
的,至少客户端这一端是这样的。当然,对服务器来说,还有另一种方式。因此,服务器每次调用读
取函数,一般都会返回客户端发出的整条消息。但不要误会,如果客户端的消息大到超过了TCP 的最
大传输单元,在线上,它会被分成几个小的数据包,这种情况下,接收端需要多次执行接收调用,才
能收完整条消息。为了更好地说明流式传输,运行客户端和服务器时带上-O 选项即可。这样,客户端
便只管发送数据,接收端只管读取数据。
服务器如下执行:
server -p:5150 -o
而客户端如下执行:
client -p:5150 -s:IP -n:10 -o
大家最可能见到的是客户端进行了10 次send 调用,而服务器在一次或两次recv 调用中,就读
取了10 条消息。
*/
//程序清单6-2 回应客户端代码
// 说明:回显客户端,连接TCP 服务器,发送数据,并且读服务器返回的数据
// 编译命令:cl -o Client Client.c ws2_32.lib
//
// 命令行参数:
// client [-p:x] [-s:IP] [-n:x] [-o]
// -p:x 发送的远程端口
// -s:IP 服务器IP 地址或主机名
// -n:x 发送消息次数
// -o 只发送消息,不接收
//
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "Ws2_32.lib ")
#define DEFAULT_COUNT 10
#define DEFAULT_PORT 5150
#define DEFAULT_BUFFER 2048
#define DEFAULT_MESSAGE "This is a test of the emergency \
broadcasting system"
char szServer[128], // 连接的服务器
szMessage[1024]; // 发送给服务器的消息
int iPort = DEFAULT_PORT; // 连接到服务器的端口
DWORD dwCount = DEFAULT_COUNT; // 发送消息次数
BOOL bSendOnly = FALSE; // 只发送数据,不接收
void usage() ;
void ValidateArgs(int argc, char **argv) ;
// 函数:main
// 说明:执行主线程,初始化Winsock,解释命令行参数,创建套接字,连接服务器,然后发送
//和接
// 收数据
int main(int argc, char **argv)
{
WSADATA wsd;
SOCKET sClient;
char szBuffer[DEFAULT_BUFFER];
int ret,i;
struct sockaddr_in server;
struct hostent *host = NULL;
// 解释命令行并且载入Winsock
ValidateArgs(argc, argv);
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("Failed to load Winsock library!\n");
return 1;
}
strcpy(szMessage, DEFAULT_MESSAGE);
// 创建套接字,并且尝试连接服务器
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == INVALID_SOCKET)
{
printf("socket() failed: %d\n", WSAGetLastError());
return 1;
}
server.sin_family = AF_INET;
server.sin_port = htons(iPort);
server.sin_addr.s_addr = inet_addr(szServer);
// 如果提供的服务器地址不是形如"aaa.bbb.ccc.ddd",则为主机名,尝试解析它
if (server.sin_addr.s_addr == INADDR_NONE)
{
host = gethostbyname(szServer);
if (host == NULL)
{
printf("Unable to resolve server: %s\n", szServer);
return 1;
}
CopyMemory(&server.sin_addr, host->h_addr_list[0],
host->h_length);
}
if (connect(sClient, (struct sockaddr *)&server,sizeof(server)) == SOCKET_ERROR)
{
printf("connect() failed: %d\n", WSAGetLastError());
return 1;
}
// 发送和接收数据
for(i = 0; i < dwCount; i++)
{
ret = send(sClient, szMessage, strlen(szMessage), 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
printf("send() failed: %d\n", WSAGetLastError());
break;
}
printf("Send %d bytes\n", ret);
if (!bSendOnly)
{
ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0);
if (ret == 0) // Graceful close
break;
else if (ret == SOCKET_ERROR)
{
printf("recv() failed: %d\n", WSAGetLastError());
break;
}
szBuffer[ret] = '\0';
printf("RECV [%d bytes]: '%s'\n", ret, szBuffer);
}
}
closesocket(sClient);
WSACleanup();
return 0;
}
void usage()
{
printf("usage: client [-p:x] [-s:IP] [-n:x] [-o]\n\n");
printf(" -p:x Remote port to send to\n");
printf(" -s:IP Server's IP address or hostname\n");
printf(" -n:x Number of times to send message\n");
printf(" -o Send messages only; don't receive\n");
ExitProcess(1);
}
// 函数:ValidateArgs
// 说明:解释命令行参数,设置全局变量
void ValidateArgs(int argc, char **argv)
{
int i;
for(i = 1; i < argc; i++)
{
if ((argv[i][0] == '-') || (argv[i][0] == '/'))
{
switch (tolower(argv[i][1]))
{
case 'p': // Remote port
if (strlen(argv[i]) > 3)
iPort = atoi(&argv[i][3]);
break;
case 's': // Server
if (strlen(argv[i]) > 3)
strcpy(szServer, &argv[i][3]);
break;
case 'n': // Number of times to send message
if (strlen(argv[i]) > 3)
dwCount = atol(&argv[i][3]);
break;
case 'o': // Only send message; don't receive
bSendOnly = TRUE;
break;
default:
usage();
break;
}
}
}
}