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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:07:19+00:00 2026-05-22T19:07:19+00:00

Hello there im trying to send files using client-server classes in java. For some

  • 0

Hello there im trying to send files using client-server classes in java. For some reason when the method that sends the file is called the socket closes. here is the code :

FileInputStream fIn = new FileInputStream(file);
out = new BufferedOutputStream(clientSocket.getOutputStream());
byte fileContent[] = new byte[(int) file.length()];
fIn.read(fileContent);
for (byte b : fileContent) {
    out.write(b);
}

and the code from the client :

FileOutputStream fIn = new FileOutputStream("testing");
BufferedInputStream inAout = new BufferedInputStream(clientSocket.getInputStream());
byte fileContent[] = new byte[1000000];
inAout.read(fileContent);
fIn.write(fileContent);

and the error message i get : SEVERE: null
java.net.SocketException: Socket closed

Im not really experienced with this so if any can help it would be great.

  • 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-22T19:07:20+00:00Added an answer on May 22, 2026 at 7:07 pm

    The InputStream.read(byte[]) method returns an int for the number of bytes it actually read. It’s not guaranteed to read as many bytes as you requested from the byte array. It’ll often return the size of the underlying buffer and you’ll have to call it many times.

    You can use this to be more efficient by streaming the bytes from the socket to the file instead of buffering the whole byte array in memory. Likewise on the server side you can do the same thing to save memory and be faster than writing a byte at a time.

    Here’s a working example of a server and client in one that connects to itself to transfer a file:

    public class SocketFileExample {
        static void server() throws IOException {
            ServerSocket ss = new ServerSocket(3434);
            Socket socket = ss.accept();
            InputStream in = new FileInputStream("send.jpg");
            OutputStream out = socket.getOutputStream();
            copy(in, out);
            out.close();
            in.close();
        }
    
        static void client() throws IOException {
            Socket socket = new Socket("localhost", 3434);
            InputStream in = socket.getInputStream();
            OutputStream out = new FileOutputStream("recv.jpg");
            copy(in, out);
            out.close();
            in.close();
        }
    
        static void copy(InputStream in, OutputStream out) throws IOException {
            byte[] buf = new byte[8192];
            int len = 0;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
        }
    
        public static void main(String[] args) throws IOException {
            new Thread() {
                public void run() {
                    try {
                        server();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
    
            client();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone I'm trying to improve my Java skills by solving some problems from
I am trying to use gmail's SMTP server smtp.gmail.com to send mails using C
Hello I am trying to get the difference between to text files. There are
hello there :) Im trying to figure out how i can grab some content
I'm trying to call a web service with a Java client. The WSDL looks
I am trying to write a servlet that will send a XML file (xml
I'm trying to compose a .zip file in a CGI program and send that
For testing purposes, I am trying to send emails to hello@test.com , but qmail
I am trying to send a Topic name in the URL like, <a href=hello?TopicN=blahblahblha>
Hello is there any windows API function that would return if drive is writable.

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.