• Advertisement
Sign in to follow this  

Send and recv is suicidal!

This topic is 4271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a server program to play with the concepts of winsock. The part I'm having trouble with is this:
recv(sock,(char*)&id,sizeof(id),0);
recv(sock,password,sizeof(password),0);
cout<<"Client number "<<id<<" has connected"<<endl; //Tell user who connected
cout<<"Password: "<<password<<endl; //For debugging

My client sends as follows:
send(server,(char*)&id,sizeof(id),0);
send(server,password,sizeof(password),0);
cout<<"Data sent"<<endl;

For some reason, no matter what I enter, the server says "Client number 4007136 has connected" "Password: <random junk>" Although the password output changes all the time, the number is ALWAYS 4007136. Why aren't these simple functions working?

Share this post


Link to post
Share on other sites
Advertisement
Is password a pointer? If so, I'm afraid you have an unterminated string.

Share this post


Link to post
Share on other sites
Okay, 3 things I'd try.

1) zero your varibles before the read's.
2) check the size of your reads, recv returns the number of bytes read.
3) flush after your send. small packets can and will get queued.

After that, you should have enought info to work out wahts going on.

Armand

Share this post


Link to post
Share on other sites
Also, you may want to try outputting to the console the value of this line:
sizeof(password);
The answer might not be what you expect.

Share this post


Link to post
Share on other sites
Yeah, sure.
Here's the code for accepting a connection:


clientSocket=accept(sock,clientAddr,0);
cout<<"\nConnection requested"<<endl;

if(clientSocket==INVALID_SOCKET)
{
cout<<"Error: connection failed"<<endl;
}
else //Continue if socket is good
{
recv(sock,(char*)&id,sizeof(id),0);
recv(sock,password,sizeof(password),0);
cout<<"Client number "<<id<<" has connected"<<endl;
cout<<"Password: "<<password<<endl;



And here's the client connecting:


//Connect to server
cout<<"Logging in..."<<endl;
if(connect(server,(const sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR)
{
cout<<"Error: Could not connect to server!"<<endl;
cin.ignore(1,'\n');
WSACleanup();
return 0;
}

//Send the id and password to server, and see if we're allowed to continue
send(server,(char*)&id,sizeof(id),0);
send(server,password,sizeof(password),0);
cout<<"Data sent"<<endl;



I tried zeroing out the variables before they're recieved, and the number changed from 4007136 to 0, and the password changed to "" every time.

Share this post


Link to post
Share on other sites
Show the declarations of 'id' and 'password', and how they're manipulated up until the point they are sent, please.

Share this post


Link to post
Share on other sites
Something I find useful - Install ethereal, that'll tell you if the problem is with your send() or recv(), since you'll be able to see what Winsock actually sends.

Share this post


Link to post
Share on other sites
You should be receiving using the socket you got back from accept, not the listening socket.


clientSocket=accept(sock,clientAddr,0);
cout<<"\nConnection requested"<<endl;

if(clientSocket==INVALID_SOCKET)
{
cout<<"Error: connection failed"<<endl;
}
else //Continue if socket is good
{
//you want to receive on the clientSocket, not sock
recv(clientSocket,(char*)&id,sizeof(id),0);
recv(clientSocket,password,sizeof(password),0);
cout<<"Client number "<<id<<" has connected"<<endl;
cout<<"Password: "<<password<<endl;
[\source]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement