wsprintf(status, "HttpProxy: 代理服务器正常运行中..."); ParseStatus(ProxyDlg, status); i = 0; /// fd_set fds; TIMEVAL timeout; int nError; ///////////////////////////////////// wsprintf(status,"HttpProxy: wait request..."); ParseStatus(ProxyDlg, status); //wait request from client do { //time out timeout.tv_sec = 2; timeout.tv_usec = 0; // we use select to avoid blocking on accept forever. // it FD_ZERO(&fds); FD_SET(MainSocket, &fds); //MainSocket+1 ??? nError = select(MainSocket, &fds, NULL, NULL, &timeout); if(nError==WSAEINTR) { ParseError(ProxyDlg, "WSAEINTR"); continue; //interrupted: no harm done } else if(nError<0) {ParseError(ProxyDlg, "nError"); return 0; //error } if(FD_ISSET(MainSocket, &fds)==0) { //ParseError(ProxyDlg, "HttpProxy: timeout"); continue; } //accept it ClientSocket = accept(MainSocket, (SOCKADDR *)&Client, &AddLen); if(ClientSocket == SOCKET_ERROR) { ParseError(ProxyDlg, "HttpProxy: 接受客户请求错误"); continue; } wsprintf(status,"beginthread..."); ParseStatus(ProxyDlg, status); i ++ ; if( i >= 1000) i = 0; Global[i] = ClientSocket;
//set the thread parameter memset(&threadRequest, 0, sizeof(threadRequest)); threadRequest.ProxyDlg = threadProxy->ProxyDlg; threadRequest.pSocket = (void*)&Global[i]; //all ready, we create thread to process the request of the client //you can use it to instead your while loop AfxBeginThread(HttpRequest, &threadRequest); // ::Sleep(100); }while(threadProxy->bInThread); wsprintf(status, "CloseSocket"); ParseStatus(ProxyDlg, status); closesocket(MainSocket); return 0;
ret=recv(aSocket[SocketCount-1],buff,128,0);
所在的循环未结束前,是无法执行
aSocket[SocketCount]=accept(m_hSocket,NULL,NULL);的
若这个循环执行时间过长,结果如何,不难想象另:....//create and bind m_hSocket
ret=listen(m_hSocket,2);语句放在循环外就行了吧
否则的话从第二次开始bind(),listen()返回的都是失败
希望有高人站出来,指点一二。
贴出来仅供参考:)UINT HttpProxy(LPVOID pParam)
{
.................//define the member and accept the pParam
//go.... //create
MainSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(MainSocket == SOCKET_ERROR)
{
ParseError(ProxyDlg, "端口创建错误");
return -2;
}
Host.sin_family = AF_INET;
Host.sin_port = htons(port);
Host.sin_addr.s_addr = inet_addr(addr);
//bind
if(bind(MainSocket, (SOCKADDR *)&Host, sizeof(Host)) != 0)
{
ParseError(ProxyDlg, "HttpProxy: 绑定错误");
return -3;
}
//listen
if(listen(MainSocket,5) == SOCKET_ERROR)
{
ParseError(ProxyDlg, "HttpProxy: 监听错误");
return -4;
}
AddLen = sizeof(Client);
wsprintf(status, "HttpProxy: 代理服务器正常运行中...");
ParseStatus(ProxyDlg, status);
i = 0;
///
fd_set fds;
TIMEVAL timeout;
int nError;
/////////////////////////////////////
wsprintf(status,"HttpProxy: wait request...");
ParseStatus(ProxyDlg, status);
//wait request from client
do
{
//time out
timeout.tv_sec = 2;
timeout.tv_usec = 0;
// we use select to avoid blocking on accept forever.
// it
FD_ZERO(&fds);
FD_SET(MainSocket, &fds); //MainSocket+1 ???
nError = select(MainSocket, &fds, NULL, NULL, &timeout);
if(nError==WSAEINTR)
{
ParseError(ProxyDlg, "WSAEINTR");
continue; //interrupted: no harm done
}
else if(nError<0)
{ParseError(ProxyDlg, "nError");
return 0; //error
}
if(FD_ISSET(MainSocket, &fds)==0)
{
//ParseError(ProxyDlg, "HttpProxy: timeout");
continue;
}
//accept it
ClientSocket = accept(MainSocket, (SOCKADDR *)&Client, &AddLen); if(ClientSocket == SOCKET_ERROR)
{
ParseError(ProxyDlg, "HttpProxy: 接受客户请求错误");
continue;
}
wsprintf(status,"beginthread...");
ParseStatus(ProxyDlg, status);
i ++ ;
if( i >= 1000)
i = 0;
Global[i] = ClientSocket;
//set the thread parameter
memset(&threadRequest, 0, sizeof(threadRequest)); threadRequest.ProxyDlg = threadProxy->ProxyDlg;
threadRequest.pSocket = (void*)&Global[i];
//all ready, we create thread to process the request of the client
//you can use it to instead your while loop
AfxBeginThread(HttpRequest, &threadRequest);
// ::Sleep(100);
}while(threadProxy->bInThread); wsprintf(status, "CloseSocket");
ParseStatus(ProxyDlg, status);
closesocket(MainSocket);
return 0;
}
my email:[email protected]
我是比较笨的说,可是我还是不明白:你怎样控制global[i+1]在global[i]后处理的?
这个顺序这么重要啊
你都说了“后一个请求的数据接收与否不需要考虑前一个请求的数据是不是接收完”:)
global是个全局的SOCKET型数组