Java program for the Implementation of FTP Protocol - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Wednesday, October 12, 2011

Java program for the Implementation of FTP Protocol

Java program for the Implementation of FTP Protocol


Java program for the Implementation of FTP Protocol



import java.io.*;
import java.net.*;
import java.util.*;
public class TFTP{
            public static int serverPort=69;
            public static DatagramSocket ds;
            public static byte WRQ=2;
            public static byte DATA=3;
            public static byte ACK=4;

            public static void TheServer()throws Exception
            {
            byte b[]=new byte[1024];
            DatagramPacket p;
            String filename="";
            byte opcode;
            byte value;
            byte blockno=-1;
            boolean flag=false;
            FileOutputStream f=null;
            byte databuf[];
            System.out.println("********This is TFTP Server*********");
            while(true){
                        for(int i=0;i<1024;i++)
                        b[i]=0;
                        p=new DatagramPacket(b,b.length);
                        ds.receive(p);
                        opcode=b[0];
                        value=b[1];
                        if(opcode==WRQ)
                        {
                                    filename=new String(b,1,p.getLength()-1);
                                    System.out.println("Got Write Request Filename="+filename+"mode=netascii");
                                    f=new FileOutputStream(filename);
                                    blockno=0;
                        }
                        else if(opcode==DATA)
                        {
                                    f.write(b,2,p.getLength()-2);
                                    f.flush();
                                    blockno=value;
                                    System.out.println("length="+(p.getLength()-2)+"blockno="+blockno);
                                    if((p.getLength()-2)<512)
                                    flag=true;
                        }
                        databuf=new byte[2];
                        databuf[0]=ACK;
                        databuf[1]=blockno;
                        ds.send(new DatagramPacket(databuf,databuf.length,p.getAddress(),p,getPort()));
                        if(flag)
                        {

                                    System.out.println("Data Transfer is Complete");
                                    System.out.println("Saved in file\""+filename+"\"");
                                    f.close();
                                    break;
                        }
               }
            }

            public static void TheClient() throws Exception
            {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            byte b[]=new byte[1024];
            DtagramPacket p;
            FileInputStream f=null;
            String sourceFile,destFile;
            byte opcode;
            byte value;
            byte blockno=0;
            byte databuf[]=new byte[600];
            byte filedata[]=new byte[512];
            int n=0;
           
            System.out.println("********This is TFTP Client*********");
            System.out.println("Enter the Source filename for TFTP server:");
            sourceFile=br.readLine();
            System.out.println("Enterthe filename for TFTP server:");
           
            destFile=br.readLine();
            byte tmp[]=destFile.getBytes();
            databuf[0]=WRQ;
            for(int i=0;i<tmp.length;i++)
            databuf[i+1]=tmp[i];
            ds.send(new DatagramPacket(databuf,1+tmp.length,InetAddress.getLocalHost(),serverPort));
            f=new FileInputStream(sourceFile);
            while(true)
            {
                        for(int i=0;i<1024;i++)
                        b[i]=0;
                        p=new DatagramPacket(b,b.length);
                        ds.receive(p);
                        opcode=b[0];
                        value=b[1];
                        if(opcode==ACK)
                        {
                                    blockno=value;
                                    System.out.println("Got ack block no="+blockno);
                                    blockno++;
                                    n=f.read(filedata);
                                    if(n==-1)
                                    break;
                        }
                        databuf[0]=DATA;
                        databuf[1]=blockno;
                        for(int i=0;i<n;i++)
                        databuf[i+2]=filedata[i];
                        ds.send(new DatagramPacket(databuf,n+2,InetAddress.getLocalHost(),serverPort));
            }
    }



            public static void main(String args[]) throws Exception{
                        if(args.length==1)
                        {
                                    ds=new DatagramSocket();
                                    TheClient();
                        }
                        else
                        {
                                    ds=new DatagramSocket(serverPort);
                                    TheServer();
                        }
            }
}
Expected Output:
E:\gnec\test>java TFTP
********This is TFTP Server*********
Got Write Request Filename=s.txtmode=netascii
length=70blockno=1
Data Transfer is Complete
Saved in file"s.txt"

E:\gnec\test>java TFTP 0
********This is TFTP Client*********
Enter the Source filename for TFTP server:
IP.java
Enterthe filename for TFTP server:
s.txt
Got ack block no=0
Got ack block no=1
E:\gnec\test>java TFTP
********This is TFTP Server*********
Got Write Request Filename=s.txtmode=netascii
length=512blockno=1
length=512blockno=2
length=512blockno=3
length=512blockno=4
length=512blockno=5
length=512blockno=6
length=44blockno=7
Data Transfer is Complete
Saved in file"s.txt"

E:\gnec\test>java TFTP 1
********This is TFTP Client*********
Enter the Source filename for TFTP server:
IP.java
Enterthe filename for TFTP server:
s.txt
Got ack block no=0
Got ack block no=1
Got ack block no=2
Got ack block no=3
Got ack block no=4
Got ack block no=5
Got ack block no=6
Got ack block no=7

No comments:

Post a Comment