I have the following structure defined in C, and I want to send it using Berkeley Socket over a TCP connection between client and a server in Linux:
struct Argument{
int pid;
int length;
chat op;
char *data;
};
Since I have "char *data" which is a pointer than can be used for allocating a variable size of data in the local sending machine, I have to send this structure in two different times to the receiver side. The first time, I send only the fixed variables ie the first three variables. And then upon the reception, I allocate a buffer with a length size to receive the data part in the second time.
So my question is is there anyway to only send this structure one time to the other side with a variable data field size, not two times as what am I doing?
if (write(peer_fd, (struct Argument*) arg, sizeof (struct Argument)) < 0)
{
close(peer_fd);
return -1;
}
Thanks a lot.
Consider using scatter-gather read-write techniques.
The wtitev allows you to write from multiple buffers (the fixed sized header and the char array) in a single call. You incur the cost of a single system call, and the sockets code inside the kernel assembles the data into a single buffer (if possible), and makes a single network call.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.