Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

LAB-3:

NAME: G RAM PRASAD REDDY


REGISTER NUMBER: 15MIS0384
COURSE CODE: SWE2002
COURSE NAME: COMPUTER NETWORKS
LAB SLOT: L37+38
1. Using UDP sockets, write a Java program to display the current
date and time.

Code:
At client side

import java.net.*;
import java.io.*;

public class Client {

public static void main(String[] args) throws Exception{

System.out.println("Server Time >>>>");

DatagramSocket cs=new DatagramSocket();

InetAddress ip=InetAddress.getByName("localhost");

byte[] rd=new byte[100];


byte[] sd=new byte[100];

DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,1234);

DatagramPacket rp=new DatagramPacket(rd,rd.length);

cs.send(sp);

cs.receive(rp);

String time=new String(rp.getData());

System.out.println(time);

cs.close();

}
}
At server side

import java.net.*;
import java.io.*;
import java.util.*;

public class Server {

public static void main(String[] args) throws Exception{


DatagramSocket ss=new DatagramSocket(1234);

while(true){

System.out.println("Server is up....");

byte[] rd=new byte[100];


byte[] sd=new byte[100];

DatagramPacket rp=new DatagramPacket(rd,rd.length);

ss.receive(rp);

InetAddress ip= rp.getAddress();

int port=rp.getPort();

Date d=new Date(); // getting system time

String time= d + ""; // converting it to String

sd=time.getBytes(); // converting that String to byte

DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,port);

ss.send(sp);

rp=null;

System.out.println("Done !! ");

}
Output:

At server side

At Client side

__________________________________________________________________________
2.Write a program to implement a simple message transfer from
client to server using UDP sockets.

Code:

At client side:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient
{
public static void main(String args[])
{
try
{
String server_address = "localhost";
int server_port = 1111;
String message = "Hello Vit University";

InetAddress address = InetAddress.getByName(server_address);

DatagramPacket packet = new DatagramPacket(message.getBytes(),


message.getBytes().length, address, server_port);

DatagramSocket socket = new DatagramSocket();


socket.send(packet);
System.out.println("UDPClient: Sent data to Server ; Message = " + message);
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error in sending the Data to UDP Server");
}
}
}
At server side
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPServer


{
public static void main(String args[])
{
int server_port = 1111;
System.out.println("UDP Server Listening in " + server_port);
try
{

DatagramSocket socket = new DatagramSocket(server_port);


byte[] msgBuffer = new byte[1024];

DatagramPacket packet = new DatagramPacket(msgBuffer, msgBuffer.length);

while (true)
{
socket.receive(packet);
String message = new String(msgBuffer, 0, packet.getLength());
System.out.println("UDPServer: Message received = " + message);
packet.setLength(msgBuffer.length);
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error in getting the Data from UDP Client");
}
}
}
Output

At client side

At server side
______________________________________________________________________________
3. Program to display an echoed message
Code:

At Client side

import java.net.*;
import java.util.*;
public class Client
{ public static void main( String args[] ) throws Exception {
InetAddress add = InetAddress.getByName("127.0.0.1");
DatagramSocket dsock = new DatagramSocket( );
String message1 = "This is client calling";
byte arr[] = message1.getBytes( );
DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
dsock.send(dpack); // send the packet
Date sendTime = new Date( ); // note the time of sending the message
dsock.receive(dpack); // receive the packet
String message2 = new String(dpack.getData( ));
Date receiveTime = new Date( ); // note the time of receiving the message
System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for "
+
message2);
}
}

At Server side
import java.net.*;
import java.util.*;
public class Server
{
public static void main( String args[]) throws Exception
{
DatagramSocket dsock = new DatagramSocket(7);
byte arr1[] = new byte[150];
DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
while(true)
{ dsock.receive(dpack);
byte arr2[] = dpack.getData();
int packSize = dpack.getLength();
String s2 = new String(arr2, 0, packSize);
System.out.println( new Date( ) + " " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
dsock.send(dpack);
}
}}
Output:

At client side

At server side
4.Write a program to implement a chat server and client using UDP
sockets.
Code:

At client side
import java.io.*;
import java.net.*;
class Client
{
public static DatagramSocket clientsocket;
public static DatagramPacket dp;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[] = new byte[1024];
public static int cport = 789, sport = 790;
public static void main(String[] a) throws IOException
{
clientsocket = new DatagramSocket(cport);
dp = new DatagramPacket(buf, buf.length);
dis = new BufferedReader(new
InputStreamReader(System.in));
ia = InetAddress.getLocalHost();
System.out.println("Client is Running... Type 'STOP'to Quit");
while(true)
{
String str = new String(dis.readLine());
buf = str.getBytes();
if(str.equals("STOP"))
{
System.out.println("Terminated...");
clientsocket.send(new
DatagramPacket(buf,str.length(), ia,
sport));
break;
}
clientsocket.send(new DatagramPacket(buf,
str.length(), ia, sport));
clientsocket.receive(dp);
String str2 = new String(dp.getData(), 0,
dp.getLength());
System.out.println("Server: " + str2);
}
}
}

At server side

import java.io.*;
import java.net.*;
class Server
{
public static DatagramSocket serversocket;
public static DatagramPacket dp;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[] = new byte[1024];
public static int cport = 789,sport=790;
public static void main(String[] a) throws IOException
{
serversocket = new DatagramSocket(sport);
dp = new DatagramPacket(buf,buf.length);
dis = new BufferedReader
(new InputStreamReader(System.in));
ia = InetAddress.getLocalHost();
System.out.println("Server is Running...");
while(true)
{
serversocket.receive(dp);
String str = new String(dp.getData(), 0,
dp.getLength());
if(str.equals("STOP"))
{
System.out.println("Terminated...");
break;
}
System.out.println("Client: " + str);
String str1 = new String(dis.readLine());
buf = str1.getBytes();
serversocket.send(new
DatagramPacket(buf,str1.length(), ia, cport));
}
}
}

Output:

Client Side:

Server Side:
5. The message entered in the client is sent to the server and the server encodes
the message and returns it to the client. Encoding is done by replacing a
character by the character next to it (i.e. a as b, b as c,….z as a). Write a Java
program to implement this using UDP sockets.

CODE:
Client Side:
import java.io.*;
import java.net.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

class a{
public static StringBuffer encrypt(String text, int s)
{
StringBuffer result= new StringBuffer();
for (int i=0; i<text.length(); i++)
{
if (Character.isUpperCase(text.charAt(i)))
{
char ch = (char)(((int)text.charAt(i) +
s - 65) % 26 + 65);
result.append(ch);
}
else
{
char ch = (char)(((int)text.charAt(i) +
s - 97) % 26 + 97);
result.append(ch);
}
}
return result;
}
}

public class UDPClient


{
public static void main(String args[])
{
try
{
String server_address = "localhost";
int server_port = 1111;
String message = "ATTACKATONCE";
System.out.println("Message Before Encoding is:" + message);
int t=1;
a o =new a();
StringBuffer res = o.encrypt(message,t);
String res1 = res.toString();

InetAddress address = InetAddress.getByName(server_address);

DatagramPacket packet = new DatagramPacket(res1.getBytes(),


res1.getBytes().length, address, server_port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
System.out.println("UDPClient: Sent data to Server ; Encoded Message = " + res1);
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error in sending the Data to UDP Server");
}
}
}

At Server Side:
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPServer


{
public static void main(String args[])
{
int server_port = 1111;
System.out.println("UDP Server Listening in " + server_port);
try
{

DatagramSocket socket = new DatagramSocket(server_port);


byte[] msgBuffer = new byte[1024];

DatagramPacket packet = new DatagramPacket(msgBuffer, msgBuffer.length);

while (true)
{
socket.receive(packet);
String message = new String(msgBuffer, 0, packet.getLength());
System.out.println("UDPServer:Encoded Message received = " + message);
packet.setLength(msgBuffer.length);
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error in getting the Data from UDP Client");
}
}
}

OUTPUT:
Client Side:

Server Side:

You might also like