Socket Programming using UDP ---------------Create an application which transfer file form server to client using UDP.

Aim:- Create an application which transfer file form server to client using UDP.
Tools:- NetBeans IDE, jdk

Program:-

Server.java


package filetransfr;
import java.io.*;
import java.net.*;
public class Filetransfr {   
    public static void main(String[] args) throws Exception {       
        boolean flg;
        byte data[]=new byte[100];
        System.out.println("Server is Ready...");
        int cprt=777,sprt=888;
        InetAddress xn= InetAddress.getLocalHost();
        DatagramPacket dp=new DatagramPacket(data, data.length, xn, sprt);
        DatagramSocket ds=new DatagramSocket(cprt);
        ds.receive(dp);      
        ds.close();
        String fname=new String(dp.getData(),0,dp.getLength());
        System.out.println("Received File Name From Client : " + fname);
        File f=new File(fname);
        if(f.exists()) flg=true;
        else flg=false;
        String dt;
        if(flg==true)
            dt="yes";
        else
            dt="no";
        System.out.println("File Found : " + dt);
        data=dt.getBytes();
        dp=new DatagramPacket(data, data.length, xn, sprt);
        ds=new DatagramSocket(cprt);
        ds.send(dp);
        System.out.println("File Found Status Send To Client...");
        ds.close();
        if(flg==true){
            FileReader rd=new FileReader(f);
            BufferedReader br=new BufferedReader(rd);
            String str=null;
            System.out.println("Send File Data To Client ...");
            while((str=br.readLine())!=null){
                ds=new DatagramSocket(cprt);
                data=str.getBytes();
                dp=new DatagramPacket(data, data.length, xn, sprt);               
                ds.send(dp);
                ds.close();
            }           
        }
        else{
            System.out.println("File Found : " + dt);
        }       
      }
}

Client.java

package filetransfr;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;

public class clnt   {
    public  static void main(String args[]) throws Exception{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in) );
        System.out.print("Enter FileName : ");
        String fnm=br.readLine();       
        byte data[]=new byte[100];
        int cprt=777,sprt=888;
        InetAddress xn= InetAddress.getLocalHost();       
        data=fnm.getBytes();
        DatagramPacket dp=new DatagramPacket(data, data.length, xn, cprt);
        DatagramSocket ds=new DatagramSocket(sprt);
        ds.send(dp);
        System.out.println("Send File Name To Server...");
        ds.close();       
        dp=new DatagramPacket(data, data.length, xn, cprt);
        ds=new DatagramSocket(sprt);
        ds.receive(dp);
        String s=new String(dp.getData(),0,dp.getLength());
        System.out.println("Received File Found Status ...");
        System.out.println("File Found : " + s);       
        System.out.println("Received File Data From Server....");
        System.out.println("File Data Are :");
        while(true)
        {
            ds.receive(dp);
            s=new String(dp.getData(),0,dp.getLength());
            if(s==null)
            {
                break;
            }
            System.out.println(s);
        }       
    }   

}

Output:-


Post a Comment

0 Comments