The main task for this week is a project in which we
need to write a short program to implement the function of transmission between
two entities, (ie. Two computers with different IP address named Robot and
Student respectively) using two different transmission layer protocols – TCP
and UDP. What’s more, for the bonus, we need to extend the program to implement
the function that measures the different throughput for different receiver
buffer size. The foal of the task is to help us get familiar with the usage of
socket programming as well as some important properties of sending and
receiving data such as buffer size and how port works as well.
To design the program, the most important part is to understand
how the connection procedure and properties should be in either TCP or UDP
protocols. In the past lecture, we should have known that the procedures for
these two transmission modes should be like:
Figure 1 : functions flow of
connection-oriented socket and connectionless Socket
Implementing the functions shown above and we can
accomplish our basic goal. Besides, there also have some detailed staff that we
should take care of.
First of all, when considering the structure object sockaddr_in,
it is describing and identifying the socket address family of either the server
or the client such as IP address and port number for the socket.
For example, the definition of the family address in the program
for the robot is like:
Secondly, for TCP socket, it is
a stream socket which does not preserve message boundary, so in order to avoid
the situation that the buffer load the information before all the data is
correctly received, a loop is needed for receiving data in TCP protocol. It is
the same for the sending procedure. Take the codes of receiving as an example,
it is like :
Thirdly, in the bonus part,
we need to let the robot continuing sending packets to the destination, lasting
for 30 seconds. To solve this problem, a time function is needed, clock_t
determines a variable corresponding to a certain time, in the mean time
function clock() will return the current time from the computer. By using these
two properties and a loop, the continuous sending procedure can be done. In the
program, it is like:
Through the programming project, though it is a hard and tiring work, my
understanding of this course has been deeply developed. Although it is just a
tiny little part of computer network, it stands for what is the work procedure
like for the engineer to develop and implement those theories we are talking in
the class. To discuss a method, or a tactic a easy, but to implement it is a
not a easy work, which would always requires for deeply understanding and cautious
habits.
In the project, one requirement is to find
out the initial receiving buffer size and in addition to modify its value to
see how the buffer size would effect the throughput. To accomplish this task,
we need two functions which are included in the "winsock2.h" .They
are getsockopt() and setsockopt() respectively.
For the function getsockopt(), it’s used to get the
information in the socket bind by the user. It’s structure is as the figure
shown below:
sockfd : is the description the
socket, it should be pointed to the socket
you are willing to execute.
level : is determine for the options of the socket,
there exit
two levels that are SOL_SOCKET and IPPROTO_TCP.
optname:
the option that the user need to get from the socket
optval
: a pointer that points to the buffer location of the option value.
optlen
: a pointer that points to the length of the buffer area
For the function setsockopt(), it’s used to set the value in the
socket bind by the user. It’s structure is as the figure shown below:
where:
sockfd : is the description the
socket, it should be pointed to the socket
you are willing to execute.
level : is determine for the options of the socket,
there exits four levels
which are SOL_SOCKET, IPPROTO_TCP,
IPPROTO_IP and IPPROTO_IPV6
optval
: a pointer that points to the buffer location of the option value.
optlen
: a pointer that points to the length of the buffer area
By using these two functions, we can get the information that the
default receive buffer size is 8192 bytes. Once the user increase or decrease
the buffer size, it can be seen that the throughput of the packets would
increase and decrease in pace with the buffer size.