Client:
I've used write()
to send data from client -> server.
I need to determine the file size at the server which is the 3rd argument in write(sockfd, buffer, strlen(buffer)
.
Should I use writev()
to send the data from client -> server as separate buffers? writev(sockfd, buffer, strlen(buffer))
? Is this the right approach?
write(sockfd, buffer, strlen(buffer));
Server:
read()
.readv()
- to obtain the file size?It doesn't matter. TCP sends bytes. You send some bytes, some more bytes and some more bytes. It doesn't know about buffers. Buffers are just how you tell TCP which bytes to send.
When you use writev with 3 buffers, you're telling TCP to send the bytes in the first buffer and then the bytes in the second buffer and then the bytes in the third buffer. They all get joined together. Same as if you told it to write one big buffer.
If you want to send two things at once (like a file size and then file data) then writev can be more convenient, or not. Note that writev can decide to stop writing at any point in any buffer, and you have to call it again to write the rest. That makes it not very convenient.
And it has no relevance to the server either. The server is allowed to read the bytes into one buffer and then a second buffer if the first one fills up and then a third buffer if the second one fills up. Or it can read them into one big buffer. TCP doesn't care - they're the same bytes either way.
readv has the same problem as writev where it might only read the first one and a half buffers, instead of all 3 at once, and then you have to call it again and tell it to read the second half of the second buffer and the entire third buffer.