Skip to main content
deleted 114 characters in body
Source Link
Jite
  • 4.4k
  • 1
  • 19
  • 18

As a complement to David Schwartz proper answer:

Depending on underlying protocolif 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.

You are best suited to do it in a while loop, somethinglike while(sent_bytes < len) (and ofc check for -1 and 0 return values). Note alsoNote that I used parenthesis to make it easier to read only, and it assumes that buf is a pointer to 1 byte type.)

As a complement to David Schwartz proper answer:

Depending on underlying protocol, 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 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.

You are best suited to do it in a while loop, somethinglike while(sent_bytes < len) (and ofc check for -1 and 0 return values). Note also that I used parenthesis to make it easier to read only, and it assumes that buf is a pointer to 1 byte type.

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

added 9 characters in body
Source Link
Jite
  • 4.4k
  • 1
  • 19
  • 18

As a complement to David Schwartz proper answer:

Depending on underlying protocol, 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 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.

You are best suited to do it in a while loop, somethinglike while(sent_bytes < len) (and ofc check for -1 and 0 return values). Note also that I used parenthesis to make it easier to read only, and it assumes that buf is a pointer to 1 byte type.

As a complement to David Schwartz proper answer:

Depending on underlying protocol, 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, 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 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.

You are best suited to do it in a while loop, somethinglike while(sent_bytes < len) (and ofc check for -1 and 0 return values). Note also that I used parenthesis to make it easier to read only, and it assumes that buf is a pointer to 1 byte type.

As a complement to David Schwartz proper answer:

Depending on underlying protocol, 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 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.

You are best suited to do it in a while loop, somethinglike while(sent_bytes < len) (and ofc check for -1 and 0 return values). Note also that I used parenthesis to make it easier to read only, and it assumes that buf is a pointer to 1 byte type.

added 142 characters in body
Source Link
Jite
  • 4.4k
  • 1
  • 19
  • 18

As a complement to David Schwartz proper answer:

Depending on underlying protocol, 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, 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 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.

You are best suited to do it in a while loop, somethinglike while(sent_bytes < len) (and ofc check for -1 and 0 return values). Note also that I used parenthesis to make it easier to read only, and it assumes that buf is a pointer to 1 byte type.

As a complement to David Schwartz proper answer:

Depending on underlying protocol, 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, 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 like send(sock, (buf + 5), (len - 5), 0).

You are best suited to do it in a while loop, somethinglike while(sent_bytes < len) (and ofc check for -1 and 0 return values). Note also that I used parenthesis to make it easier to read only, and it assumes that buf is a pointer to 1 byte type.

As a complement to David Schwartz proper answer:

Depending on underlying protocol, 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, 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 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.

You are best suited to do it in a while loop, somethinglike while(sent_bytes < len) (and ofc check for -1 and 0 return values). Note also that I used parenthesis to make it easier to read only, and it assumes that buf is a pointer to 1 byte type.

Source Link
Jite
  • 4.4k
  • 1
  • 19
  • 18
Loading