我正在用客户端和服务器编写一个小应用程序 - 客户端发送问题并且服务器回答。
我成功完成了第一部分 - 服务器从客户端获取问题,做一些工作并发回答案。我无法弄清楚如何告诉客户端等待来自服务器的响应。
这是我的客户代码:
char* ipAddress = (char*)malloc(15);
wcstombs(ipAddress, (TCHAR*)argv[1], 15);
DWORD port = wcstod(argv[2], _T('\0'));
DWORD numOfThreads = wcstod(argv[3], _T('\0;'));
DWORD method = wcstod(argv[4], _T('\0;'));
//initialize windows sockets service
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
assert(iResult==NO_ERROR);
//prepare server address
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ipAddress);
server_addr.sin_port = htons(port);
//create socket
SOCKET hClientSocket= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
assert(hClientSocket!=INVALID_SOCKET);
//connect to server
int nRes=connect(hClientSocket, (SOCKADDR*)&server_addr, sizeof(server_addr));
assert(nRes!=SOCKET_ERROR);
char* buf = "GET /count.htm HTTP/1.1\r\nHost: 127.0.0.1:666\r\nAccept: text/html,application/xhtml+xml\r\nAccept-Language: en-us\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/5.0\r\n\r\n";
int nBytesToSend= strlen(buf);
int iPos=0;
while(nBytesToSend)
{
int nSent=send(hClientSocket,buf,nBytesToSend,0);
assert(nSent!=SOCKET_ERROR);
nBytesToSend-=nSent;
iPos+=nSent;
}
closesocket(hClientSocket);
int nLen = sizeof(server_addr);
SOCKET hRecvSocket=accept(hClientSocket,(SOCKADDR*)&server_addr, &nLen);
assert(hRecvSocket!=INVALID_SOCKET);
//prepare buffer for incoming data
char serverBuff[256];
int nLeft=sizeof(serverBuff);
iPos=0;
do //loop till there are no more data
{
int nNumBytes=recv(hRecvSocket,serverBuff+iPos,nLeft,0);
//check if cleint closed connection
if(!nNumBytes)
break;
assert(nNumBytes!=SOCKET_ERROR);
//update free space and pointer to next byte
nLeft-=nNumBytes;
iPos+=nNumBytes;
}while(1);
SOCKET hRecvSocket=accept(hClientSocket,(SOCKADDR*)&server_addr, &nLen);
行之后的断言失败。
答案 0 :(得分:1)
“发送”循环后的closesocket和accept调用 - 删除这些调用。 accept适用于侦听传入连接的服务器,而不适用于已连接的客户端。
send()循环完成后,直接进入recv()循环。这应该可以解决您当前的问题:
另外,你的发送循环忘记按照我的意图在缓冲区上引用iPos。这就是你想要的:
int nSent=send(hClientSocket,buf+iPos,nBytesToSend,0);
在网络编程中,由于网络条件超出您的控制范围,套接字将失败。因此,网络呼叫上的“断言”并不总是合适的。最好只是期待失败并准备好处理它。通常,关闭套接字和活动连接是处理大多数错误的方法。