纯C手写套接字select模型服务器端
#include<WinSock2.h>
#include<stdio.h>
#include<iostream>
#include<string>
#include<stdlib.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
fd_set everyclient;
BOOL WINAPI fun(DWORD dwCtrlType)
{
switch (dwCtrlType)
{
case CTRL_CLOSE_EVENT:
for (u_int i = 0; i < everyclient.fd_count; i++)
{
closesocket(everyclient.fd_array[i]);
}
WSACleanup();
}
return TRUE;
}
int main(void)
{
SetConsoleCtrlHandler(fun,TRUE);
WORD wdVersion = MAKEWORD(2, 2);
WSADATA wdScokMsg;
int nRes = WSAStartup(wdVersion, &wdScokMsg);
if (0 != nRes)
{
switch (nRes)
{
case WSASYSNOTREADY:
printf("重启计算机!");
break;
case WSAEINPROGRESS:
printf("更新网络库!");
break;
}
return 0;
}
SOCKET socketServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == socketServer)
{
int a = WSAGetLastError();
WSACleanup();
return 0;
}
if (2 != HIBYTE(wdScokMsg.wVersion) || LOBYTE(wdScokMsg.wVersion) != 2)
{
WSACleanup();
return 0;
}
struct sockaddr_in si;
si.sin_family = AF_INET;
si.sin_port = htons(12345);
si.sin_addr.S_un.S_addr = inet_addr("0.0.0.0");
int res = bind(socketServer, (const struct sockaddr*) & si, sizeof(si));
if (SOCKET_ERROR == res)
{
int a = WSAGetLastError();
closesocket(socketServer);
WSACleanup();
return 0;
}
if (SOCKET_ERROR == listen(socketServer, SOMAXCONN))
{
int a = WSAGetLastError();
closesocket(socketServer);
WSACleanup();
return 0;
}
struct sockaddr_in clientMsg;
int len = sizeof(clientMsg);
printf("服务器已运行!");
FD_ZERO(&everyclient);
FD_SET(socketServer,&everyclient);
while (1)
{
fd_set tempsocket = everyclient;
fd_set writesocket = everyclient;
FD_CLR(socketServer,&writesocket);
fd_set readsocket = everyclient;
fd_set errorsocket = everyclient;
struct timeval st;
st.tv_sec = 3;
st.tv_usec = 0;
int nRes=select(0,&readsocket,&writesocket,&errorsocket,NULL)
char send_buf[100] = {0};
if (0==nRes)
{
continue;
}
else if (nRes > 0)
{
for (u_int i=0;i< readsocket.fd_count;i++)
{
if (readsocket.fd_array[i]==socketServer)
{
SOCKET socketClient = accept(socketServer,NULL,NULL);
if (SOCKET_ERROR == socketClient)
{
continue;
}
FD_SET(socketClient,&everyclient);
printf("客户端链接成功!\n");
}
else
{
char strbuf[1500] = { 0 };
int nRecv = recv(readsocket.fd_array[i],strbuf,1500,0);
{
if (0==nRecv)
{
SOCKET socketTemp = readsocket.fd_array[i];
FD_CLR(readsocket.fd_array[i],&everyclient);
closesocket(socketTemp);
}
else if (nRecv>0)
{
printf("%s\n",strbuf);
for (u_int i = 0; i < writesocket.fd_count; i++)
{
if (SOCKET_ERROR == send(writesocket.fd_array[i], "ok", sizeof("ok"), 0))
{
int a = WSAGetLastError();
}
}
}
else
{
int a = WSAGetLastError();
switch (a)
{
case 10054:
{
printf("客户端已下线!");
SOCKET socketTemp = readsocket.fd_array[i];
FD_CLR(readsocket.fd_array[i], &everyclient);
closesocket(socketTemp);
}
}
}
}
}
}
for (u_int i = 0;i< errorsocket.fd_count;i++)
{
char str[100] = { 0 };
int len = 99;
if (SOCKET_ERROR==getsockopt(errorsocket.fd_array[i], SOL_SOCKET, SO_ERROR, str, &len))
{
printf("无法得到错误信息!!\n");
}
printf("%s\n",str);
}
}
else
{
int a = WSAGetLastError();
printf("%d\n", a);
}
}
return 0;
system("pause");
}