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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:13:12+00:00 2026-05-26T03:13:12+00:00

I am trying to write a server that accepts files and write it in

  • 0

I am trying to write a server that accepts files and write it in certain directory using DataInputStream and BufferedInputStream.

The server gets ‘user name(string)’ ‘number of files(int)’ ‘file name(string)’ ‘size of each file(long)’ and ‘contents of file which is uninterpreted bytes(byte[])’

and if everything is successful then, I am supposed to send boolean value.

But the problem is that it is not receiving file correctly.

From time to time I get ‘broken pipe’ error message or the file is corrupted after I receive.

I looked at my code for 4hrs and couldn’t find the problem.

Would you please help me about this?
You can assume that client is working fine.

  • 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-26T03:13:13+00:00Added an answer on May 26, 2026 at 3:13 am

    FIrst you don’t have to close all of those streams. That’s probably why you’re seeing the broken pipe problem. You just need to close the input and output stream.

    DataInputStream din = new DataInputStream( new BufferedInputStream( socket.getInputStream() ) );
    DataOutputStream dout = new DateOutputStream( new BufferedOutputStream( socket.getOutputStream() );
    try {
    
    
    } finally {
       din.close();
       dout.close();
    }
    

    The reason you don’t have to close all of those streams is because your Buffered*Streams and socket InputStream/OutStream will be closed when din/dout.close() is called. Those will close the streams through the reference they chain to. You can also get rid of all that if( blah != null ) junk on each of those because if you make it to the finally clause you know they are non-null. You know that’s the case if you don’t new up inside the try.

    You’re also leaking your FileOutputStream because you overwrote the fos variable with the second new FileOutputStream(). What are you doing there with the SUBMIT_DONE file? That’s truly weird. It’s pretty bad idea to do that. Don’t use variable references twice like that. I would probably close the first file after your loop. Think about wrapping that loop with try {} finally { fos.close(); }.

    And you might try using methods to break this up a little. Ditch the static.

    Update:

    What exactly do you think the following is doing?

    while(c!='\0') {
       userName += c;
       c = din.readChar();
    }
    

    Depending on how you are sending the data from your client or server you could just use:

    String userName = din.readUTF();
    

    Remember with DataInputStream you are processing formatted BINARY data. You also have that exact loop code repeated again for the filenames. If you can’t use readUTF() then create a method that wraps up that loop and returns a string and call it from those two places. You have all sorts of security issues allowing clients to upload raw filenames and files to you. I hope to sweet baby jeez this server you’re building isn’t being deployed in production.

    You also need to flush and close each file you receive over the socket so the full amount of data sent is completely written to the files.

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

Sidebar

Related Questions

I'm trying to write a script that will download files from an FTP server.
I'm trying to write an insert statement for a SQL Server table that inserts
I'm trying to write a simple server that will grab an mp3 file from
I am (trying to) write a server-side daemon in c, and it accepts connections
Right now I'm trying to serve CSS and JS files from a server that
I'm trying to write an XML over HTTP push notification consumer that accepts the
I'm trying to write a simple HTTP web server using c, but I keep
I am trying to write an SSL client that sends mail using the javax.mail
I am trying to write a small program, that opens a server, creates a
I am trying to write a server application that listens to both IPv6 and

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.