You are on page 1of 11

Lab Manual for AdvanceComputer Programming

Lab-14
Communication using TCP protocol
Lab 14: Communication using TCP Protocol

Table of Contents
1. Introduction 146

2. Activity Time boxing 147

3. Objective of the experiment 147

4. Concept Map 147


4.1 Client Server Architecture 147
4.2 Socket 147
4.3 Server Socket 148
4.4 Common Ports 148

5. Homework before Lab 148


5.1 Problem Solution Modeling 148
5.1.1 Problem 1 description: 148
5.1.2 Problem 2 description: 148
5.2 Practices from home 149
5.2.1 Task-1 149

6. Procedure& Tools 149


6.1 Tools 149
6.2 Setting-up JDK 1.7 [Expected time = 5mins] 149
6.2.1 Compile a Program 149
6.2.2 Run a Program 149
6.3 Walkthrough Task[Expected time = 30mins] 149
6.3.1 Implementation of TCP Client 149
6.3.2 Implementation of TCP Server 150

7. Practice Tasks 152


7.1 Practice Task 1 [Expected time = 20mins] 152
7.2 Practice Task 2 [Expected time = 40mins] 152
7.3 Out comes 152
7.4 Testing 152

8. Evaluation Task (Unseen) [Expected time = 55mins for two tasks] 153

9. Evaluation criteria 153

10. Further Reading 153


10.1 Books 153
10.2 Slides 153

Department of Computer Science, Page 145


C.U.S.T.
Lab 14: Communication using TCP Protocol

Lab 14: Communication using TCP protocol

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.

Figure 1: TCP Handshake

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.

Department of Computer Science, Page 146


C.U.S.T.
Lab 14: Communication using TCP Protocol

Relevant Lecture Material

a) Revise Lecture No. 23 and 24


b) Text Book: Java: How to Program by Paul J. Deitel, Harvey M. Deitel
1. Read Chapter 19.

2. Activity Time boxing

Table 1: Activity Time Boxing


Task No. Activity Name Activity time Total Time
5.1 Evaluation of Design 20mins 20mins
6.2 Setting-upPath for JDK 5mins 5mins
6.3 Walkthrough Tasks 30mins 30mins
7 Practice tasks 30mins for each task approx 60mins
8 Evaluation Task 60mins for all assigned task 55mins

3. Objective of the experiment

 To understand the Client Server Architecture.


 To understand the scenarios where to use Server Sockets.
 Tobe able to communicate between multiple systems on the network using the TCP
protocol.

4. Concept Map

4.1 Client Server Architecture

Client server architecture represents a model of network communication between multiple


systems where each system connected to the network is either a client or a server. For example
when in the university you access “dataserver” then the system named “dataserver” acts as a
server and your system which is requesting the access to a specific folder acts as a client. In this
kind of architecture the client are dependent on the server for the resources. Technically the
client server architecture is also referred as a two tier architecture.

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.

4.3 Server Socket

Department of Computer Science, Page 147


C.U.S.T.
Lab 14: Communication using TCP Protocol

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.

4.4 Common Ports

Following are some well know dedicated ports which are already binded with some system
services. You cannot use these ports to provide your services.

Port Number Description


20 FTP -- Data
21 FTP -- Control
22 SSH Remote Login Protocol
23 Telnet
25 Simple Mail Transfer Protocol (SMTP)
53 Domain Name System (DNS)
80 HTTP
110 POP3

5. Homework before Lab

You must solve the following problems at home before the lab.

5.1 Problem Solution Modeling

After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you.

5.1.1 Problem 1 description:

What are the unicast and multicast addresses? Write the details on a paper and submit it.

5.1.2 Problem 2 description:

List down the common exceptions that can occur while using Socket and ServerSocket. How can
we resolve these common exceptions?

5.2 Practices from home


Solve the following subtasks.

Department of Computer Science, Page 148


C.U.S.T.
Lab 14: Communication using TCP Protocol

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

Java Development Kit (JDK) 1.7

6.2 Setting-up JDK 1.7 [Expected time = 5mins]

Refer to Lab 1 sec 6.2.

6.2.1 Compile a Program

Refer to Lab 1 sec 6.2 for details.

6.2.2 Run a Program

Refer to Lab 1 sec 6.2 for details.

6.3 Walkthrough Task[Expected time = 30mins]

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.

6.3.1 Implementation of TCP Client

Following Steps are performed to write a TCP client.

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.

Department of Computer Science, Page 149


C.U.S.T.
Lab 14: Communication using TCP Protocol

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.

4. After communication, the last step is to close the stream.

import java.io.*;
import java.net.*;
import java.util.logging.*;
import javax.swing.JOptionPane;

public class Client {

public static void main(String[] args) {


try
{
Socket s;
s = new Socket("localhost", 222);

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 message =JOptionPane.showInputDialog("Enter name");
pw.println(message);
s.close();
}

catch (UnknownHostException ex) {


Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}

6.3.2 Implementation of TCP Server

Writing a server is just like writing a client with some minor changes.

Department of Computer Science, Page 150


C.U.S.T.
Lab 14: Communication using TCP Protocol

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.

3. 3rd step is to start listening for connection request.

4. accept() method is used to accept the connections and return a simple socket to do


communication with the client. This call will wait till the time there is a request from the
client.

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.*;

public class HelloServer {


public static void main(String args[])
{
try
{
ServerSocket ss= new ServerSocket(222);
System.out.println("server started");

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();

Department of Computer Science, Page 151


C.U.S.T.
Lab 14: Communication using TCP Protocol

}
}
}

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

7.1 Practice Task 1 [Expected time = 20mins]


Create a program in which client sends his name and password to the server, server should verify
name and password, if it is correct server should return a welcome message, if it is not then error
message.
Hint: You can store some names with passwords in a text file.

7.2 Practice Task 2 [Expected time = 40mins]

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.

7.3 Out comes

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.

Test Cases for Practice Task-1

Department of Computer Science, Page 152


C.U.S.T.
Lab 14: Communication using TCP Protocol

Sample Input Sample Output


Ali Hello Ali!
Sana Hello Sana!
123 Hello 123!

Test Cases for Practice Task-2

Since the task-2 behavior is different therefor,its test cases will be explained/provided by the lab
instructor at the runtime.

8. Evaluation Task (Unseen) [Expected time = 55mins for two tasks]

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).

Table 3: Evaluation of the Lab


Sr. No. Task No Description Marks
1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7 Practice tasks and Testing 35
4 8 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10

10. Further Reading

This section provides the references to further polish your skills.

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$\

Department of Computer Science, Page 153


C.U.S.T.
Lab 14: Communication using TCP Protocol

Department of Computer Science, Page 154


C.U.S.T.

You might also like