| 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;
}
|