Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 17th, 2007, 11:04 AM   #1
Exodust
Newbie
 
Join Date: Mar 2006
Location: India, The great :p
Posts: 7
Rep Power: 0 Exodust is on a distinguished road
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;
}
__________________
My Blog!!
Exodust is offline   Reply With Quote
Old Jan 19th, 2007, 12:44 AM   #2
Exodust
Newbie
 
Join Date: Mar 2006
Location: India, The great :p
Posts: 7
Rep Power: 0 Exodust is on a distinguished road
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
__________________
My Blog!!
Exodust is offline   Reply With Quote
Old Jan 20th, 2007, 8:29 AM   #3
Duck
Programmer
 
Join Date: Jun 2006
Location: England London
Posts: 72
Rep Power: 3 Duck is on a distinguished road
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
Duck is offline   Reply With Quote
Old Jan 20th, 2007, 10:46 AM   #4
Exodust
Newbie
 
Join Date: Mar 2006
Location: India, The great :p
Posts: 7
Rep Power: 0 Exodust is on a distinguished road
Ok, Thanks, I'll give it a shot. And thanks for the link.
__________________
My Blog!!
Exodust is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:32 PM.

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