0

I am able to send Files through socket and receive on other ends . Now i want to send even file name so that after receiving file and saving file from socket i can save the name of file. What to add in client to send filename and in server to receive filename thanks in advance to all

Client.java

try {

                clientSocket = new Socket(targetIP, port);
                os = clientSocket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);


                InputStream is = clientSocket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                signalActivity("About to start handshake");

                byte[] buffer = new byte[4096];

                FileInputStream fis = new FileInputStream(fileToSend);
                BufferedInputStream bis = new BufferedInputStream(fis);
                // long BytesToSend = fileToSend.length();

                while(true)
                {

                    int bytesRead = bis.read(buffer, 0, buffer.length);

                    if(bytesRead == -1)
                    {
                        break;
                    }

                    //BytesToSend = BytesToSend - bytesRead;
                    os.write(buffer,0, bytesRead);
                    os.flush();
                }
                fis.close();
                bis.close();

                br.close();
                isr.close();
                is.close();

                pw.close();
                os.close();

                clientSocket.close();


            } catch (IOException e) {
            }
            catch(Exception e)
            {

            }

Server.java

try {



            welcomeSocket = new ServerSocket(port);

            while(true && serviceEnabled)
            {

                socket = welcomeSocket.accept();

                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);


                String inputData = "";

//              String savedAs = "WDFL_File_" + System.currentTimeMillis();

//save the original name and extention

                File file = new File(saveLocation, savedAs);

                byte[] buffer = new byte[4096];
                int bytesRead;

                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos);

                while(true)
                {
                    bytesRead = is.read(buffer, 0, buffer.length);
                    if(bytesRead == -1)
                    {
                        break;
                    }
                    bos.write(buffer, 0, bytesRead);
                    bos.flush();

                }

                bos.close();
                socket.close();
                //Start writing to file

            }


        } catch (IOException e) {

        }
        catch(Exception e)
        {

        }
Neeraj
  • 1,769
  • 3
  • 24
  • 41
Payal
  • 9
  • 2

1 Answers1

0

Your client should send the file name first. After that the contents of the file.

The server should read the file name first so the following content of the file can be saved under the same file name.

greenapps
  • 11,154
  • 2
  • 16
  • 19