Lab 13 - Socket Programming Using UDP Protocol
Lab 13 - Socket Programming Using UDP Protocol
Lab-13
Socket Programming using UDP Protocol
Lab 13: Socket Programming using UDP Protocol
Table of Contents
1 Introduction 137
8 Evaluation Task (Unseen) [Expected time = 55mins for two tasks] 145
1 Introduction
In this lab we are going to implement a User Datagram Protocol which is commonly known as
UDP. The TCP protocol will be studied in the next Lab.
UDP is a connection less protocol which means that to send and receive data connection is not
established. The data in UDP protocol is converted into packets then individual packets travel on
the network till they reach the destination. The UDP protocol is unreliable; which means that if a
packet is lost then it is gone for good, it won’t be retransmitted again.
UDP protocol is used in situations where the real-time communication is required. For example
in a CCTV camera setup the real-time communication is important therefore UDP protocol is
used. UDP based communication is comparatively fast as it does not generate acknowledgements
of the received packets.
Your task in this lab will be to implement the UDP protocol and test it.
4 Concept Map
Network programming involves two types of programs:
A server program is a program that supplies services to one or multiple users who run client
programs to get access to those services. These client and server computers communicate
through well-established protocols, which decide the nature of the communications between
them.
The World Wide Web uses Web servers that provide services. The clients in the www are
Web browsers, such as Internet Explorer or Google Chrome. The protocol used to
communicate between Web servers and browsers is called HTTP. For example when you try
to access www.jinnah.edu.pk in the browser then you use http protocol.
4.2 IP Address
To identify the system on the network we use a combination of a number with three dots. This
combination is composed or 4 bytes. The decimal value represented by these bytes is known as
IP address. For example the IP address of the University’s internet gateway is 172.16.0.26.
4.3 Ports
Besides an IP address, you must use a port to get access/data from a computer over the Internet.
For example, the port number for the HTTP is 80. When you access www.jinnah.edu.pk the
request is automatically sent to port 80. If the service is running then it generates a reply which
we see in our browser.
All the websites are mapped on an IP address. Since we cannot remember all the IP addresses so
it’s simple to map the IP addresses to the names. So the name on which the IP address of the
system is mapped is known as a Host Name. For example the Host name of the system that holds
the lectures is Dataserver which is also mapped on some IP address. You can access that system
with both name and IP address. The Domain name server is used to resolve the IP address using
the name we provide.
Every computer has a special host name and IP address that’s used to identify it to itself. It is
referred as localhost and IP is 127.0.0.1
InetAddress is the class which is used to hold and manipulate the IP address. It is the most
powerful class in terms of its features to manipulate IPs.
Example:
classInetAddressTest{
address = InetAddress.getByName("www.google.com");
System.out.println(address);
InetAddresssw[] = InetAddress.getAllByName("www.jinnah.edu.pk");
for(inti=0; i<sw.length; i++)
System.out.println(sw[i]);
}
}
This class is part of the java.net package, so any program that uses it must import either
java.net.InetAddress or java.net.*.
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.
5.2.1 Task-1
Create a digital clock on server that starts from the time set by the client. If client again changes
the time, clock starts from the new time.
.
6 Procedure& Tools
In this section you will study create a synchronized thread.
6.1 Tools
This task is designed to guide you towards implementing a network based communication
solution using UDP 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 connect request.
Final Code:
import java.net.*;
import java.io.*;
public class Server
{
public static void main(String [] args)throws Exception
{
String s;
byte [] data=new byte[64] ;
intportnumber=3000;
DatagramSocketsoc=new DatagramSocket(portnumber);
soc.setSoTimeout(10000);
DatagramPacketpac=new DatagramPacket(data,data.length);
booleanendOfCom=false;
do
{
try
{
soc.receive(pac);
data=pac.getData();
s=new String(data);
System.out.println(s);
} catch (InterruptedIOException e){
endOfCom = true;
} catch (IOExceptionio){/* err */ }
} while (! endOfCom);
}
}
Final Code:
import java.net.*;
importjava.net.InetAddress.*;
public class Client
{
public static void main(String [] args)throws Exception
{
String s="abcdefghijkl";
byte [] data=s.getBytes();
intportnumber=3000;
InetAddress add=InetAddress.getByName("127.0.0.1");
DatagramSocketsoc=new DatagramSocket();
DatagramPacketpac=new DatagramPacket(data,data.length,add,portnumber);
soc.send(pac);
}
}
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\Lab11
Write a program in which you have to send the data on network and receive that data and display
that data. Data can be composed of multiple messages.
Create five files in which you stored some data. The program takes the input as a file name from
user; your task is to read the data from a file and throw that data on network and save it in
another file on the system where you receive it.
Take the five numbers from user and send them through a network to the server. Your task is to
find count of the negative and positive numbers and send the final result back to the client. The
client displays the received data on the screen.
Take two numbers from user and send them through a network to the server. Your task is to
multiply,divide,add or subtract those numbers as specified by the client on the server and send
the final result back to the client. The client displays the received data on the screen.
After completing this lab, student will be able to understand the usage of UDP protocol and will
also be able to send and receive data between two systems using UDP protocol.
7.6 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.
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 Further Reading
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$\