0% found this document useful (0 votes)
18 views

Practical No. 17

Uploaded by

harshdpatil677
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Practical No. 17

Uploaded by

harshdpatil677
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No.

17
Main program O/P: Java is Easy!!!

Exercise Q.1
import java.net.*; import java.net.*;
import java.util.Scanner; import java.util.Scanner;

public class DgramChat1 { public class DgramChat2 {


public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(); DatagramSocket ds = new DatagramSocket();
DatagramSocket ds1 = new DatagramSocket(92); DatagramSocket ds1 = new DatagramSocket(93);
InetAddress ia = InetAddress.getByName("localhost"); InetAddress ia = InetAddress.getByName("localhost");
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
String str = "", str1 = ""; String str = "", str1 = "";
while (true) { while (true) {
// Sending // Sending
System.out.print("You:"); System.out.print("You:");
str = sc.nextLine(); str = sc.nextLine();
byte[] b = str.getBytes(); byte[] b = str.getBytes();
DatagramPacket dp = new DatagramPacket(b, DatagramPacket dp = new DatagramPacket(b,
b.length, ia, 93); b.length, ia, 92);
ds.send(dp); ds.send(dp);
// Receiving // Receiving
DatagramPacket dp1 = new DatagramPacket(new DatagramPacket dp1 = new DatagramPacket(new
byte[1024], 1024); byte[1024], 1024);
ds1.receive(dp1); ds1.receive(dp1);
str1 = new String(dp1.getData(), 0, str1 = new String(dp1.getData(), 0,
dp1.getLength()); dp1.getLength());
System.out.println("Friend: " + str1); System.out.println("Friend: " + str1);
} }
} }
} }

DgramChat1 O/P DgramChat2 O/P


Q.2
import java.io.*;
import java.util.Scanner;
public class CopyContent {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the file name to copy content from:");
String name1 = sc.nextLine();
File file1 = new File(name1);
if (!file1.exists()) {
System.out.println("Source file does not exist.");
return;
}
System.out.println("Enter the file name to copy content to:");
String name2 = sc.nextLine();
File file2 = new File(name2);
try (FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2)) {
byte[] buffer = new byte[1024];
int readByte;
while ((readByte = fis.read(buffer)) != -1) {
fos.write(buffer, 0, readByte);
}
System.out.println("File copied successfully.");
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}

O/P:
Q.3
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.Scanner;
public class FileRec {
public static void main(String[] args) throws Exception { public class FileSend {
DatagramSocket socket = new DatagramSocket(9876); public static void main(String[] args) throws Exception {
byte[] recbuf = new byte[1024]; Scanner sc = new Scanner(System.in);
System.out.println("Waiting to receive file..."); System.out.println("Enter the file name to send:");
FileOutputStream fos = new String name = sc.nextLine();
FileOutputStream("ReceivedFile.txt"); File file = new File(name);
while (true) { if (!file.exists()) {
DatagramPacket dp = new DatagramPacket(recbuf, System.out.println("File does not exist.");
recbuf.length); return;
socket.receive(dp); }
String receivedData = new String(packet.getData(), DatagramSocket socket = new DatagramSocket();
0, packet.getLength()); InetAddress ia = InetAddress.getByName("localhost");
if (receivedData.equals("END")) { byte[] sendbuf = new byte[1024];
System.out.println("File transfer completed."); FileInputStream fis = new FileInputStream(file);
break; int readbyte;
} while ((readbyte = fis.read(sendbuf)) != -1) {
fos.write(dp.getData(), 0, dp.getLength()); DatagramPacket dp = new
} DatagramPacket(sendbuf, sendbyte, ia, 92);
fos.close(); socket.send(dp);
socket.close(); }
} String endmsg = "END";
} DatagramPacket ldp = new
DatagramPacket(endmsg.getBytes(), endmsg.length(), ia,
92);
socket.send(ldp);
fis.close();
socket.close();
System.out.println("File sent successfully.");
}
}

File Receiver O/P File Sender O/P

You might also like