CS 372 Computer Networks: Assignment #3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

4/2/2018 www.cs.northwestern.edu/~agupta/_projects/networking/TCPClientServer/report.

html

CS 372
Computer Networks
Assignment #3

Submitted by:
Ashish Gupta , 98131
Ravi Krishna , 98422

Description

In this assignment , we were told to develop a series of programs based on socket programming using both TCP and UDP protocol.
7 programs were developed in this assignment

1. TCP Echo Client


2. UDP Echo Client
3. TCP Echo Server
4. UDP Echo Server
5. UDP Date Client
6. TCP/UDP Echo Server using I/O Multiplexing
7. A TCP based client/server system consisting of a server which responds to multiple clients and allows them to issue "ls" and "more"
commands to view the directory information and view a file on the server machine.

Logical Steps in assignment

1. TCP Echo Client

In the TCP Echo client a socket is created.


Using the socket a connection is made to the server using the connect() function.
After a connection is established , we send messages input from the user and display the data received from the server using
send() and read() functions.

2. UDP Echo Client

In the UDP Echo client a socket is created.


Then we bind the socket.
After the binding is succesful , we send messages input from the user and display the data received from the server using
sendto() and recvfrom() functions.

3. TCP Echo Server

http://www.cs.northwestern.edu/~agupta/_projects/networking/TCPClientServer/report.html 1/4
4/2/2018 www.cs.northwestern.edu/~agupta/_projects/networking/TCPClientServer/report.html

In the TCP Echo server , we create a socket and bind to a advertized port number.
After binding , the process listens for incoming connections.
Then an infinite loop is started to process the client requests for connections.
After a connection is requested , it accepts the connection from the client machine and forks a new process.
The new process receives data from the client using recv() function and echoes the same data using the send() function.
Please note that this server is capable of handling multiple clients as it forks a new process for every client trying to connect
to the server.

4. UDP Echo Server

In the UDP Echo server , we create a socket and bind to a advertized port number.
Then an infinite loop is started to process the client requests for connections.
The process receives data from the client using recvfrom () function and echoes the same data using the sendto() function.
Please note that this server is capable of handles multiple clients automatically as UDP is a datagram based protocol hence
no exclusive connection is required to a client in this case.

5. UDP Date Client

In the UDP Date client a socket is created.


Then we bind the socket.
After the binding is succesful , we send an arbitrary datagram to port 13 on the server machine and then display the datagram
received which is supposed to be the data/time on the server.

6. TCP/UDP Echo Server using I/O Multiplexing

We have used the concept of I/O multiplexing for managing both TCP and UDP port in the same server.

fd_set fdvar;
FD_ZERO(&fdvar);
FD_SET(tcp_sfd,&fdvar);
FD_SET(udp_sfd,&fdvar);
int maxpl = max(tcp_sfd,udp_sfd);
cout << "Waiting for a client...\n";
if(select(maxpl+2 ,&fdvar,NULL,NULL,NULL)==-1)
{
perror("error in select");
}
if(FD_ISSET(udp_sfd,&fdvar))
{
// UDP
}
else
{
//TCP
}
struct fd_set can be set to monitor activity on many ports at the same time.
FD_SET(tcp_sfd,&fdvar) sets the appropriate bit in fdvar so that select may monitor tcp_sfd
similarly FD_SET(udp_sfd,&fdvar) sets the appropriate bit in fdvar so that select may monitor
the udp_sfd.

http://www.cs.northwestern.edu/~agupta/_projects/networking/TCPClientServer/report.html 2/4
4/2/2018 www.cs.northwestern.edu/~agupta/_projects/networking/TCPClientServer/report.html
We use select system call to monitor socket fd's for TCP & UDP servers which are
listening to the same port ie. port 3001

FD_ISSET checks which sfd has received a packet.


Once this is known we handle the request as in case of TCP client or UDP client

7. A TCP based client/server system

In this part of the assignment we have proceeded on the lines of TCP Echo Server / Echo Client.

We create a TCP Server which listens for various TCP Clients trying to connect to the server.
Once a client connects we start a new process which deals with that particular client.
It receives the message from the client and breaks it into various tokes seperated by space.
It then analyses the arguments for "ls" and "cat" command otherwise it returns a default message.

If it receives a "ls" or "cat" command , it executes the command on the system and collects the output in a file on the server side. Then it
reads the file line by line and sends it to the client machine.
In the "cat" part of the program , it transmits 24 lines of the file at one time and then waits for an acknowledgement from the client machine
before proceeding to display the next 24 lines of the file.
The user on the client machine may also transmit "q" to finish the "cat" command in between its execution.

The TCP Server also shows which clients are connecting and also displays what information it gets from the various clients to show how the
server is working.

Output

1. The Server in the TCP Server/Client System generates the following output :

File Server started...

Connected to : 10.20.14.25
Received from 10.20.14.25 : ls

Received from 10.20.14.25 : hello

Connected to : 10.20.14.25
Received from 10.20.14.25 : ls

Received from 10.20.14.25 : cat UDPServer.cpp

Received from 10.20.14.25 :

Received from 10.20.14.25 :

Received from 10.20.14.25 : q

Received from 10.20.14.25 : exit

Connection closed to : 10.20.14.25

As we can see , it displays which clients have connected to it. It also displays any messages receieved from it. If the client closes down , it
shows that the connection to the respective client has been closed.

2. The Client in the TCP Server/Client System generates the following output :

Connecting to 10.20.14.25
Connected.

http://www.cs.northwestern.edu/~agupta/_projects/networking/TCPClientServer/report.html 3/4
4/2/2018 www.cs.northwestern.edu/~agupta/_projects/networking/TCPClientServer/report.html
10.20.14.25 : ls
TCPClient.cpp
TCPFileClient.cpp
TCPFileServer.cpp
TCPServer.cpp
UDPClient.cpp
UDPDateClient.cpp
UDPServer.cpp
YSOSREHFTFANAEETQVTMXWSLPIQVIZJJUXBLDKTWRTJSXPLPLGCKCWVTEOQMPZVJYZVBLOZDJJVHAIYN
a.out
cs.gif
inet.h
io
iomux.cpp
report.html
tc
tcpc
tcps
temp
tfc
tfs
ts
uc
us

10.20.14.25 : hello
what was that !?
10.20.14.25 :

As we can see , the client display a prompt with the IP number of the server. We can issue commands to the server and get the corresponding
output.

3. Output from the IOMultiplexed TCP/UDP Server

Waiting for a client...


UDP : Received from 24.167.4.8 :

Waiting for a client...


UDP : Received from 24.167.4.8 : ashish

Waiting for a client...


UDP : Received from 24.167.4.8 : hello

Waiting for a client...


Waiting for a client...
TCP : Received from 127.0.0.1 : tcp client

TCP : Received from 127.0.0.1 : how are wyou

The above server displays what type of protocol a client is using to communicate and displays the messages received.

http://www.cs.northwestern.edu/~agupta/_projects/networking/TCPClientServer/report.html 4/4

You might also like