主题:请教下,为什么我发送了Http请求,服务端没回应呢?
我最近写代理服务器类似的程序,但是我将客户机给我的包处理后,发送到服务器确没有得到客户机的相应,一会儿就会显示"The remote server has closed."
不知道五一还有高手在线帮我看看不?
唉,最近老师崔着要在五一后必须把毕业设计搞定,五一也不能玩了,郁闷中...
部分代码:
... ...
typedef struct CombineSock{
SOCKET sClient;//socket communicate with client.
SOCKET sServer;
SOCKADDR_IN addrClient;//client address.
SOCKADDR_IN addrServer;
BOOL bIsClientClosed;//if the client closed.
BOOL bIsServerClosed;
}COMBINESOCK,FAR *PCOMBINESOCK;
typedef struct Package{
COMBINESOCK csCom;
char szInfo[DEFAULT_BUFFER_SIZE];//the data buffer.
}PACKAGE,FAR *PPACKAGE;
... ...
sCom[i].sClient=accept(g_sListen,(SOCKADDR*)&sCom[i].addrClient,&iAddrSize);
if(sCom[i].sClient==INVALID_SOCKET)
{
printf("Accept connection failed with error:\n");
WSAGetErrorToInfo();
break;
}
//get the client name,and display status information.
... ...
//create a thread to handle the request.
hRecv=CreateThread(NULL,0,RecvClient,(PVOID)&sCom[i],0,NULL);
DWORD WINAPI RecvClient(LPVOID lpParam)
{
PACKAGE ComPack;
... ..
ComPack.csCom=*((COMBINESOCK*)lpParam);
memset(ComPack.szInfo,0,DEFAULT_BUFFER_SIZE);
... ...
while(TRUE)//always accept the client request until encountered errors,
//or client closed connection.
{
retRecvC=recv(ComPack.csCom.sClient,ComPack.szInfo,DEFAULT_BUFFER_SIZE,0);
if(retRecvC==0)//the client closed the connection gracefully.
{
printf("Client %s closed the connection.\n",
szClientName);
ComPack.csCom.bIsClientClosed=TRUE;
break;
}
else if(retRecvC==SOCKET_ERROR)//encountered errors.
{
printf("Receive form %s failed with error:\n",
szClientName);WSAGetErrorToInfo();
ComPack.csCom.bIsClientClosed=TRUE;
break;
}
ComPack.szInfo[retRecvC]='\0';
#ifdef _DEBUG
printf("Received %s:\n%s\n",szClientName,ComPack.szInfo);
#endif
GetAddrAndPort(ComPack.szInfo,szAddrReq,
&ComPack.csCom.addrServer.sin_port);
... ...
if(!GetAddr(szAddrReq,&ComPack.csCom.addrServer.sin_addr.s_addr))
{
printf("The server %s requested by client %s can't reach.\n",
szAddrReq,szClientName);
//send the error message to the client.
... ...
break;
}
HANDLE hSendToServer;
//create thread to fetch the request from remote server.
hSendToServer=CreateThread(NULL,0,ProxyToServer,(LPVOID)&ComPack,
0,NULL);
... ...
}
closesocket(ComPack.csCom.sClient);
return 1;
}
不知道五一还有高手在线帮我看看不?
唉,最近老师崔着要在五一后必须把毕业设计搞定,五一也不能玩了,郁闷中...
部分代码:
... ...
typedef struct CombineSock{
SOCKET sClient;//socket communicate with client.
SOCKET sServer;
SOCKADDR_IN addrClient;//client address.
SOCKADDR_IN addrServer;
BOOL bIsClientClosed;//if the client closed.
BOOL bIsServerClosed;
}COMBINESOCK,FAR *PCOMBINESOCK;
typedef struct Package{
COMBINESOCK csCom;
char szInfo[DEFAULT_BUFFER_SIZE];//the data buffer.
}PACKAGE,FAR *PPACKAGE;
... ...
sCom[i].sClient=accept(g_sListen,(SOCKADDR*)&sCom[i].addrClient,&iAddrSize);
if(sCom[i].sClient==INVALID_SOCKET)
{
printf("Accept connection failed with error:\n");
WSAGetErrorToInfo();
break;
}
//get the client name,and display status information.
... ...
//create a thread to handle the request.
hRecv=CreateThread(NULL,0,RecvClient,(PVOID)&sCom[i],0,NULL);
DWORD WINAPI RecvClient(LPVOID lpParam)
{
PACKAGE ComPack;
... ..
ComPack.csCom=*((COMBINESOCK*)lpParam);
memset(ComPack.szInfo,0,DEFAULT_BUFFER_SIZE);
... ...
while(TRUE)//always accept the client request until encountered errors,
//or client closed connection.
{
retRecvC=recv(ComPack.csCom.sClient,ComPack.szInfo,DEFAULT_BUFFER_SIZE,0);
if(retRecvC==0)//the client closed the connection gracefully.
{
printf("Client %s closed the connection.\n",
szClientName);
ComPack.csCom.bIsClientClosed=TRUE;
break;
}
else if(retRecvC==SOCKET_ERROR)//encountered errors.
{
printf("Receive form %s failed with error:\n",
szClientName);WSAGetErrorToInfo();
ComPack.csCom.bIsClientClosed=TRUE;
break;
}
ComPack.szInfo[retRecvC]='\0';
#ifdef _DEBUG
printf("Received %s:\n%s\n",szClientName,ComPack.szInfo);
#endif
GetAddrAndPort(ComPack.szInfo,szAddrReq,
&ComPack.csCom.addrServer.sin_port);
... ...
if(!GetAddr(szAddrReq,&ComPack.csCom.addrServer.sin_addr.s_addr))
{
printf("The server %s requested by client %s can't reach.\n",
szAddrReq,szClientName);
//send the error message to the client.
... ...
break;
}
HANDLE hSendToServer;
//create thread to fetch the request from remote server.
hSendToServer=CreateThread(NULL,0,ProxyToServer,(LPVOID)&ComPack,
0,NULL);
... ...
}
closesocket(ComPack.csCom.sClient);
return 1;
}