Assignment 3 Lab
Assignment 3 Lab
Assignment 3 Lab
1. Write a program to implement a text based message transfer from client to server process using
UDP
Server
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
ds.receive(DpReceive);
System.out.println("Client:-" + data(receive));
if (data(receive).toString().equals("bye"))
{
System.out.println("Client sent bye.....EXITING");
break;
}
Client
import java.io.*;
import java.net.*;
import java.util.*;
InetAddress ip = InetAddress.getLocalHost();
byte buf[] = null;
while (true)
{
String inp = sc.nextLine();
buf = inp.getBytes();
DatagramPacket DpSend =
new DatagramPacket(buf, buf.length, ip, 1234);
ds.send(DpSend);
if (inp.equals("bye"))
break;
}
}
}
Output server
Output client
2. Write a program to implement a chat server and client in java using UDP sockets
Client code
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
Output client
3. Write a program to implement date server and client in java using UDP sockets
Server code
import java.net.*;
import java.io.*;
import java.util.Date;
public class three3server
{
public static void main(String[] args)throws IOException
{
ServerSocket ss= new ServerSocket(5000);
System.out.println("The Server has reserved port No.: "+ss.getLocalPort()+" for this S
ervice");
Socket cs=ss.accept();
System.out.println("Client with IP Address "+cs.getInetAddress()+" has communicated vi
a port No.: "+cs.getPort());
Date d=new Date();
String s="Current Date & Time on Server is:"+d;
PrintWriter toclient=new PrintWriter(cs.getOutputStream(),true);
toclient.print(s);
toclient.close();
cs.close();
ss.close();
}
}
Client code
import java.net.*;
import java.io.*;
public class three3client{
public static void main(String[] args) throws UnknownHostException,IOException {
Socket cs= new Socket("LocalHost",5000);
System.out.println("Client "+cs.getInetAddress()+" is communicating from port No.: "+c
s.getPort());
BufferedReader fromserver=new BufferedReader(new InputStreamReader(cs.getInputStream()
));
System.out.println(fromserver.readLine());
fromserver.close();
cs.close();
}
}
Output
Server
Client
4. Write a program to implement a DNS server and client in java using UDP sockets.
Server code
import java.io.*;
import java.net.*;
public class three4server {
private static int indexOf(String[] array, String str) {
str = str.trim();
for (int i = 0; i < array.length; i++) {
if (array[i].equals(str)) return i;
}
return -1;
}
public static void main(String arg[]) throws IOException {
String[] hosts = {
"zoho.com",
"gmail.com",
"google.com",
"facebook.com"
};
String[] ip = {
"172.28.251.59",
"172.217.11.5",
"172.217.11.14",
"31.13.71.36"
};
System.out.println("Press Ctrl + C to Quit");
while (true) {
DatagramSocket serversocket = new DatagramSocket(1362);
byte[] senddata = new byte[1021];
byte[] receivedata = new byte[1021];
DatagramPacket recvpack = new DatagramPacket(receivedata,
receivedata.length);
serversocket.receive(recvpack);
String sen = new String(recvpack.getData());
InetAddress ipaddress = recvpack.getAddress();
int port = recvpack.getPort();
String capsent;
System.out.println("Request for host " + sen);
if (indexOf(hosts, sen) != -1)
capsent = ip[indexOf(hosts, sen)];
else
capsent = "Host Not Found";
senddata = capsent.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,
senddata.length, ipaddress, port);
serversocket.send(pack);
serversocket.close();
}
}
}
Client code
import java.io.*;
import java.net.*;
public class three4client{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientsocket = new DatagramSocket();
InetAddress ipaddress;
if (args.length == 0)
ipaddress = InetAddress.getLocalHost();
else
ipaddress = InetAddress.getByName(args[0]);
byte[] senddata = new byte[1024];
byte[] receivedata = new byte[1024];
int portaddr = 1362;
System.out.print("Enter the hostname : ");
String sentence = br.readLine();
senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata, senddata.length, ipaddress, portadd
r);
clientsocket.send(pack);
DatagramPacket recvpack = new
DatagramPacket(receivedata, receivedata.length);
clientsocket.receive(recvpack);
String modified = new String(recvpack.getData());
System.out.println("IP Address: " + modified);
clientsocket.close();
}
}
Output
Server output
Client output
5. Write a UDP based server code to get the date of birth of the client and calculate the age
as on today. The client has to enter the year, month and day of birth. For example, if the date
of birth of a user is 1/07/2001 then his age is 14 years 0 months and 17 days if today's date is
18/07/2018. Get today's date from the server.
• Code for Date formatting
• Creating UDP sockets for server and client
• Code for validation of date format and computation of age for UDP transfer of segments.
Code client
import java.io.*;
import java.net.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Scanner;
public class udpClientEO {
public static void main(String args[]) {
try {
DatagramSocket ds = new DatagramSocket(1000);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
System.out.println("Enter your date of birth (dd-MM-yyyy): ");
String dob = sc.next();
byte b[] = new byte[1024];
b = dob.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getLocalHost(), 20
00);
ds.send(dp);
byte b1[] = new byte[1024];
DatagramPacket dp1 = new DatagramPacket(b1, b1.length);
ds.receive(dp1);
String str = new String(dp1.getData(), 0, dp1.getLength());
System.out.println("Your Age as calculated by the server is " + str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Code server
import java.io.*;
import java.net.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Scanner;
public class udpServerEO
{
public static void main(String args[]){
try{
DatagramSocket ds = new DatagramSocket(2000);
byte b[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(b,b.length);
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(str);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = formatter.parse(str);
Instant instant = date.toInstant();
ZonedDateTime zone = instant.atZone(ZoneId.systemDefault());
LocalDate givenDate = zone.toLocalDate();
Period period = Period.between(givenDate, LocalDate.now());
String s = period.getYears()+" years "+period.getMonths()+"months and "+period
.getDays()+" days";
byte b1[] = new byte[1024];
b1 = s.getBytes();
DatagramPacket dp1 = new
DatagramPacket(b1,b1.length,InetAddress.getLocalHost(),1000);
ds.send(dp1);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Output client
Output server