$30
CSP334: Computer Networks
Lab Assignment No 5
Socket Programming #1
Instructions:
1. Please use only the C as the language of programming.
2. Please submit/upload on LMS, the following files for each of your programs: (1) the client and the server source files each (2) the
client and the serve executable files each (3) a brief Readme file that shows the usage of the program.
3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code.
4. Neat coding is necessary as illustrated in the class, that will carry specific marks for each program that you write.
5. In the first review (during the lab hours) on the stipulated date, it is expected that a few (if not all) of the assignments in this list are
compledted and the execution shown and explained to the teacher in the lab.
Problems
1. As discussed in class, the socket address is defined as follows:
s t r u c t i n a d d r { u l o n g s a d d r ; }
/∗32− b i t s netID o r hostID i n NBO ∗/
s t r u c t s o c k a d d r i n {
s h o r t s i n f a m i l y ; /∗2 b y t e s AF INET∗/
u s h o r t s i n p o r t ; /∗ 2 by te p o r t no ∗/
s t r u c t i n a d d r si n a d d r ; /∗ 4 b y t e s ∗/
ch a r s i n z e r o [ 8 ] ; /∗ 8 b y t e s unused ∗/
/∗ used f o r padding the l e n g t h o f the s t r u c t u r e t o 16 b y t e s ∗/
}
Write a brief note on the meaning of each component of both the structures.
2. Write a program with two functions: (1) First function takes an IPA as argument from the user on
the command line (i.e. in the form of a string) and populates the structure sockaddr in with the
IPA in the NBO; using the function int inet aton(const char *cp, struct in addr *inp)
(2) Second, reads the IPA from an already populated structure and prints the IPA in the string format
using the counterpart function int inet ntoa(const char *cp, struct in addr *inp)
3. As discussed in class, the Intel machines are little-endian machines whereas the NBO is big-endian. Using
the logic discussed in the class, write a program that detects and prints whether a machine is a littleendian or a big-endian. Write another program that would - convert the same to network byte order and
given an IPA and port number in network byte order, would convert the same to the host byte order.
4. Write a tcp client program that does not use bind() function, but relies upon the kernel to provide
an IPA and port number. Since the IPA and port number are provided by the kernel, write a function
which obtains the local socket name using the call viz.int getsockname(int sockfd, struct
sockaddr *localaddr, socklet t *addrlen) and displays the allocated IPA and port numbers.
Look up the man pages of getsockname to learn how to use this function.
5. Write a program that takes multiple command line arguments (which are machine names) and uses
appropriate function to print their IP addresses. Write its counterpart that takes multiple command line
arguments (which are IP addresses) and uses appropriate function to print their hostnames. Include an
additional option which determines which function to invoke and which argument to take.
1
6. Modify the TCP and UDP dautime service client illustrated in the lab to display the hostname and the
port number of the server, also along with the time of the day. Recollect that he daytime client uses the
datyime service running on port no 13 on the server to fetch the time of the day from the server and
display the same on the standard output on the client side.
7. Now modify the client in the problem number 6 that requests an iterative TCP server on port no 99XX
(XX are the last two digits of your IIT enrollement number) to send it the time of the day. The server
upon receiving the client request responds with the time of the day, which the client outputs on the
standard output, along with the hostname and the port number of the server. Now start the tcpdump
with appropriate arguments (refer $man tcpdump) to capture packets from or to a remote host. After
you have started the TCP server and the TCP client terminate tcpdump, examine its output and make
appropriate inferences based on the output.
8. Repeat the exercise in Problem no 7, but now with UDP as the underlying Transport protocol.
9. Modify the server program in problem 7 to make it work as a concurrent server.
2