c - Transmitting variable structure size with Berkeley socket over a TCP connection -
i have following structure defined in c, , want send using berkeley socket on tcp connection between client , server in linux:
struct argument{ int pid; int length; chat op; char *data; };
since have "char *data" pointer can used allocating variable size of data in local sending machine, have send structure in 2 different times receiver side. first time, send fixed variables i.e. first 3 variables. , upon reception, allocate buffer length size receive data part in second time.
so question is there anyway send structure 1 time other side variable data field size, not 2 times doing?
if (write(peer_fd, (struct argument*) arg, sizeof (struct argument)) < 0) { close(peer_fd); return -1; }
thanks lot.
consider using scatter-gather read-write techniques.
the wtitev allows write multiple buffers (the fixed sized header , char array) in single call. incur cost of single system call, , sockets code inside kernel assembles data single buffer (if possible), , makes single network call.
Comments
Post a Comment