My code has a problem which I don't understand at all. Here is the code from the server:
usr = uslist_return(&users,i);
if(usr != NULL)
{
send(client,usr,sizeof(usr),0);
//Some extra code
}
else
{
utmp.userid = -1;
send(client,&utmp,sizeof(utmp),0);
}
Here is the code from the client:
recv(sock,tmp,sizeof(tmp),0);
if(tmp->userid > 0)
{
//Some Code
}
else
{
//Some other code
}
The problem here is that although I check that send() doesn't send a NULL pointer, the pointer that send() sends ends up being NULL. Maybe it's from recv(). I don't have the slightest idea.
Ignore uslist_return(...). It's just a function that returns a pointer. And the utmp is the same type (but not a pointer) as usr.
Edit: The server sends a pointer in if brackets and a struct in else brackets. The problem occures when the server sends the pointer usr and the client gets it and saves it in the pointer tmp (same type pointers). I Haven't check the else case yet, due to this.
Edit 2: Thanks Martin James for commenting that. It appears that recv() returns -1. Perror shows that the error is Bad Address. I don't know what this is.
CodePudding user response:
It appears you are confusing a count of users, say, 27, with the size in byte of the variable usr
, say, 8 bytes. The send()
argument len
is number of bytes you are sending. If each record is uniform in size then it's count * sizeof(record), otherwise you need to sum the size of each record.
Also, sending pointers, as you allude to, does not usually make sense as server and client use different address spaces.
Check return value of send()
and recv()
.