ryanmattscott
ryanmattscott

Reputation: 341

What does send() and recv() return? Not getting full message, always limited to one byte/8 characters?

I'm doing a simple socket example in C on ubuntu linux. I know that the send() and recv() function don't necessarily wait to send/receive the whole message. I'm trying to send a simple hello world message. I know the size of it (at least I think I do), so I'm wondering what I could add to my send() and recv() implementation to wait for a known message size.

Reading the man page for send() and recv() says the function prototype looks like this : ssize_t send(int sockfd, const void* buf, size_t len, int flags) and recv() uses the same prototype. My question is what do they return - number of bytes or number of characters? Either way why is it always 8?

When testing the size of my message the sizeof() operator is always returning 8 whether I use char* message = "test"; or char* message = "Hello world, I clearly do not understand what is going on here";

client code

  fprintf(stdout, "client connected to %s:%d\n", hostname, portno);
  printf("Size of %s is : %lu\n", message, sizeof(message));
  printf("other size of %s is %lu, message, sizeof(message)/sizeof(char));
  ssize_t written_bytes = send(socket_fd, message, sizeof(message),0);
  assert(written_bytes == sizeof(message)); //Shouldn't the code fail here?
  printf("written bytes: %lu\n", written_bytes);

I would add the server code, but there's no point in wondering why it's not receiving everything if it is never sending it.

Upvotes: 0

Views: 765

Answers (1)

haccks
haccks

Reputation: 106012

send and recv returns number of bytes sent and received respectively. When you are testing the size of your message with sizeof operator then you are actually calculating the size of pointer which is 8 bytes on your machine.

Upvotes: 1

Related Questions