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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:36:03+00:00 2026-06-15T20:36:03+00:00

How can I stop the SocketException from occurring? I am trying to do a

  • 0

How can I stop the SocketException from occurring?

I am trying to do a simple transfer of a serialized object from a client to a server on my local machine.

I have been able to use a slight variation of the following code to send strings, but when I try to send an object

Customer customerToReceive = (Customer) input.readObject();// EXCEPTION OCCURS RIGHT HERE

I get a SocketException that I don’t understand how to interpret.

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.read(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at MattServer.runCustomerServer(MattServer.java:44)
at MattServer.<init>(MattServer.java:14)
at MattServerTest.main(MattServerTest.java:10)

Here is the Client code, which doesn’t seem to complain at all:
public class MattClient
{
Socket client;
ObjectOutputStream output;
ObjectInputStream input;
String message;

public MattClient()
{
    runCustomerClient();
}

public void runCustomerClient()
{
    try
    {
        //Connection:
        System.out.println("Attempting connection...");
        client = new Socket("localhost",12345);
        System.out.println("Connected to server...");

        //Connect Streams:

        //output.flush();
        System.out.println("Got IO Streams...");

        //SEND MESSAGES:
        try
        {
            for(int i = 1;i<=10;i++)
            {
                output = new ObjectOutputStream(client.getOutputStream());
                Customer customerToSend = new Customer("Matt", "1234 fake street", i);
                System.out.println("Created customer:");
                System.out.println(customerToSend.toString());
                output.writeObject(customerToSend);
                output.flush();
            };
            message = "TERMINATE";
            System.out.println(message);
            output.writeObject(message);
            output.reset();
            output.flush();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch(Exception e2)
        {
            e2.printStackTrace();
        }
        finally
        {

        }
    }
    catch (IOException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    catch(Exception e3)
    {
        e3.printStackTrace();
    }
    finally
    {

    }
}

And the server which revolts :

public class MattServer
{
ServerSocket server;
Socket socket;
ObjectInputStream input;
ObjectOutputStream output;
String message;

public MattServer()
{
    runCustomerServer();
}

public void runCustomerServer()
{
    try
    {
        server = new ServerSocket(12345,100000);
        while(true)
        {
            //CONNECTION:
            System.out.println("Waiting for connection");
            socket = server.accept();
            System.out.println("Connection received...");

            //CONNECT STREAMS:
            //output = new ObjectOutputStream(socket.getOutputStream());
            //output.flush();
            input = new ObjectInputStream(socket.getInputStream());
            System.out.println("Got IO Streams...");

            //PROCESS STREAMS:
            System.out.println("Connection successful!");
            do
            {
                System.out.println("Started loop");
                try
                {
                    System.out.println("in try...");
                    System.out.println(socket.getInetAddress().getHostName());
                    Customer customerToReceive = (Customer) input.readObject();// EXCEPTION OCCURS RIGHT HERE
                    Object o = input.readObject();
                    System.out.println("Object of class " + o.getClass().getName() + " is " + o);
                    System.out.println("Got customer object");
                    System.out.println(customerToReceive.toString());
                }
                catch(ClassNotFoundException cnfE)
                {
                    System.out.println("Can't convert input to string");
                }
            } while(!message.equals("TERMINATE"));

            System.out.println("Finished.");

        }
    }
    catch(IOException ioE)
    {
        ioE.printStackTrace();
    }
    finally
    {
        try
        {
            input.close();
            socket.close();
            server.close();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • 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-06-15T20:36:04+00:00Added an answer on June 15, 2026 at 8:36 pm

    This exception is the result of closing the connection too early, that is, the client sends its data and immediatly closes the connection. In this case, the server usually has not been able to read all data sent by the client and the termination of the connection comes in the middle of the transfer as seen by the server side.

    Note that flush()-ing a network OutputStream does not mean or guarantee that any/all data has actually been transmitted. In the best scenario this only makes sure that all data is passed on to the network stack on the local machine, which will decide at its discretion when to actually transfer the data.

    One solution to this is to let the server close the connection when it is ready to. The client should then wait, e.g. in a blocking read() operation, and will then be notified by the named exception when the server signals the end of the transmission.

    Another way is to implement your own acknowledgement so that the client will wait for the server to send an acknowledgement message back, after which the connection can be safely closed by both sides.

    For reference, there are socket options which influence the behavior of the connection in cases like yours, namely:

    SO_LINGER and
    TCP_NODELAY

    (These socket options are not a solution to your problem but may change the observed behavior in some cases.)

    EDIT:

    It appears the relevance of the SO_LINGER option for the observed behavior is not as obvious as I thought it was from the referenced documentation. Therefore, I will try to make the point clearer:

    In Java there are basically two ways to terminate a TCP connection via close():

    1. Without enabling the SO_LINGER option. In this case, a TCP connection reset (RST) is immediatly sent and the call to close() returns. The peer side of the connection will receive an exception stating that the ‘connection has been reset’ when trying to use the connection (read or write) after the RST has been received.

    2. With enabled SO_LINGER option. In this case, a TCP FIN is generated to orderly shut down the connection. Then, if the peer does not acknowlege the data sent within the given time frame, a timeout occurs and the local party issuing the close() continues as in case #1, sending an RST and then declaring the connection ‘dead’.

    So, usually one wants to enable SO_LINGER to allow the peer to process the data and then tear down the connection cleanly. If SO_LINGER is not enabled and close() is called before all data has been processed by the peer (i.e. ‘too early’) the named exception re the reset connection occurs at the peer.

    As said above, the TCP_NODELAY option may change the observed behavior in a non-deterministic way, because the data written is more likely to already be transferred across the network before the call to close() causes the reset of the connection.

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

Sidebar

Related Questions

I have this simple array and I'd like to know how I can stop
I have this code. I can receive data from my client successfully, but only
I have looked and tried but don't see where I can stop some being
I'm trying to write a program that can stop and start services using SilverLight
How can I stop the ABAP extended program check (SLIN) from reporting errors in
How can I stop this from happening, it is grabbing files in .svn folders
I know that we can stop inheritance of the web.config files from the root
I read somewhere that you can stop Magento from requiring two product descriptions... this
Is there some way I can stop Chrome from auto populating input boxes? I
is there a way I can stop the opacity from affecting my links text

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.