0% found this document useful (0 votes)
11 views4 pages

Practical 17

The document contains code for implementing UDP client-server applications in Java to transfer files and exchange messages. It includes code for a basic UDP client and server that can send and receive strings. It also includes code for UDP clients and servers that can transfer a file from the client to the server by breaking the file into datagram packets. The file transfer code uses DatagramSockets and DatagramPackets to send file bytes in packets from the client to the server, which then reconstructs the file by writing the received bytes to a file.

Uploaded by

krrishmalik74
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)
11 views4 pages

Practical 17

The document contains code for implementing UDP client-server applications in Java to transfer files and exchange messages. It includes code for a basic UDP client and server that can send and receive strings. It also includes code for UDP clients and servers that can transfer a file from the client to the server by breaking the file into datagram packets. The file transfer code uses DatagramSockets and DatagramPackets to send file bytes in packets from the client to the server, which then reconstructs the file by writing the received bytes to a file.

Uploaded by

krrishmalik74
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/ 4

PRACTICAL NO 17

import java.io.*;
import java.net.*;
public class UDPServer
{
public static void main(String[] args) throws IOException
{
DatagramSocket ds = new DatagramSocket(2019);
byte[] recByte = new byte[512];
byte[]sendByte = new byte[512];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" UDP Server Socket is created....");
do
{
DatagramPacket receiveDP = new DatagramPacket(recByte, recByte.length);
ds.receive(receiveDP);
String clientMessage = new String(receiveDP.getData(), 0, receiveDP.getLength());
if (clientMessage.equals("STOP"))
{
System.out.println("Terminated");
break;
}
System.out.println("Client Message:" + clientMessage);
InetAddress ip = receiveDP.getAddress();
System.out.print("\n\nEnter Server Message:");
String serverMessage = br.readLine();
sendByte = serverMessage.getBytes();
DatagramPacket sendDP = new DatagramPacket(sendByte, sendByte.length, ip,receiveDP.getPort());
ds.send(sendDP);
} while (true);
}
}

import java.io.*;
import java.net.*;
public class UDPClient
{
public static void main(String[] args) throws IOException
{
DatagramSocket ds = new DatagramSocket(5000);
byte[] recBytes = new byte[512];
byte[]sendBytes;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" UDP Client Socket is created... ");
InetAddress ip = InetAddress.getLocalHost();
do
{
System.out.print("\nEnter Client Message:");
String clientMessage = br.readLine();
sendBytes = clientMessage.getBytes();
if (clientMessage.equals("STOP"))
{
System.out.println("Terminated");
break;
}
DatagramPacket sendDP = new DatagramPacket(sendBytes, sendBytes.length, ip, 2019);
ds.send(sendDP);
DatagramPacket receiveDP = new DatagramPacket(recBytes, recBytes.length);
ds.receive(receiveDP);
String serverMessage = new String(receiveDP.getData(), 0, receiveDP.getLength());
System.out.println("\n\nServer Message:" + serverMessage);
} while (true);
ds.close();
}
}

import java.io.*;
import java.net.*;
public class FileUDPServer
{
public static void main(String[] args) throws Exception
{
byte[] b = new byte[1024];
DatagramSocket ds = new DatagramSocket(10000);
FileOutputStream f = new FileOutputStream("F://Adv Java//210302//out.txt");
while (true)
{
DatagramPacket dp = new DatagramPacket(b, b.length);
ds.receive(dp);
f.write(dp.getData(), 0, dp.getLength());
}
}
}

import java.io.*;
import java.net.*;
public class FileUDPClient
{
public static void main(String[] args) throws Exception
{
byte[] b = new byte[1024];
FileInputStream f = new FileInputStream("F://Adv Java//210302//in.txt");
DatagramSocket ds = new DatagramSocket();
int i = 0;
while (f.available() != 0)
{
b[i] = (byte) f.read();
i++;
}
f.close();
InetAddress serverAddress = InetAddress.getLocalHost();
ds.send(new DatagramPacket(b, i, serverAddress, 10000));
ds.close();
}
}

import java.io.*;
import java.net.*;
public class UDPFileTransferServer
{
public static void main(String[] args) throws IOException
{
DatagramSocket s = new DatagramSocket(2019);
byte[] r = new byte[1024];
while (true) {
DatagramPacket p = new DatagramPacket(r, r.length);
s.receive(p);
byte[] d = p.getData();
int l = p.getLength();
if (l == 0)
{
System.out.println("Terminated");
break;
}
try (FileOutputStream o = new FileOutputStream("out.txt"))
{
o.write(d, 0, l);
System.out.println("File received");
}
catch (IOException e)
{}
}
s.close();
}
}

import java.io.*;
import java.net.*;
public class UDPFileTransferClient
{
public static void main(String[] args) throws IOException
{
DatagramSocket s = new DatagramSocket();
byte[] r = new byte[1024];
InetAddress i = InetAddress.getLocalHost();
try (FileInputStream f = new FileInputStream("in.txt"))
{
byte[] d = new byte[f.available()];
f.read(d);
s.send(new DatagramPacket(d, d.length, i, 2019));
s.receive(new DatagramPacket(r, r.length));
System.out.println("Server: " + new String(r, 0, r.length));
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
s.close();
}
}
}

You might also like