Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5943213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:21:46+00:00 2026-05-22T16:21:46+00:00

I want to send a file through a socket. The server is sitting on

  • 0

I want to send a file through a socket. The “server” is sitting on another computer than mine and the client in another computer as well. The file can go to and fro server and client but only in their current directory. The approach i have taken up to now is by using a file input stream and writing the file on a file output stream neverthelees this doesnt work as far as i understand.. Is there another way to send files through sockets?

Here is my code what could be wrong here?

public class Copy {

    private ListDirectory dir;
    private Socket socket;

    public Copy(Socket socket, ListDirectory dir) {
        this.dir = dir;
        this.socket = socket;
    }

    public String getCopyPath(String file) throws Exception {
        String path = dir.getCurrentPath();
        path += "\\" + file;
        return path;

    }

    public void copyFileToClient(String file, String destinationPath) 
    throws Exception {

        byte[] receivedData = new byte[8192];
        BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(getCopyPath(file)));
        String findDot = file;
        String extension = "";

        for (int i = 0; i < findDot.length(); i++) {
            String dot = findDot.substring(i, i + 1);
            if (dot.equals(".")) {
                extension = findDot.substring(i + 1);
            }
        }
        if (extension.equals("")) {
            extension = "txt";
        }
        BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(destinationPath + "\\"
                + "THECOPY" + "." + extension)));

        int len;

        while ((len = bis.read(receivedData)) > 0) {

            bos.write(receivedData, 0, len);
        }
        //      in.close();
        bis.close();
        //      output.close();
        bos.close();

    }

    //  public static void main(String args[]) throws Exception {
    //      Copy copy = new Copy();
    //      System.out.print(copy.getCopyPath("a"));
    //  }

}

And some client code :

...

DataOutputStream outToServer = new DataOutputStream(
        clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(
        new InputStreamReader(clientSocket.getInputStream()));
boolean exit = false;

else if (sentence.length() > 3 && sentence.substring(0, 3).equals("get")) {
        String currPath = dir.getCurrentPath();
        outToServer.writeBytes(sentence + "_" + currPath + "\n");

} else {

...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:21:47+00:00Added an answer on May 22, 2026 at 4:21 pm

    Your copyFileToClient method uses a FileInputStream and FileOutputStream directly, i.e. it does not transfer anything to/from the client at all, only from one local file to another. This is fine if you want to manage remotely files on the server, but does not help for sending data between different computers.

    You have to somehow send the data through the OutputStream/InputStream of the Socket – i.e. use the FileInputStream at the sending side and the FileOutputStream at the receiving one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to send a zip file through SOAP (from a SOAP client to
I want to send a xml file to my wcf service , how can
I want to send files as well as some other information through sockets. I
im trying to send a file in android through sockets. i want to add
I want to send file chunks to server async to server (MVC action in
I have a text file that I want to send over the network, this
I want to use the TcpClient and TcpListener to send an mp3 file over
For testing purposes I want send mail to my localhost user account rather than
i want to develop a pretty basic client-server program. one software reads xml (or
I have a problem with a function. I want to send bytes to through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.