Socket Programming || Networks || Java

 The File Transfer Protocol  in Socket Programming is used to transfer files between two computers over a network and Internet. In this article we will look at how to work with  FTPclient. FTP Manager is an advanced FTP client that  transfers the file between your computer and the FTP server.

 

socket programming,java

 

 

Source Code:

File Server Main:

 import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

public class Main {

        public static void main(String[] args) throws IOException {

       

        ServerSocket s = new ServerSocket(9999);

        boolean server = true;

        while(server){

            Socket socket = s.accept();

            ClientThread ct = new ClientThread(socket);

            ct.runThread();

        }

                

    }

   }

File Server Client Thread:

import java.io.BufferedInputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

// Extend the Thread class.

public class ClientThread extends  Thread {

           

            //          You have to declare Socket and BufferedInputStream  as private variables.

    private Socket socket;

    private BufferedInputStream input;

   

    /*You will have a constructor which will assign the socket received

    from the Main to the socket of this thread.*/

    //There may be a methods in the ClientThread.java for running  connection.

    public ClientThread(Socket s) {

        socket = s;

    }

    //There may be a methods in the ClientThread.java for running  connection.

    void runThread() throws IOException{

        InputStream ins = socket.getInputStream();

        DataInputStream in = new DataInputStream(ins);

        String ip = in.readUTF();

        String filename= in.readUTF();

        System.out.println(“File named “+filename+” has been requested by “+ip);

        File folder = new File(“E:\\Server\\”);

        filename = “E:\\Server\\”+filename;

        File f = new File(filename);

        byte fileExists = 0;

        File[] list = folder.listFiles();

        for(int i=0; i<list.length; i++){

            if(list[i].isFile()){

                if(String.valueOf(filename).equals(String.valueOf(list[i]))){

                    fileExists=1;

                    break;

                }

            }

        }

        OutputStream out = socket.getOutputStream();

        DataOutputStream outs = new DataOutputStream(out);  

        outs.write(fileExists);

        if(fileExists==0)

            closeConection();

        else{

            int l = (int)f.length();

            byte[] total = new byte[l];

            input = new BufferedInputStream(new FileInputStream(f));

            input.read(total);

            byte[] a1 = new byte[1024];

            int length = 0;

            int it = l/1024+1;

            outs.writeUTF(“”+it);

            outs.writeUTF(String.valueOf(l));

            for(int i=0; i<it; i++){

                for(int j=0;j<1024; j++,length++){

                    if(length>l-1)

                        break;

                    a1[j] = total[length]; 

                }

                outs.write(a1);

            }

        }

    }

   

    //method in the ClientThread.java for closing the connection

    void closeConection() throws IOException{      

        socket.close();

    }

   

}

File Client Main:

 

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.Socket;

import java.util.Scanner;

public class Main {

  

    public static void main(String[] args) throws IOException {

        // TODO code application logic here

           

        InputStreamReader isr;

        BufferedReader br;

        Socket socket = new Socket(“localhost”,9999);

        OutputStream output = socket.getOutputStream();

        DataOutputStream dos = new DataOutputStream(output);

        System.out.println(“Enter a valid  ip address”);

        Scanner in = new Scanner(System.in);

        String ip = in.nextLine();

        dos.writeUTF(ip);

        System.out.println(“Enter file name to downlaod”);

        String fileName = in.nextLine();

        dos.writeUTF(fileName);

       //You will have an InputStreamReader for taking input from the user.

       

        InputStream input = socket.getInputStream();

        DataInputStream dis = new DataInputStream(input);

        int fExists = dis.read();

        if(fExists == 0){

            System.out.println(“File “+fileName+” is not present in Server’s director

y”);

            System.exit(0);

        }

        System.out.println(“Your file is Downloading  …”);

        int it = Integer.valueOf(dis.readUTF());

        int count = Integer.valueOf(dis.readUTF());

        byte a1[] = new byte[1024];

        byte[] tArray = new byte[count];

        int k =0;

        for(int i=0; i<it; i++){

            dis.read(a1);

            for(int j=0; j<1024; j++,k++){

                if(k>count-1)

                    break;

                tArray[k] = a1[j];

            }

        }

        File nFile = new File(“E:\\”+fileName);

        OutputStream out = new FileOutputStream(nFile);

        out.write(tArray);

        System.out.println(“File “+fileName+” has been sucessfuly downloaded”);

    }

   

}

Add a Comment

Your email address will not be published. Required fields are marked *