Send and recv is suicidal!

Started by
9 comments, last by TLAK1001 17 years, 10 months ago
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?
Advertisement
Is password a pointer? If so, I'm afraid you have an unterminated string.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Password is defined as an array of 36 chars
char password[36];
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
Armand -------------------------It is a good day to code.
Can you post your connect and listen/accept code as well? There might be some important clues in how the socket is being set up.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.
It only takes one mistake to wake up dead the next morning.
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.
Show the declarations of 'id' and 'password', and how they're manipulated up until the point they are sent, please.
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.
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]
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson

This topic is closed to new replies.

Advertisement