猿问

请问在网络编程中select函数如何接受一个accept事件?

网络编程中select函数如何接受一个accept事件


一只甜甜圈
浏览 580回答 3
3回答

慕无忌1623718

#include <winsock.h>#include <stdio.h>#define PORT &nbsp; &nbsp; &nbsp; 5150 //端口#define MSGSIZE &nbsp; &nbsp;1024 //信息大小#pragma comment(lib, "ws2_32.lib")int &nbsp; &nbsp;g_iTotalConn = 0; //连接数量SOCKET g_CliSocketArr[FD_SETSIZE]; //套接字数组DWORD WINAPI WorkerThread(LPVOID lpParameter);//线程函数int main(){&nbsp; WSADATA &nbsp; &nbsp; wsaData;&nbsp; SOCKET &nbsp; &nbsp; &nbsp;sListen, sClient;&nbsp;&nbsp;SOCKADDR_IN&nbsp;local, client;&nbsp; int &nbsp; &nbsp; &nbsp; &nbsp; iaddrSize = sizeof(SOCKADDR_IN);&nbsp; DWORD &nbsp; &nbsp; &nbsp; dwThreadId;&nbsp; // Initialize Windows socket library&nbsp; //装载套接字库&nbsp; WSAStartup(0x0202, &wsaData);&nbsp; // Create listening socket&nbsp; //创建套接字&nbsp; sListen = socket(AF_INET,&nbsp;SOCK_STREAM,&nbsp;IPPROTO_TCP);&nbsp; // Bind&nbsp; //绑定&nbsp; local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);&nbsp; local.sin_family =&nbsp;AF_INET;&nbsp; local.sin_port = htons(PORT);&nbsp; bind(sListen, (struct sockaddr *)&local, sizeof(SOCKADDR_IN));&nbsp; // Listen&nbsp; //监听&nbsp; listen(sListen, 3);&nbsp; // Create worker thread&nbsp; //创建线程&nbsp; CreateThread(NULL, 0, WorkerThread, NULL, 0, &dwThreadId);&nbsp;&nbsp;&nbsp; while (TRUE)&nbsp; {&nbsp; &nbsp; // Accept a connection&nbsp; &nbsp; //接受一个连接,返回的是客户套的套接字&nbsp; &nbsp; sClient = accept(sListen, (struct sockaddr *)&client, &iaddrSize);&nbsp; &nbsp; //这里client的SOCKADDR_IN &nbsp;client 中可以取出IP&nbsp; &nbsp; printf("Accepted client:%s:%d\n",&nbsp;inet_ntoa(client.sin_addr), ntohs(client.sin_port));&nbsp; &nbsp; // Add socket to g_CliSocketArr&nbsp; &nbsp; //把客户端套接字放入数组中&nbsp; &nbsp; g_CliSocketArr[g_iTotalConn++] = sClient;&nbsp; }&nbsp;&nbsp;&nbsp; return 0;}DWORD WINAPI WorkerThread(LPVOID lpParam)//线程{&nbsp; int &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i;&nbsp;&nbsp;fd_set&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; fdread;//结构&nbsp; int &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ret;&nbsp; struct timeval tv = {1, 0};//超时时间 SELECT模型中用到的这里是1秒&nbsp; char &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; szMessage[MSGSIZE];//信息数组,事实上就是个缓冲区&nbsp;&nbsp;&nbsp; while (TRUE)&nbsp; {&nbsp; &nbsp;&nbsp; &nbsp; FD_ZERO(&fdread);//清空fd_set结构&nbsp; &nbsp; for (i = 0; i < g_iTotalConn; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; FD_SET(g_CliSocketArr[i], &fdread);//把客户套接字放到SELECT要求的数组中&nbsp; &nbsp; }&nbsp; &nbsp; //**************************************&nbsp; &nbsp; // We only care read event&nbsp; &nbsp; //只关心读的情况&nbsp; &nbsp; ret = select(0, &fdread, NULL, NULL, &tv);&nbsp; &nbsp; if (ret == 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; // Time expired 超时&nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }&nbsp; &nbsp; //如果SELECT返回不是0&nbsp; &nbsp; for (i = 0; i < g_iTotalConn; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if (FD_ISSET(g_CliSocketArr[i], &fdread))&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // A read event happened on g_CliSocketArr[i]&nbsp; &nbsp; &nbsp; &nbsp; //一个可读发生在这个套接字上&nbsp; &nbsp; &nbsp; &nbsp; ret = recv(g_CliSocketArr[i], szMessage, MSGSIZE, 0);&nbsp; &nbsp; &nbsp; &nbsp; //把它读出到缓冲区&nbsp; &nbsp; &nbsp; if (ret == 0 || (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET))&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; // Client socket closed&nbsp; &nbsp; &nbsp; //客户端关闭&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Client socket %d closed.\n", g_CliSocketArr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;closesocket(g_CliSocketArr[i]);//关闭这个套接字&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (i < g_iTotalConn - 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //将数组中最后一个套接字挪到当前的位置上 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_CliSocketArr[i--] = g_CliSocketArr[--g_iTotalConn];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;// We received a message from client&nbsp; &nbsp; &nbsp; &nbsp;//如果以上没发生,那么就接收到一个客户端的信息&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; szMessage[ret] = '\0';&nbsp; &nbsp; &nbsp; &nbsp; //直接回送回去&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; send(g_CliSocketArr[i], szMessage, strlen(szMessage), 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}&nbsp; }&nbsp;&nbsp;&nbsp; return 0;}1

四季花海

怎么在VS2005里面调试socket函数一个VS2005 无法调试C++的问题现象:在vs2005 中,建立一个空项目,添加一个c++文件main.cpp#include iostreamusing namespace std;int main(){cout hello ;}此时按f5 启动调试,即会提示项目无法调试:无法找到“xxx.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。搜了一些资料,都说建立项目的时候不应建立空项目,而应当建立一个console application。人写到。 其实问题在于,在空项目中不生成调试文件pdb,所以无法调试。要让项目生成pdb 文件,需要更改:项目属性,configuration properties- linker- Generate Debug Info 从 no 改为 yes但这样还是不够的,还需要更改:
随时随地看视频慕课网APP
我要回答