Sign in to follow this  
TLAK1001

Send and recv is suicidal!

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this