You are on page 1of 10

DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

Practical Number: 17

Name – Abhishek Farande

Roll No-54

Q. Execute the following program and write the output.

Program Code-

Sender Side-

import java.net.*;

public class DatagramSender {

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

DatagramSocket ds=new DatagramSocket();

String str="java is easy!!!";

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

DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),ip,3000);

ds.send(dp);

ds.close();

Receiver Side-

import java.net.*;

public class DatagramRec {

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

DatagramSocket ds =new DatagramSocket(3000);

byte[] buf=new byte[1024];

DatagramPacket dp = new DatagramPacket(buf,1024);

ds.receive(dp);

String str=new String(dp.getData(),0, dp.getLength());

System.out.println(str);

Created by Abhishek Appaso Farande Page 1


DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

ds.close();

OUTPUT-

Created by Abhishek Appaso Farande Page 2


DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

Name – Abhishek Farande

Roll No-54

Q. Write a program using DatagramPacket and DatagramSocket to create chat


application.

Program Code:

Server Side-

import java.net.*;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class ServerAbhi

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

DatagramSocket ds = new DatagramSocket(2019);

byte[] receiveData = new byte[512];

byte[] sendData = new byte[512];

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in)

);

System.out.println(" Abhi`s Server Socket is created, waiting for client ");

do

Created by Abhishek Appaso Farande Page 3


DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

DatagramPacket receiveDP = new DatagramPacket(receiveData,receiveData.length);

ds.receive(receiveDP);

String clientMessage = new String(receiveDP.getData(),0,receiveDP.getLength());

System.out.println("Client Message:"+clientMessage);

InetAddress ip = receiveDP.getAddress();

System.out.print("\n\nEnter Server Message:");

String serverMessage = br.readLine();

sendData = serverMessage.getBytes();

DatagramPacket sendDP = new DatagramPacket(sendData, sendData.length, ip,


receiveDP.getPort());

ds.send(sendDP);

receiveData = new byte[512];

}while(true);

Client Side-

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.*;

public class ClientAbhi

Created by Abhishek Appaso Farande Page 4


DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

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

DatagramSocket ds = new DatagramSocket();

byte[] receiveData = new byte[512];

byte[] sendData = new byte[512];

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in)

);

System.out.println(" UDP Client Socket is created, waiting for server ");

InetAddress ip = InetAddress.getLocalHost();

do

System.out.print("\nEnter Client Message:");

String clientMessage = br.readLine();

sendData = clientMessage.getBytes();

DatagramPacket sendDP = new DatagramPacket(sendData, sendData.length, ip,


2019);

ds.send(sendDP);

DatagramPacket receiveDP = new DatagramPacket(receiveData,receiveData.length);

Created by Abhishek Appaso Farande Page 5


DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

ds.receive(receiveDP);

String serverMessage = new String(receiveDP.getData(),0,receiveDP.getLength());

System.out.println("\n\nServer Message:"+serverMessage);

}while(true);

OUTPUT-

Server Side-

Client Side-

Created by Abhishek Appaso Farande Page 6


DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

Name – Abhishek Farande

Roll No-54

Q. Write a program using DatagramPacket and DatagramSocket to copy the contents


of one file into other.

Program Code:

Server Side-

import java.net.*;

import java.io.*;

public class ServerFile

public static void main(String args[])throws IOException

byte b[]=new byte[3072];

DatagramSocket dsoc=new DatagramSocket(2019);

FileOutputStream f=new FileOutputStream("C:\\Users\\Abhishek Farande\\OneDrive\\Desktop\\advance


java\\Programs\\Abhi.txt");

DatagramPacket dp=new DatagramPacket(b,b.length);

dsoc.receive(dp);

String data = new String(dp.getData(),0,dp.getLength());

f.write(data.getBytes(), 0, data.length());

Client Side-

import java.net.*;

import java.io.*;

public class ClientFile

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

byte b[] = new byte[1024];

Created by Abhishek Appaso Farande Page 7


DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

FileInputStream f = new FileInputStream("C:\\Users\\Abhishek Farande\\OneDrive\\Desktop\\advance


java\\Programs\\adi.txt");

DatagramSocket dsoc = new DatagramSocket();

int i=0;

while(f.available() != 0)

b[i]=(byte)f.read();

i++;

f.close();

dsoc.send(new DatagramPacket(b,i,InetAddress.getLocalHost(),2019));

OUTPUT-

The file “Adi.txt`s” content is copied into file “Abhi.txt”

Created by Abhishek Appaso Farande Page 8


DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

Name – Abhishek Farande

Roll No-54

Q. Write a program using DatagramPacket and DatagramSocket to transfer the file


from one location to the another.

Program Code:

Server Side-

import java.net.*;

import java.io.*;

public class ServerT {

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

byte b[] = new byte[3072];

DatagramSocket dsoc = new DatagramSocket(1000);

FileOutputStream f = new FileOutputStream("C:\\Users\\Abhishek


Farande\\OneDrive\\Desktop\\advance java\\Programs\\Abhi.txt");

while (true) {

DatagramPacket dp = new DatagramPacket(b, b.length);

dsoc.receive(dp);

System.out.println(new String(dp.getData(), 0, dp.getLength()));

Client Side-

import java.net.*;

import java.io.*;

public class ClientT

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

Created by Abhishek Appaso Farande Page 9


DKTE’s Yashwantrao Chavan Polytechnic,Ichalkaranji.

byte b[]=new byte[1024];

FileInputStream f=new FileInputStream("C:\\Users\\Abhishek


Farande\\OneDrive\\Desktop\\advance java\\adi.txt");

DatagramSocket dsoc=new DatagramSocket(2000);

int i=0;

while(f.available()!=0)

b[i]=(byte)f.read();

i++;

f.close();

dsoc.send(new DatagramPacket(b,i,InetAddress.getLocalHost(),1000));

OUTPUT-

Created by Abhishek Appaso Farande Page 10

You might also like