Zero99
January 1st, 2006, 10:44 PM
ok im not sure if its the send() function im doing wrong or the Recv() function... here's my code
-------------------CLIENT SIDE----------------------------
... << Pressing the key F3 to send data in WinMain() code >> ...
int rVal; <- defined at top of program
char buffer[18]; <- also defined at top of program
ZeroMemory(buffer, 18);
strcpy(buffer, "65755556566666666"); <- the data im sending..numbers
rVal = send(theSocket, buffer, strlen(buffer), 0);
rVal = WSAGetLastError();
if (rVal == SOCKET_ERROR) {
sError("SEND()");
return NETWORK_ERROR;
}
^^ the above all goes well, no errors ^^
-----------------------SERVER SIDE-----------------------------
// in my server it accepts the connection from b4 and switches to a new socket that will be able to use FD_READ | FD_CLOSE, this is the process:
WSAAsyncSelect(s, hWnd, WM_USER + 1, FD_ACCEPT);
//goes over to my WndProc() switch(Msg):
...
case WM_USER + 1:
HandleAccept(wParam, lParam); <- handles the connection request
break;
...
int HandleAccept(WPARAM wParam, LPARAM lParam)
{
SOCKET sock = (SOCKET)wParam;
WORD event = LOWORD(lParam);
WORD error = HIWORD(lParam);
if(event == FD_ACCEPT)
{
sockaddr_in NewRemoteSin;
newsock = accept(sock, (sockaddr*)&NewRemoteSin, NULL);
WSAAsyncSelect(newsock, hWnd, WM_USER + 2, FD_READ | FD_CLOSE);
}
return TRUE;
}
// ^^^ my new socket that is going to read data being sent is now called "newsock", the WSAAsyncSelect() is now set to FD_READ | CLOSE
<< blah blah i send the data (the numbers when i press F1 on the client side) >>
<< My WndProc() once again >>>
...
case WM_USER + 2:
HandleData(wParam, lParam);
break;
...
<< Now it calls HandleData as above... >>
<< So now we have HandleData>>
int HandleData(WPARAM wParam, LPARAM lParam)
{
SOCKET sock = (SOCKET)wParam;
WORD event = LOWORD(lParam);
WORD error = HIWORD(lParam);
if(event == FD_CLOSE)
{
closesocket(sock);
}
else if(event == FD_READ)
{
ZeroMemory(buf, 18);
recv(sock, buf, 18, 0);
MessageBox(hWnd, buf, "Received Data!", MB_OK);
//closesocket(s);
}
return TRUE;
}
i think i went into a little too much detail...but i did not know how to explain it, but the problem is that the MessageBox() in the HandleData() function never appears...i have spent like 3 hours, moving things around checking for errors, the only thing i came across was when i put the error check above the line closesocket(sock) in the HandleData() function, it says that the client side has closed... it never goes to the FD_READ case. I have no clue what is wrong. thx in advance
-------------------CLIENT SIDE----------------------------
... << Pressing the key F3 to send data in WinMain() code >> ...
int rVal; <- defined at top of program
char buffer[18]; <- also defined at top of program
ZeroMemory(buffer, 18);
strcpy(buffer, "65755556566666666"); <- the data im sending..numbers
rVal = send(theSocket, buffer, strlen(buffer), 0);
rVal = WSAGetLastError();
if (rVal == SOCKET_ERROR) {
sError("SEND()");
return NETWORK_ERROR;
}
^^ the above all goes well, no errors ^^
-----------------------SERVER SIDE-----------------------------
// in my server it accepts the connection from b4 and switches to a new socket that will be able to use FD_READ | FD_CLOSE, this is the process:
WSAAsyncSelect(s, hWnd, WM_USER + 1, FD_ACCEPT);
//goes over to my WndProc() switch(Msg):
...
case WM_USER + 1:
HandleAccept(wParam, lParam); <- handles the connection request
break;
...
int HandleAccept(WPARAM wParam, LPARAM lParam)
{
SOCKET sock = (SOCKET)wParam;
WORD event = LOWORD(lParam);
WORD error = HIWORD(lParam);
if(event == FD_ACCEPT)
{
sockaddr_in NewRemoteSin;
newsock = accept(sock, (sockaddr*)&NewRemoteSin, NULL);
WSAAsyncSelect(newsock, hWnd, WM_USER + 2, FD_READ | FD_CLOSE);
}
return TRUE;
}
// ^^^ my new socket that is going to read data being sent is now called "newsock", the WSAAsyncSelect() is now set to FD_READ | CLOSE
<< blah blah i send the data (the numbers when i press F1 on the client side) >>
<< My WndProc() once again >>>
...
case WM_USER + 2:
HandleData(wParam, lParam);
break;
...
<< Now it calls HandleData as above... >>
<< So now we have HandleData>>
int HandleData(WPARAM wParam, LPARAM lParam)
{
SOCKET sock = (SOCKET)wParam;
WORD event = LOWORD(lParam);
WORD error = HIWORD(lParam);
if(event == FD_CLOSE)
{
closesocket(sock);
}
else if(event == FD_READ)
{
ZeroMemory(buf, 18);
recv(sock, buf, 18, 0);
MessageBox(hWnd, buf, "Received Data!", MB_OK);
//closesocket(s);
}
return TRUE;
}
i think i went into a little too much detail...but i did not know how to explain it, but the problem is that the MessageBox() in the HandleData() function never appears...i have spent like 3 hours, moving things around checking for errors, the only thing i came across was when i put the error check above the line closesocket(sock) in the HandleData() function, it says that the client side has closed... it never goes to the FD_READ case. I have no clue what is wrong. thx in advance