您好所有我一直在努力,从一个简单的客户端接收这两个地址,然后使用异步Socket和WINAPI打印出来一个服务器上,到目前为止,我已经设置了窗口,并已能够接受连接,但是当我尝试向服务器发送两条消息时,它只收到一条消息,第二条消息失败。下面是代码:recv函数失败
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hEdit = NULL;
int len = sizeof(Server);
switch (msg)
{
case WM_CREATE:
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, WIDTH, HEIGHT, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case 1111:
if (LOWORD(lParam) == FD_ACCEPT)
{
socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
socket = accept(sListen, (SOCKADDR*)&Server, &len);
print_line(hwnd, "IT WAS ACCEPTED!!!!!!!!\r\n");
}
if (LOWORD(lParam) == FD_CLOSE)
{
print_line(hwnd, "Client left the server!\r\n");
}
if (LOWORD(lParam) == FD_READ)
{
char NICK[4096] = { 0 };
char IP[4096] = { 0 };
ZeroMemory(NICK, strlen(NICK));
ZeroMemory(IP, strlen(IP));
if (recv(socket, IP, sizeof(IP), NULL) == INVALID_SOCKET)//get the IP address
{
print_line(hwnd, "Failed to recieve IP Address from socket!");
print_line(hwnd, "\r\n");
}
if (recv(socket, NICK, sizeof(NICK), NULL) == INVALID_SOCKET)//get the Nickname
{
print_line(hwnd, "Failed to recieve nickname from socket!");
print_line(hwnd, "\r\n");
}
//prints the Username and IP address to the window screen
print_line(hwnd, "Username: ");
print_line(hwnd, NICK);
print_line(hwnd, "\r\n");
print_line(hwnd, "IP Address: ");
print_line(hwnd, IP);
print_line(hwnd, "\r\n");
}
break;
default:
HWND hEdit;
RECT rcClient;
GetClientRect(hwnd, &rcClient);
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
}
检查第二个呼叫之前的错误,并张贴结果 – 2014-09-26 22:48:02
@MarcoA。你的意思是第一个recv函数调用?如果这是你的意思,我已经检查过,并没有错误,它打印的IP地址 – user3097544 2014-09-26 22:49:29
对不起,我的意思是:检查第二次通话后的错误,所以你可以看到更详细的错误 – 2014-09-26 22:55:19