Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Problems with send() and recv() functions(socket programing) (http://www.programmingforums.org/showthread.php?t=12395)

Exodust Jan 17th, 2007 12:04 PM

Problems with send() and recv() functions(socket programing)
 
Hi,
I am trying to make a chat program over a LAN network for my college project.
So as a start, I tried to make a server program and a client program, and ran them on the same pc using that loop back address 127.0.0.1.

Now, I successfully made the client program connect to the server program. But when I tried to implement send() and recv() to receive a string of text and show the text in a messagebox, I failed to do so. I can't find the error in the code. It is compiling with no problems, but while running, the server receives no text string. :(

Here is the code (for client) (showing only the windows procedure and the required functions:
:

HWND hwnd;
SOCKET s;

bool ConnectToHost(int PortNo,char* IPAddress)
{
    WSADATA wsadata;
   
    int error=WSAStartup(0x0202, &wsadata);
   
    if(error)
    return false;
   
    if(wsadata.wVersion!=0x0202)
    {
                                WSACleanup();
                                return false;
    }
   
    SOCKADDR_IN target;
   
    target.sin_family=AF_INET;
    target.sin_port=htons(PortNo);
    target.sin_addr.s_addr= inet_addr(IPAddress);
   
    s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
   
    if(s==INVALID_SOCKET)
    {return false;
    }
   
    if(connect(s,(SOCKADDR*)&target,sizeof(target))==SOCKET_ERROR)
    return false;
    else return true;
}

void CloseConnection()
{
    if(s)
    closesocket(s);
    WSACleanup();
}

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            { bool flag;
              flag=ConnectToHost(1337,"127.0.0.1");
              if(flag) MessageBox(hwnd,"Connected","Connected",MB_OK);
              else MessageBox(hwnd,"Error","Error",MB_OK);
              char messg[20]="Nice!!!!";
              send(s,messg,8,0);
            }
            break;
           
        case WM_QUIT:
            {CloseConnection();
            }
            break;
        case WM_DESTROY:
            PostQuitMessage (0);      /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;


Here is the code for the server program:
:

SOCKET s;
WSADATA w;
HWND hwnd;

bool ListenOnPort(int portno)
{
    int error=WSAStartup(0x0202,&w);
   
    if(error)
    {
              return false;
    }
   
    if(w.wVersion!=0x0202)
    {
              WSACleanup();
              return false;
    }
   
    SOCKADDR_IN addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(portno);
   
    addr.sin_addr.s_addr=htonl(INADDR_ANY);
   
    s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
   
    if(s==INVALID_SOCKET)
    {
                          return false;
    }
   
    if(bind(s,(LPSOCKADDR)&addr,sizeof(addr))==SOCKET_ERROR)
    {
          return false;
    }
   
    listen(s,SOMAXCONN);
    WSAAsyncSelect(s,hwnd,MY_MESSAGE_NOTIFICATION,(FD_ACCEPT|FD_CONNECT|FD_READ|FD_CLOSE));
   
}

void CloseConnection()
{
    if(s)
    closesocket(s);
    WSACleanup();
}

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
          ListenOnPort(1337);
          break;
         
        case WM_QUIT:
            CloseConnection();
            break;
        case MY_MESSAGE_NOTIFICATION:
            {
                  switch(lParam)
                  {
                      case FD_ACCEPT:
                          break;
                      case FD_CONNECT:
                          break;
                      case FD_READ:
                          char buffer[80];
                          memset(buffer,0,sizeof(buffer));
                          recv(s,buffer,sizeof(buffer)-1,0);
                          MessageBox(hwnd,buffer,"Captured Text...",MB_OK);
                          break;
                      case FD_CLOSE:
                          break;
                  }
              }
              break;
                               
       
        case WM_DESTROY:
            PostQuitMessage (0);      /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


Exodust Jan 19th, 2007 1:44 AM

It's been two days, and no replies.....

I've searched through each instance of the function send() or recv() in the MSDN.... read some examples etc. All examples use the functions like I do. What's the problem then?

Forum veterans please read this and help. Dawei, where are you :)

Duck Jan 20th, 2007 9:29 AM

Quote:

if(connect(s,(SOCKADDR*)&target,sizeof(target))==SOCKET_ERROR)
return false;
else return true;
There could be other errors, but something that stands out to me is in your ConnectToHost function you haven't called the WSAAsyncSelect function to setup windows message-based notification of network events for the connect socket.

EDIT: By the way there are many nice example projects you can download from places like codeproject.com/gamedev.net

Exodust Jan 20th, 2007 11:46 AM

Ok, Thanks, I'll give it a shot. And thanks for the link.


All times are GMT -5. The time now is 1:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC