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

  • Home
  • SEARCH
  • 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 169515
In Process

The Archive Base Latest Questions

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

I am making a prototype client & server so that I can understand how

  • 0

I am making a prototype client & server so that I can understand how to handle reconnects. The server ought to create a serversocket and listen forever. A client may connect, send its data, and close its socket but it will not send a ‘I’m done and closing’ type message to the server. For this reason, the server gets a EOFException when it does a readByte() since the remote client has closed. In the error handler for the EOFException, it will close the socket and open a new one.

Here’s the problem: The client sometimes gets a SocketWriteError when it does the outputStream.write() call even after it successfully opens the socket/inputstream/outpustream. It may have something to do with the frequency that I’m opening and closing these sockets. One interesting thing is that the client does an arbitrary number of writes/close/reconnects before crapping out. It will sometimes crap out on the first reconnect, other times it will take 50 reconnects before seeing the SocketWriteError.

Here’s the error on the client side:

 java.net.SocketException: Connection reset by peer: socket write error        at java.net.SocketOutputStream.socketWrite0(Native Method)        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)        at java.net.SocketOutputStream.write(SocketOutputStream.java:115)        at bytebuffertest.Client.main(Client.java:37) 

Here are some snippets of code:

SERVER:

public static void main(String[] args) {     Server x = new Server();     x.initialize(); }  private void initialize() {     ServerSocket s;     InputStream is;     DataInputStream dis;     while (true) //ADDED THIS!!!!!!!!!!!!!!!!!!!!!!     {         try         {             s = new ServerSocket(4448);             s.setSoTimeout(0);             s.setReuseAddress(true);             is = s.accept().getInputStream();             System.out.println('accepted client');             dis = new DataInputStream(is);             try             {                  byte input = dis.readByte();                 System.out.println('read: ' + input);             } catch (Exception ex)             {                 System.out.println('Exception');                 dis.close();                 is.close();                 s.close();             }         } catch (IOException ex)         {             System.out.println('ioexception');         }     } } 

CLIENT:

public static void main(String[] args) {     Socket s;     OutputStream os;     try     {         s = new Socket('localhost', 4448);         s.setKeepAlive(true);         s.setReuseAddress(true);         os = s.getOutputStream();         int counter = 0;         while (true)         {             try             {                 os.write((byte) counter++);                 os.flush();                  os.close();                 s.close();                  s = new Socket('localhost', 4448);                 s.setKeepAlive(true);                 s.setReuseAddress(true);                 os = s.getOutputStream();             } catch (Exception e)             {                 e.printStackTrace();                 System.err.println('ERROR: reconnecting...');             }         }     } catch (Exception ex)     {         ex.printStackTrace();         System.err.println('ERROR: could not connect');     } } 

Does anyone know how to properly reconnect?

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

    Don’t close the ServerSocket on an error, just .accept() a new connection.

    What I normally do is each time ServerSocket.accept() returns a Socket, I spawn off a thread to handle sending and receiving from that Socket. That way you’re ready to start accepting a new connection as soon as somebody wants to connect to you.

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

Sidebar

Ask A Question

Stats

  • Questions 217k
  • Answers 217k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I seemed to have found the answer. I set the… May 12, 2026 at 11:24 pm
  • Editorial Team
    Editorial Team added an answer Does userctl.BringToFront() work? And of course you should set Visible=rue May 12, 2026 at 11:24 pm
  • Editorial Team
    Editorial Team added an answer I will suggest you a rewrite of the test using… May 12, 2026 at 11:24 pm

Related Questions

I am writing some objects (classes I guess) in javascript. Class B inherits from
I am using IE 6 (corporate mandated version) on XP, as well as (primarily)
I'm formulating an Ajax request using prototype in the following method: function updateServerStep(stepNumber){ alert(updating
I am writing an application in JavaScript (with JQuery). The application has a lot

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.