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 8878523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:43:56+00:00 2026-06-14T19:43:56+00:00

Well i am trying to transfer a file using sockets in java Here is

  • 0

Well i am trying to transfer a file using sockets in java

Here is the code

Client Code

try{
    // get streams
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    DataInputStream  din = new DataInputStream (socket.getInputStream());
    dos.writeUTF(fileName);
    dos.flush();

    boolean isOk = din.readBoolean();
    if(!isOk){
        throw new StocFileNotFound("Fisierul: " + fileName +" was not found on:" + address.toString());
    } else {
        baos = new ByteArrayOutputStream();
        byte biti [] = new byte[1024];

        while(din.read(biti,0,1024) != -1){
            baos.write(biti,0,biti.length);
        }
    }

}
catch(IOException e){}
finally {
    try{ socket.close(); } catch (IOException  e){}
}

and then I return the baos.toByteArray() and write it to a file with the OutputStream`s write method.

Server code

try{
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    DataInputStream  din = new DataInputStream (socket.getInputStream());

    // check if it is really a file or if it is an existing file
    File file = new File(din.readUTF());

    // write false
    if ( !file.exists() || !file.isFile() ){
        dos.writeBoolean(false);
        dos.flush();
    }

    // write true and write the file
    else {
        byte biti[] = new byte[1024];
        dos.writeBoolean(true);

        FileInputStream fis = new FileInputStream(file);

        while(fis.read(biti,0,1024) != -1){
            dos.write(biti,0,biti.length);
        }

        dos.flush();

        try{ fis.close(); } catch (IOException e){}

    }

} catch (IOException e){}
finally {
    try{socket.close();}catch(IOException e){}
}

The problem

When i transfer a .txt file and view it in gedit it shows the text followed by multiple \00\00\00, though when i open it using notepad(in wine) it shows only the text. Plus viewing images and .doc works also. So is it something with gedit or is it with my program?

Edit
i was sending something like “hi, hope it works!”

  • 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-06-14T19:43:56+00:00Added an answer on June 14, 2026 at 7:43 pm

    This is the problem (or at least a problem):

    while(fis.read(biti,0,1024) != -1)
    {
        dos.write(biti,0,biti.length);
    }
    

    You’re always writing out the whole buffer, however many bytes were actually read. You should have:

    int bytesRead;
    while ((bytesRead = fis.read(biti, 0, 1024)) != -1)
    {
        dos.write(biti, 0, bytesRead);
    }
    

    (You’ve got the same problem in both bits of code.)

    You might want to look at Guava which has various utility methods to relieve you of a lot of the tedium (and possible error) of writing this kind of code over and over again.

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

Sidebar

Related Questions

well iam trying to get version of IE used by user by using $.browser.version
hey well I'm trying to change the url structure using the htaccess file and
I'm trying to write a secure transfer file program using Python and AES and
Well the question says it all.I am trying to create a file transfer application
Well im trying to write my own custom control extender but i can't get
Well I am trying to learn the java MVC pattern, but I can't understand
Alright well I am trying to create a dictionary from a text file so
well, I am trying to include a header file in my project, while the
I'm trying to implement file upload functionality in the iPhone app. Server code is
I have a client-server program that uses sockets to transfer information in a stateful

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.