Lab 14 - Communication Using TCP Protocol
Lab 14 - Communication Using TCP Protocol
Lab-14
Communication using TCP protocol
Lab 14: Communication using TCP Protocol
Table of Contents
1. Introduction 146
8. Evaluation Task (Unseen) [Expected time = 55mins for two tasks] 153
1. Introduction
This lab is the continuation of the previous lab in which you have developed a network
messenger using the UDP protocol. In this lab you will make a messenger using a more reliable
protocol which is a TCP protocol.
UDP protocol was a connection less protocol in which there were no acknowledgements for the
data delivery. On the other hand TCP which is a transmission control protocol provides
reliability using a three way hand shake. It also provides error-checked data delivery. In case
there is an error in the data then it informs the sender and asks for the retransmission. If we have
a critical system where delivery of data and acknowledgement of delivery is important then we
use TCP protocol. Figure 1 shows how the three way handshake occurs to establish a connection
using TCP protocol. The three way hand shake works like we make a phone call in the real
world. First we dial a number then the phone rings at the other end. The other person picks the
phone and after salutations we start talking; in other words we start sending and receiving the
data. In this example the person calling becomes the client and the person receiving the call and
answering the call becomes a server.
In this lab you will learn the client server architecture. You will also learn the types of specific
exceptions associated with the TCP protocol and how to handle these exceptions to make a
robust program.
4. Concept Map
4.2 Socket
A single system has two types of ports. One type is hardware ports which we can see physically
and the other type is logical ports through which the system sends and receives data on the
network. The total number of logical ports is 64K. Socket is an interface provided by Java which
helps the Java programs to communicate with the ports. In the client server architecture sockets
manage the end to end communication between the systems on the network.
Since servers provide service on a dedicated port, therefore it is difficult to serve all the clients
simultaneously. Java has provided an alternate to this problem. Java has provided a special
socket for the server side which is known as ServerSocket. This is a special socket because it
binds to a static dedicated port and as soon as it receives the request from the client, it finds the
empty port form the 64K ports it has and redirects the client to that new port. All the later
communication with the client is handled using the new port and the ServerSocket becomes
available to receive request from the new clients.
Following are some well know dedicated ports which are already binded with some system
services. You cannot use these ports to provide your services.
You must solve the following problems at home before the lab.
After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you.
What are the unicast and multicast addresses? Write the details on a paper and submit it.
List down the common exceptions that can occur while using Socket and ServerSocket. How can
we resolve these common exceptions?
5.2.1 Task-1
Write a pseudo code for a server which is used to share data like the USB share drive in M.A.J.U. You will
have to identify all its features and how the server will manage the files.
6. Procedure& Tools
In this section you will study how to send and receive data between two systems using TCP
Sockets.
6.1 Tools
This task is designed to guide you towards implementing a network based communication
solution using TCP protocol. There are two steps involved to achieve the communication. One is
writing a server and the other is writing a client to generate a request.
1. Create a socket by calling the socket() method. The system will find an open logical port
which is free and will bind that port to the socket.
2. Provide the address of server and dedicated port. Connect the socket to the server using the
given address and connect() method.
3. Use the streams to send and receive data. Reading and writing on the stream is the same as
was done in File Handling Lab.
import java.io.*;
import java.net.*;
import java.util.logging.*;
import javax.swing.JOptionPane;
InputStream is;
is = s.getInputStream();
OutputStream os=s.getOutputStream();
PrintWriter pw=new PrintWriter(os, true);
String message =JOptionPane.showInputDialog("Enter name");
pw.println(message);
s.close();
}
Writing a server is just like writing a client with some minor changes.
1. Create a socket which will finally communicate with the client. This socket is exactly like the
client socket which is created in the previous step.
2. Bind the ServerSocket to an address. For a ServerSocket, address consists of two things,
dedicated port and host IP address.
5. Use the streams to send and receive data. Reading and writing on the stream is the same as
was done in File Handling Lab.
6. The last step is to close the connection.
import java.io.*;
import java.net.*;
while(true){
Socket s=ss.accept();
System.out.println("connection request recieved");
InputStream is;
is = s.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br=new BufferedReader (isr);
OutputStream os=s.getOutputStream();
PrintWriter pw=new PrintWriter(os, true);
String name=br.readLine();
String message ="Hello"+name+"from server";
System.out.println(message);
s.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
To run the program you will have to open two separate consoles. The server must run first and
later you should run the client. The number of clients can be as many as you want.
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$\Advanced Computer Programming\Lab12
Write a program which receives a file name from the user and the client send that file to server
where server with the client ID. Later when the client asks for the stored file the server should
send a list back to the client consisting of its particular files only. Client should also be able to
ask for the previous version of stored file.
Bonus Practice Task:
Create a program which takes the input as a file name having '.jpg' extension from user; your
task is to throw that image file on network and save it on the system (server) where you receive
it.
After completing this lab, student will be able to do communication on the network using TCP
protocol.
7.4 Testing
This section provides you the test cases to test the working of your program. If you get the
desired mentioned outputs for the given set of inputs then your program is right.
Since the task-2 behavior is different therefor,its test cases will be explained/provided by the lab
instructor at the runtime.
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
10.1 Books
Text Book:
Java: How to Program by Paul J. Deitel, Harvey M. Deitel. Eighth Edition
Java Beginners Guide: http://www.oracle.com/events/global/en/java-outreach/resources/java-a-
beginners-guide-1720064.pdf
http://exampledepot.8waytrips.com/ for the package by package examples
www.youtube.com/watch?v=NvfJ0I3nfW4 for step by step video tutorial
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\