0

I read the documentation regarding send() function, when it was said that the third parameter (len) is "The length, in bytes, of the data in buffer pointed to by the buf parameter".

I can't seem to understand if it sends the number of bytes I pass, or I nedd to pass the size of the buffer and it sends all the data included there.

Exmaple:

#define X 256 // main : char test[X] = {0}; memcpy(test, "hello", 6); send(sockfd, test, 6, 0) send(sockfd, test, 256,0) // will the first opetion send only hello? or hello000000....? 

Thanks!

1
  • Please consider changing your accepted answer since the one chosen right now is both containing faults and doesn't answer the question. (Especially to help others looking for an answer to the same question.) Commented Nov 29, 2014 at 12:40

2 Answers 2

2

As a complement to David Schwartz proper answer:

Depending on if the socket is non-blocking,or not, it is NOT guaranteed that a single send will actually send all data. You must check return value and might have to call send again (with correct buffer offsets).

For instance if you want to send 10 bytes of data (len=10), you call send(sock, buf, len, 0). However lets say it only manages to send 5 bytes, then send(..) will return 5, meaning that you will have to call it again later like send(sock, (buf + 5), (len - 5), 0). Meaning, skip first five bytes in buffer, they're already sent, and withdraw five bytes from the total number of bytes (len) we want to send.

(Note that I used parenthesis to make it easier to read only, and it assumes that buf is a pointer to 1 byte type.)

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

4 Comments

Im really confused now, as the first answer that I got was that I need to send the total size of the buffer as third element... and you say that this is the number of bytes I want. So if I assume (just for example) that send will transer all data , and my buffer is 256 , containing only "hello" - I can use the thirs parameter as "6" and not pass "256" ?
First answer was wrong. What you specify for send is a buffer that it can send data from and how many bytes from that buffer it should send. If your buffer contains "hello" and you only want to send that (5 bytes excluding NUL terminator, or 6 bytes with), you can specify 6 as len and it will send only 'hello\0'. Specifying 256 it will send the whole content of buffer, no matter whats in it.
To clarify, send() has no concept of strings/integers or anything when it comes to buf, it only sees bytes.
Actually in blocking mode it is guaranteed by Posix.
2

The send function sends precisely the number of bytes you tell it to (assuming it's not interrupted and doesn't otherwise fail). The number of bytes you need to send is determined by the protocol you are implementing. For example, if the protocol says you should send "FOO\r\n", then you need to send 5 bytes. If the protocol specifies that integers are represented as 4 bytes in network byte order and you're sending an integer, the buffer should contain an integer in network byte order and you should send 4 bytes. The size of the buffer doesn't matter to send.

1 Comment

So just to make sure I understood you and @Jite - even though I have a constant buffer (of zise 1000 for example) - when I set the third argument to be 6 - it will send the first 6 bytes (assuming my buffer is char type), and if I need to send next 6 bytes I'll pass (buffer+6) as second paramener and 6 again as third

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.