Question

I am trying to use winsock2 in asynchronous mode. I am able to send messages from client to server, but not from server to client. When I call recv on client side, nothing is received in the buffer.

Here is my client code

ClientSocket::ClientSocket (HWND& pHwnd)
{   
    WSADATA     wsdata;

    vSzIncoming     =   0;

    int error = WSAStartup (MAKEWORD(2,2), &wsdata);

    vSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);

    error = WSAAsyncSelect (vSocket, pHwnd, WM_SOCKET, (FD_CLOSE | FD_READ));

    vHwnd           =   pHwnd;
    vIsConnected    =   false;
}

bool ClientSocket::ConnectToServer () 
{       

SockAddr.sin_port           =   htons (20000);
SockAddr.sin_family         =   AF_INET;
SockAddr.sin_addr.s_addr    =   inet_addr ("127.0.0.1");

if(connect (vSocket, (LPSOCKADDR)(&SockAddr), sizeof (SockAddr)) == SOCKET_ERROR) {


    vIsConnected = false;
    return false;
}

vIsConnected = true;
return true;
}

void ClientSocket::SendMsg (const MessageGenerator& pMessageGenerator)
{   
    send(vSocket, pMessageGenerator.GetMessage ().Buffer (), pMessageGenerator.GetLength (), 0);
}

char * ClientSocket::ReceiveMsg ()
{   
char temp[1024];
ZeroMemory (temp, sizeof (temp));

int inDataLength = recv (vSocket,
                    (char*)temp,
                     sizeof (temp) / sizeof (temp [0]),
                     0);
return temp;
}

Here's The Server code

WSADATA WsaDat;
        int nResult=WSAStartup(MAKEWORD(2,2),&WsaDat);


        Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

        SOCKADDR_IN SockAddr;
        SockAddr.sin_port=htons(nPort);
        SockAddr.sin_family=AF_INET;
        SockAddr.sin_addr.s_addr=htonl(INADDR_ANY);

        if(bind(Socket,(LPSOCKADDR)&SockAddr,sizeof(SockAddr))==SOCKET_ERROR)
        {

        }

        nResult=WSAAsyncSelect(Socket,
                hWnd,
                WM_SOCKET,
                (FD_CLOSE|FD_ACCEPT|FD_READ));

        if(listen(Socket,(1))==SOCKET_ERROR)
        {

        }

//send part

String str ="1;yahoo;|";
send(Socket,str.Buffer(),str.GetLength(),0);

//receive part

case WM_SOCKET:
    {
        switch(WSAGETSELECTEVENT(lParam))
        {
            case FD_READ:
            {
                char szIncoming[1024];
                ZeroMemory(szIncoming,sizeof(szIncoming));

                int inDataLength=recv(Socket,
                    (char*)szIncoming,
                    sizeof(szIncoming)/sizeof(szIncoming[0]),
                    0);

                String str(szIncoming);

                wcsncat(szHistory,str.GetTChar(),inDataLength);
                wcscat(szHistory,L"\r\n");


                SendMessage(hEditIn,
                    WM_SETTEXT,
                    sizeof(szIncoming)-1,
                    reinterpret_cast<LPARAM>(&szHistory));
            }

case FD_ACCEPT:
            {
                int size=sizeof(sockaddr);
                Socket=accept(wParam,&sockAddrClient,&size);                
            }

Please help me

Was it helpful?

Solution

It's difficult to say where your problem is. The best solution is to systematically remove all possible roadblocks:

  • Check the return value of recv. If there is an error your buffer is untouched so an "empty" string is returned.
  • Check on your server side if the correct client socket is used. You use the "Socket"-variable name too many times. Best is to distinguish between client socket and server socket. Also: how do you store the client-socket? Is this in a global or static variable? Are you certain that this value is correct? (This point is mostly for your server-side code)
  • Extend your code with logging so you can trace what's going on on client and server-side. A debugger might also help with that.

Also there is one rather big mistake which you should correct immediately in your code:

char * ClientSocket::ReceiveMsg ()
{   
    char temp[1024];
    ZeroMemory (temp, sizeof (temp));

    int inDataLength = recv (vSocket,
                        (char*)temp,
                         sizeof (temp) / sizeof (temp [0]),
                         0);
    return temp; // ** don't return a local memory "object" **
}

Don't do that. Either return a duplicate using _strdup or use and return your "String"-object. Of course, if you're using _strdup the caller has to free the memory with the appropriate function (free). Ah yes, the third possibility is to just expect the buffer to be given as a parameter like recv does already.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top