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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:19:11+00:00 2026-06-12T15:19:11+00:00

EDIT: on the server-side, if I remove the code from the run method in

  • 0

EDIT: on the server-side, if I remove the code from the run method in ServerWriter class and put it into the send order method, removing the code for threading, it starts working properly again, socket doesn’t close. but I need it to be multithreaded, I don’t understand why the sockets close with threading. Code is shown below

    public void sendOrder(Order o){
//      ServerWriter sw=new ServerWriter(serversocket,o);
//      Thread t=new Thread(sw);
//      t.start();
        ObjectOutputStream out=null;
        if(serversocket.isClosed()){
            System.out.println("closed");
        }
        else{
            System.out.println("notclosed");
        }
        try {
            out=new ObjectOutputStream(serversocket.getOutputStream());
            out.writeObject(o);
            out.reset();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

edit: I think the socket is closing on the server-side, not the client-side, deducing this from the output of server where it prints closed on checking isclosed(), whereas on the client-side it says Not Closed

I have a socket program. I have two methods in the client 1 for opening the socket and another for reading the data. every time I try to run this code I keep getting eofexception, which mean the socket is closed.

I had this error the last time I was working on this code a few days back so I just removed all the code from reading and pasted it in the open method and it started working (I didn’t really understand why).

Today I reverted back and just moved the code back to the read method and everything started working perfectly, the connection wasn’t closing by itself. then I made the read part a thread and still everything was working fine, I was working on the write parts of the code and now when I run it I get the same eofexception again, I haven’t changed anything in the read code, so I have no idea why this is happening. on the server-side I have done a similar thing where I accept a socket in a method and send data through a thread. On the server-side I get a SocketException.

I could really use some help in shedding some light on what I am doing wrong.

Following is the client-side code

public void open(){
    try
    {
        s=new Socket(InetAddress.getLocalHost(),12345);

        s.setKeepAlive(true);

    }

    catch(UnknownHostException u)
    {
        System.err.println("I don't know host");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 


}



public void read(){
    ClientReader cr=new ClientReader(s);
    Thread t=new Thread(cr);
    t.start();
}


class ClientReader implements Runnable{
    Socket s;
    ClientReader(Socket s){
        this.s=s;
    }
    public void run(){
        InputStream is;
        ObjectInputStream ois=null;
        try {
            is = s.getInputStream();
                    if(!(s==null)){
                System.out.println("not null");
            }
                    **//outputs NotClosed here**
        if(s.isClosed()){
            System.out.println("CLOSED");
        }
                    else system.out.println("Not Closed")

              //Throws EOFException here
         ois= new ObjectInputStream(is); 
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        Order order;
         while(true){
             try{
             order=(Order)ois.readObject();
             Client_Socket.ll.add(order);
            System.out.println(order);
            System.out.println(order.getTotal()); 

             }
             catch(EOFException e){

                 try{
                 ois.close();
                 s.close();
                 break;

             } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
             } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
    }
}

The main method for calling the client

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Client_Socket cs=new Client_Socket();
    cs.open();
    cs.read();

}

client-side exception

not null
Not Closed

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at ClientReader.run(Client_Socket.java:82)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-0" java.lang.NullPointerException
    at ClientReader.run(Client_Socket.java:91)
    at java.lang.Thread.run(Unknown Source)

exception on the server-side

Connection from Socket[addr=/192.168.0.108,port=50380,localport=12345]
closed

java.net.SocketException: Socket is closed
    at java.net.Socket.getOutputStream(Unknown Source)
    at ServerWriter.run(Server_Socket.java:123)
    at java.lang.Thread.run(Unknown Source)

server-side code snippet

public void openSocket(){
    try
    {
        service=new ServerSocket(12345);

        serversocket=service.accept();
        serversocket.setKeepAlive(true);
        System.out.println("Connection from "+serversocket);



    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("output stream error");
        System.out.println(e);
    }

}


public void sendOrder(Order o){
    ServerWriter sw=new ServerWriter(serversocket,o);
    Thread t=new Thread(sw);
    t.start();

}


class ServerWriter implements Runnable{
    Socket serversocket;
    Order o;
    ServerWriter(Socket s,Order o){
        this.serversocket=s;
        this.o=o;
    }

    public void run() {
        ObjectOutputStream out=null;
        if(serversocket.isClosed()){
            System.out.println("closed");
        }
        else{
            System.out.println("notclosed");
        }
        try {
            //SocketException here
            out=new ObjectOutputStream(serversocket.getOutputStream());
            out.writeObject(o);
            out.reset();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

method snippet for calling the server

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Order o=new Order();
    Server_Socket ss=new Server_Socket();
    ss.openSocket();
    ss.sendOrder(o);
    ss.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-06-12T15:19:13+00:00Added an answer on June 12, 2026 at 3:19 pm

    That exception means you closed the socket, and then continued to use it. Nothing to do with the peer.

    Possibly you are unaware that closing either the input stream or the output stream of a socket closes the other stream and the socket.

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

Sidebar

Related Questions

Is it possible to get an XML file (not HTML) from a server, add/remove/edit
I'm having some problems removing clients from a channellist. This is the code i
I need to send 500 Internal Server Error from an PHP script under certain
EDIT: Server is MOSS 2007 Enterprise, running SP1 and all patches up to, but
Possible Duplicate: Saving changes after table edit in SQL Server Management Studio I need
Edit: When I say SQL Server, I'm really talking about the Management Studio. Sorry
EDIT: Simple version of the question: I want to create server variables in the
I wish to edit ini files over web server, decided to use django, been
FULL EDIT: I urgently need to access a Microsoft SQL Server and read compressed
I'm trying to use SQL Server Management Studio to edit database tables. But I

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.