After the successful implementation of the UDP version of the integer-summing service, you need develop the TCP version.
TCP has several advantages and pecularities compared to UDP. TCP is a connection-oriented protocol. The client creates the TCP connection (with code that you do not need to write) and then the client sends the number of integers (as a 32-bits unsigned integer) in the vector and then all the elements of the vector. The server accepts the TCP connection (with code that you do not need to write) and then processes the integers received and returns the sum of the elements of the vector over this connection.
We ask you to complete the TCP version of the client and the functions. These two functions are tested separately. This implies that your client function is tested against a correct server function while your server function is tested against a correct client function.
There several important points about TCP that you need to remember before answering this question:
- As in the UDP question, you need to encode the integers that are transmitted in network byteorder
- TCP provides a bytestream service. Bytestream means that your code might receive a few bytes and then a few bytes later in contrast with UDP that always provides messages. Remember to check the return values of the
send
andrecv
system calls. - Remember that memory leaks are a frequent problem in C code. Every memory block that you allocate with
malloc
must be released before the termination of your function, even if some system calls return errors. - Remember computers using IPv4 and computers using IPv6 cohabitate, so your program should be able to handle both. It doesn't require a lot of adaptation (only one line in one function).
You may also need to read the manpages for the following calls: recv
, recvfrom
, send
, sendto
, close
, htonl/ntohl
, accept
, listen
, bind
and connect
.