I am using macOS and writting 2 Java classes a client clase

I am using macOS and writting 2 Java classes, a client clase and a multi-threaded server class. Could you please help me to add a function of file transfer to these two class. For example I am plannig to transfer a \"1.pdf\" file from the folder \"/Users/XX/Destop\" to another folder \"/Users/XX/Downloads\" using the sokets:

//*** Server2.java

import java.io.*;
import java.net.*;

//********************
public class Server2
{
   static ServerSocket ServSock;
   static int QLen = 6;//cmd:java Server2 35000 6

   //***
   public static void main(String args[])
   {
try
{
   //*** establish server socket
   ServSock = new ServerSocket(Integer.parseInt(args[0]), QLen);

   while (true)
   {
   //*** accept a client
Socket Sock = ServSock.accept();
  
   //*** pass the client to a new thread
new Worker(Sock).start();
   }
}
catch(IOException e)
{System.out.println(e);}
   }
}





//*************************
class Worker extends Thread
{
   Socket Sock;
   PrintWriter PW;
   BufferedReader DIS;
   String Question = \"Which file?\";


   //**************
   Worker(Socket S)
   {Sock=S;}


   //**************
   public void run()
   {
try
{
//*** thread identifies itself
   System.out.println(\"Thread: \" + getName());


//*** set up socket I/O streams
   PW = new PrintWriter (new BufferedWriter (new OutputStreamWriter(Sock.getOutputStream())),true);
   DIS = new BufferedReader (new InputStreamReader (Sock.getInputStream()));

//*** send server question
   PW.println(Question);


//*** wait for client response
   String R = DIS.readLine();
   System.out.println(\"Client says: \" + R);


//*** close this socket connection
   Sock.close();
}

catch(IOException e)
{System.out.println(e);}
   }
}

////////

//*** Client1.java

import java.io.*;
import java.net.*;

public class Client1
{
   static Socket sock;
   static PrintWriter pw;
   static BufferedReader br;
   static String response;;

   //************************************
   public static void main(String args[]) throws IOException
   {
   //java Client1 localhost 35000
String[] a ={\"localhost\",\"35000\",\"1.pdf\"};//sim cmd
response = a[2];
   //java Client1 prog1.pdf
//args[0]=\"localhost\";
//args[1]=\"35000\";
//args[2]=\"prog1.pdf\";

   //*** establish connection to remote server
sock = new Socket(a[0], Integer.parseInt(a[1])); //*** provlocalhost 35000 prog1.pdfide server name & port
DataInputStream dis=null;//******add from online sample********//
dis = new DataInputStream(new BufferedInputStream(sock.getInputStream()));//******add from online sample********//
//*** set up socket I/O streams
pw = new PrintWriter (new BufferedWriter (new OutputStreamWriter(sock.getOutputStream())),true);
br = new BufferedReader (new InputStreamReader (sock.getInputStream()));
  
   //*** wait for server question
String r = br.readLine();
System.out.println(\"Server asks: \" + r);
  
   //*** respond to server
pw.println(response);
pw.flush();
  
   //*** close this socket connection
sock.close();

   }
}

Solution

hello there. You need to add following two functions : one in server to send file and other in client to receive file. File which you are sending must be in same folder in which you have your java ServerClass otherwise you have to provide full file path. You need to modify according to your question

ServerSocket ss = new ServerSocket(15123); //port number. you can change according to your port

                                      Socket st = ss.accept();

                                      System.out.println(\"Accepted connection : \" + st);

                                      File filetotransfer = new File (\"file1.pdf\");

                                      byte [] ba = new byte [(int)filetotransfer.length()];

                                      FileInputStream fin = new FileInputStream(filetotransfer);

                                      BufferedInputStream bin = new BufferedInputStream(fis);

                                      bin.read(ba,0,ba.length);

                                      OutputStream os = st.getOutputStream();

                                      System.out.println(\"Sending Files...\");

                                      os.write(ba,0,ba.length);

                                      os.flush();

                                      st.close();

                                      System.out.println(\"File transfer complete\");

               

Now add following code in your client class to receive file sent by server

                    int filelength=99999;

                    int br;

                    int ct = 0;

                    Socket clientsocket = new Socket(\"127.0.0.1\",15123); //server IP and port number

                    byte [] ba = new byte [filelength];

                    InputStream is = clientsocket.getInputStream();

                    FileOutputStream fos = new FileOutputStream(\"filesave.pdf\");

                    BufferedOutputStream bos = new BufferedOutputStream(fos);

                    br = is.read(ba,0,ba.length);

                    ct = br;

                    do {

                       br =is.read(ba, ct, (ba.length-ct));

                       if(br >= 0) ct += br;

                    } while(br > -1);

                    bos.write(ba, 0 , ct);

                    bos.flush();

                    bos.close();

                    clientsocket.close();

I am using macOS and writting 2 Java classes, a client clase and a multi-threaded server class. Could you please help me to add a function of file transfer to t
I am using macOS and writting 2 Java classes, a client clase and a multi-threaded server class. Could you please help me to add a function of file transfer to t
I am using macOS and writting 2 Java classes, a client clase and a multi-threaded server class. Could you please help me to add a function of file transfer to t
I am using macOS and writting 2 Java classes, a client clase and a multi-threaded server class. Could you please help me to add a function of file transfer to t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site