/*
Exercise : Ex 2(b)
Title : UDP Receiver Program..
Author :Aslam Jainul
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class UdpReceiver
{
public static void main(String args[])throws IOException
{
try
{
DataInputStream dis=new DataInputStream(System.in);
byte[] rdata=new byte[1024];
DatagramSocket s=new DatagramSocket(9876);
DatagramPacket rpack=new DatagramPacket(rdata,rdata.length);
System.out.println("Waiting for file name:");
s.receive(rpack);
String fname=new String(rpack.getData());
System.out.println("From server : "+fname);
fname="TR_"+fname;
System.out.println("File name is : "+fname);
File f=new File(fname);
FileWriter fw=new FileWriter(f);
while(true)
{
byte[] rdata1=new byte[1024];
DatagramPacket rpack1=new DatagramPacket(rdata1,rdata1.length);
s.receive(rpack1);
String txt=new String(rpack1.getData());
fw.write(txt);
System.out.print(txt);
if(txt.trim().equals("done"))
{
System.out.println("Process finished");
fw.close();
break;
}
}
}
catch(IOException e)
{ System.out.println(""+e);
}
}
}
/*
Exercise : Ex 2(b)
Title : UDP Sender Program..
Author :Aslam Jainul
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class UdpSender
{
public static void main(String args[])
{
try
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the file name :");
String f=dis.readLine();
byte[] sdata=new byte[1024];
sdata=f.getBytes();
InetAddress ipa=InetAddress.getByName("127.0.0.1");
DatagramSocket s=new DatagramSocket();
DatagramPacket spack=new DatagramPacket(sdata,sdata.length,ipa,9876);
s.send(spack);
File f1= new File(f);
FileReader fr=new FileReader(f1);
int n=0;
byte[] buffer=new byte[1024];
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(f));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1)
{
String readData = String.valueOf(buf, 0, numRead);
System.out.print(readData);
buffer=readData.getBytes();
DatagramPacket spack1=new DatagramPacket(buffer,buffer.length,ipa,9876);
s.send(spack1);
}
byte[] endChar=new byte[1024];
endChar="done".getBytes();
spack=new DatagramPacket(endChar,endChar.length,ipa,9876);
s.send(spack);
}
catch(IOException e)
{
System.out.println(""+e);
}
}
}
Exercise : Ex 2(b)
Title : UDP Receiver Program..
Author :Aslam Jainul
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class UdpReceiver
{
public static void main(String args[])throws IOException
{
try
{
DataInputStream dis=new DataInputStream(System.in);
byte[] rdata=new byte[1024];
DatagramSocket s=new DatagramSocket(9876);
DatagramPacket rpack=new DatagramPacket(rdata,rdata.length);
System.out.println("Waiting for file name:");
s.receive(rpack);
String fname=new String(rpack.getData());
System.out.println("From server : "+fname);
fname="TR_"+fname;
System.out.println("File name is : "+fname);
File f=new File(fname);
FileWriter fw=new FileWriter(f);
while(true)
{
byte[] rdata1=new byte[1024];
DatagramPacket rpack1=new DatagramPacket(rdata1,rdata1.length);
s.receive(rpack1);
String txt=new String(rpack1.getData());
fw.write(txt);
System.out.print(txt);
if(txt.trim().equals("done"))
{
System.out.println("Process finished");
fw.close();
break;
}
}
}
catch(IOException e)
{ System.out.println(""+e);
}
}
}
/*
Exercise : Ex 2(b)
Title : UDP Sender Program..
Author :Aslam Jainul
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class UdpSender
{
public static void main(String args[])
{
try
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the file name :");
String f=dis.readLine();
byte[] sdata=new byte[1024];
sdata=f.getBytes();
InetAddress ipa=InetAddress.getByName("127.0.0.1");
DatagramSocket s=new DatagramSocket();
DatagramPacket spack=new DatagramPacket(sdata,sdata.length,ipa,9876);
s.send(spack);
File f1= new File(f);
FileReader fr=new FileReader(f1);
int n=0;
byte[] buffer=new byte[1024];
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(f));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1)
{
String readData = String.valueOf(buf, 0, numRead);
System.out.print(readData);
buffer=readData.getBytes();
DatagramPacket spack1=new DatagramPacket(buffer,buffer.length,ipa,9876);
s.send(spack1);
}
byte[] endChar=new byte[1024];
endChar="done".getBytes();
spack=new DatagramPacket(endChar,endChar.length,ipa,9876);
s.send(spack);
}
catch(IOException e)
{
System.out.println(""+e);
}
}
}