Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pracftical 17

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Practical 17

Program Code:
1. Execute the following Program and write the output.
 Server.java
import java.net.*;
public class DgramRec
{
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);
ds.close();
}
}
 Client.java
import java.net.*;
public class DGramSender
{
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();
}
}
OUTPUT:-
Practical 17

XIII. Exercise :
1. Write a program using DatagramPacket and DatagramSocket to create chat application.
 Server.java
import java.net.*;
public class ChatServer {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(12345)) {
byte[] buffer = new byte[1024];
System.out.println("Server is running...");
while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
System.out.println("Client: " + new String(packet.getData(), 0, packet.getLength()));
String response = "Message received!";
socket.send(new DatagramPacket(response.getBytes(), response.length(), packet.getAddress(),
packet.getPort()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
 Client.java
import java.net.*;
import java.util.Scanner;
public class ChatClient {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
Scanner scanner = new Scanner(System.in);
byte[] buffer;
while (true) {
System.out.print("Client: ");
String message = scanner.nextLine();
socket.send(new DatagramPacket(message.getBytes(), message.length(),
InetAddress.getByName("localhost"), 12345));
if (message.equalsIgnoreCase("exit")) break;
buffer = new byte[1024];
socket.receive(new DatagramPacket(buffer, buffer.length));
System.out.println("Server: " + new String(buffer).trim());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT:
Practical 17

2. Write a program using DatagramPacket and DataGramSocket to copy the contents of one file
into other.
 Server.java
import java.io.*;
import java.net.*;
public class FileCopyServer {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(12345);
FileOutputStream fos = new FileOutputStream("output.txt")) {
byte[] buffer = new byte[1024];
System.out.println("Server is running...");
while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
if (new String(packet.getData(), 0, packet.getLength()).equals("END")) break;
fos.write(packet.getData(), 0, packet.getLength());
}
System.out.println("File copied successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
 Client.java
import java.io.*;
import java.net.*;
public class FileCopyClient {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket();
FileInputStream fis = new FileInputStream("input.txt")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
socket.send(new DatagramPacket(buffer, bytesRead, InetAddress.getByName("localhost"),
12345));
}
socket.send(new DatagramPacket("END".getBytes(), 3, InetAddress.getByName("localhost"),
12345));
System.out.println("File sent successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT:-
Practical 17

3. Write a program using DatagramPacket and DatagramSocket to transfer the file from one
location to another.
 Server.java
import java.io.*;
import java.net.*;
public class FileTransferServer {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(12345);
FileOutputStream fos = new FileOutputStream("received_file.txt")) {
byte[] buffer = new byte[1024];
System.out.println("Server is running...");
while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
if (new String(packet.getData(), 0, packet.getLength()).equals("END")) break;
fos.write(packet.getData(), 0, packet.getLength());
}
System.out.println("File transferred!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
 Client.java
import java.io.*;
import java.net.*;
public class FileTransferClient {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket();
FileInputStream fis = new FileInputStream("source_file.txt")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
socket.send(new DatagramPacket(buffer, bytesRead, InetAddress.getByName("localhost"),
12345));
}
socket.send(new DatagramPacket("END".getBytes(), 3, InetAddress.getByName("localhost"),
12345));
System.out.println("File sent!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT:-

You might also like