-1

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.

5
  • 3
    the pointer I that send() sends ends up being NULL - what does it mean? How do you know this? Anyway, sizeof(usr) is a size of a pointer, not the pointed data. Commented Sep 10, 2021 at 21:13
  • You want to debug either the client or server. netcat aka nc or socket is useful utilities to play either. In the first case, usr != NULL, it sounds like a list of users but you do sizeof(usr) which is the size of the variable not count of users. In the usr == NULL, you don't tell us what utmp is. Please provide a minimal reproducible example Commented Sep 10, 2021 at 21:14
  • 1
    if usr is a pointer sizeof(usr) is the size of the pointer (probably 4 or 8), not the size of what it points to. Commented Sep 10, 2021 at 21:14
  • 1
    send(), recv() return values, you must not ignore them:( Commented Sep 11, 2021 at 3:35
  • "Perror shows that the error is Bad Address. I don't know what this is." Probably because the value of tmp is not a valid address. I can't tell for sure because your code is incomplete. However, the piece of code you posted does not seem to be correct. You could improve your question by creating a minimal reproducible example. Commented Sep 11, 2021 at 15:21

1 Answer 1

0

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().

Sign up to request clarification or add additional context in comments.

8 Comments

I don't really understand that. Because, in other places of the code, I use pointers and nothing happens. It seems that you are right, though, because I haven't reached those points in the code, yet. First I have to do that. I will check it.
Could I use send(sock,&(*usr),sizeof(*usr),0)? I don't know if is this the same as before.
You haven't told us what type usr is, so it's hard to help you. If it's an int and you just have one of them, yes, then send(..., &usr, sizeof(usr), ..) is the way to send the value stored in usr. You are NOT sending a pointer. If usr is a struct, then you may need to serialize it (convert values, and ensure pointers are converted to something that makes sense to client).
Indeed the problem was sending the pointer. The usr is a struct. I had to pass each member separately (int,char etc, not complex types). But, when I was sending a struct (not a pointer) by mistake, and the client received just some bytes, no error was occur, except from the server waiting the client to receive the remaining bytes.
Just a heads-up that there are libraries to help you with (de)serializing aka marshaling. Traditionally, you would use ASN.1, these days you might use JSON, or you can use a dedicated library like Cap'n Proto or Protobuf.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.