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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:15:40+00:00 2026-05-26T12:15:40+00:00

I’m trying to implement basic communication through sockets, what I have now is: server

  • 0

I’m trying to implement basic communication through sockets, what I have now is:

  • server start’s to listen on socket,

    this.serverSocket_ = new ServerSocket(this.serverPort_);
    clientSocket = this.serverSocket_.accept();
    
  • client connects, and server starts separate thread to operate with that client,

  • both open Object output and input streams,

    out = new ObjectOutputStream(clientSocket_.getOutputStream());
    out.flush();
    in = new ObjectInputStream(clientSocket_.getInputStream());
    
  • client sends two i Doubles, String and Long over that stream (flushes after each one),

        out.writeObject(conf_);
        out.flush();
        out.writeObject(supp_);
        out.flush();
        out.writeObject(separator_);
        out.flush();
        out.writeObject(new Long(dataFile_.length()));
        out.flush();
    
  • server succesfully receives those objects over previously opened streams,

    conf_ = (Double) in_.readObject();
    supp_ = (Double) in_.readObject();
    separator_ = (String) in_.readObject();
    fileSize_ = (Long) in_.readObject();
    
  • and now “the hard part”,

  • clients wants to send a file so opens different output stream (not object output stream) and sends the file,

    FileInputStream fis = new FileInputStream(dataFile_);
    BufferedInputStream bis = new BufferedInputStream(fis);
    OutputStream os = clientSocket_.getOutputStream();
    
    while (current < fileSize) {
        bytesRead = bis.read(mybytearray, 0, mybytearray.length);
        if (bytesRead >= 0) {
            os.write(mybytearray, 0, mybytearray.length);
            current += bytesRead;
        }
        progressBar_.setValue(current);
        progressBar_.repaint();
    }
    os.flush();
    
  • the server receives the file (also uses simple input stream instead ObjectInputStream),

    int bytesRead;
    int current = 0;
    
    tmpFile_ = File.createTempFile("BasketAnalysis", "rec.dat");
    
    byte[] mybytearray = new byte[fileSize];
    InputStream is = clientSocket_.getInputStream();
    FileOutputStream fos = new FileOutputStream(tmpFile_);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray, 0, mybytearray.length);
    current = bytesRead;
    
    do {
        bytesRead = is.read(mybytearray, current,
        (mybytearray.length - current));
        if (bytesRead >= 0)
            current += bytesRead;
    
    } while ((bytesRead > -1) && (current < fileSize));
    
    bos.write(mybytearray, 0, current);
    bos.flush();
    bos.close();
    
  • so far everything works fine, file is received, but now server performs some time consuming processing on that file after which sends response with whe results to client,

    String resp = new String("Some processing result...");
    out_.writeObject(resp);
    out_.flush();
    
  • the client is supposed to receive the result what finishes the whole communication,

    String message = (String) in.readObject();
    Console.info("server > " + message);
    

Unfortunately, that last step on client side fails with exception:

    java.io.EOFException
        at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.readObject(Unknown Source)
        at basketAnalysis.client.Client.run(Client.java:74)
        at java.lang.Thread.run(Unknown Source)

I want client to block on waiting for server response after sending the file, but it suddenly finishes with the exception after sending the file. I am pretty sure that I do something wrong with switching between simple streams and Object streams.

Does anybody know what should I change to haave it working?

Thank you in advance!

  • 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-26T12:15:40+00:00Added an answer on May 26, 2026 at 12:15 pm

    I think your basic misunderstanding is here:

    clients wants to send a file so opens different output stream

    OutputStream os = clientSocket_.getOutputStream();
    

    That doesn’t open a different output stream – that will get another reference to the same output stream which you’ve already got wrapped in the ObjectOutputStream. If you want two streams of data, you’ll need to open two separate connections.

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

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites 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.