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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:33:51+00:00 2026-05-23T13:33:51+00:00

My application is unable to transfer data properly over a socket connection and write

  • 0

My application is unable to transfer data properly over a socket connection and write it to a file properly. Files over about 65,535 bytes get corrupted and are no longer recognized by the programs designed to run them.

I have been able to send small .doc and .txt files successfully, but .mp3 .wmv .m4a .avi and just about anything else does not work. Neither do larger docs.

I have looked all over the internet for a solution to this problem. I have repeatedly tweaked the I/O code to fix the problem but it still doesn’t work! Here is the I/O code in the super class that handles sending and receiving files. If you need anymore information/other parts of code, let me know.

protected void sendFile() throws IOException {
    byte[] bytes = new byte[(int) file.length()];
    buffin = new BufferedInputStream(new FileInputStream(file));
    int bytesRead = buffin.read(bytes,0,bytes.length);
    System.out.println(bytesRead);
    out = sock.getOutputStream();
    out.write(bytes,0,fileBytes);
    out.flush();
    out.close();
}

protected void receiveFile() throws IOException {
    byte[] bytes = new byte[fileBytes];
    in = sock.getInputStream();
    for(int i=0;i<fileBytes;i++) {
        in.read(bytes);
    }
    fos = new FileOutputStream("/Datawire/"+fileName);
    buffout = new BufferedOutputStream(fos);
    buffout.write(bytes,0,fileBytes);
    buffout.flush();
    buffout.close();
}

UPDATED CODE (that works):

    protected void sendFile() throws IOException {
    if((file.length())<63000) {
        byte[] bytes = new byte[(int)file.length()];
        buffin = new BufferedInputStream(new FileInputStream(file));
        buffin.read(bytes,0,bytes.length);
        out = sock.getOutputStream();
        out.write(bytes,0,bytes.length);
        out.close();
    } else {
        byte[] bytes = new byte[32000];
        buffin = new BufferedInputStream(new FileInputStream(file));
        out = sock.getOutputStream();
        int bytesRead;
        while((bytesRead = buffin.read(bytes))>0) {
            out.write(bytes,0,bytesRead);
        }
        out.close();
    }
}

protected void receiveFile() throws IOException {
    if(fileBytes<63000) {
        byte[] bytes = new byte[32000];
        in = sock.getInputStream();
        System.out.println(in.available());
        in.read(bytes,0,fileBytes);
        fos = new FileOutputStream("/Datawire/"+fileName);
        buffout = new BufferedOutputStream(fos);
        buffout.write(bytes,0,bytes.length);
        buffout.close();
    } else {
        byte[] bytes = new byte[16000];
        in = sock.getInputStream();
        fos = new FileOutputStream("/Datawire/"+fileName);
        buffout = new BufferedOutputStream(fos);
        int bytesRead;
        while((bytesRead = in.read(bytes))>0) {
            buffout.write(bytes,0,bytesRead);
        }
        buffout.close();
    }
}
  • 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-23T13:33:52+00:00Added an answer on May 23, 2026 at 1:33 pm

    Your code is completely wrong. This is how to copy a stream in Java:

    int count;
    byte[] buffer = new byte[8192]; // more if you like but no need for it to be the entire file size
    while ((count = in.read(buffer)) > 0)
    {
      out.write(buffer, 0, count);
    }
    

    You should use this both when sending the file and when receiving the file. At present your sending method hopes that the entire file fits into memory; fits into INTEGER_MAX bytes; and is read in one chunk by the read method, without even checking the result. You can’t assume any of those things. Your receive method is complete rubbish: it just keeps overwriting the same array, again without checking any read() results.

    EDIT: Your revised code is just as bad, or worse. You are calling read() to check for EOS and then throwing that byte away, and then calling read() again and throwing away the read count it returns. You pointlessly have a different path for files < 64000, or 63000, or whatever it is, that has zero benefit except to give you two code paths to test, or possibly four, instead of one. The network only gives you 1460 bytes at a time at best anyway so what is the point? You already have (a) a BufferedInputStream with a default buffersize of 8192, and (b) my code that uses a byte[] buffer of any size you like. My code above works for any amount of data in two lines of executable code. Yours is 20. QED.

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

Sidebar

Related Questions

well I'm unable to write file if application Run auto at start-up but when
When I compile my application , I get following compile error. Unable to find
Disclaimer: I am unable to implement this properly in the application, as the application
Summary: memcpy seems unable to transfer over 2GB/sec on my system in a real
I am unable to transfer file using a smack, I am using android emulator
i am making an application which first compresses a file and then transfer it.
Im trying to create file transfer service over WCF, but i got some problems
On a .Net production server, I am unable to look at the Application trace.
I'm unable to debug a WinForms C# application using the released version of Visual
I am unable to create a Web Service Client in my NetBeans Web Application

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.