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();
}
13
  • You should post the exception and stacktrace, as it's sort of unclear what you are describing. Oct 9, 2012 at 22:42
  • You should check whether "is" is null or not, and not whether s is closed or not. They are different objects and you are not aware of the underlying structure. Oct 9, 2012 at 22:56
  • @Greg Giacovelli i have added the exception
    – yahh
    Oct 9, 2012 at 23:12
  • @Ender Wiggin i have checked it on the server side it says closed on the client side it says not closed.
    – yahh
    Oct 9, 2012 at 23:13
  • You know the ports are not the same right? You listen on 12345 and send a connect to 1234 ... typo? Oct 9, 2012 at 23:17

1 Answer 1

3

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.

2
  • i havent closed the streams, could you look at my latest edit. i have just removed threading and copied the same code into the sendorder() and it started working, but i need the threading.
    – yahh
    Oct 10, 2012 at 0:13
  • 1
    @yahh You have definitely closed something. The exception says so. In your code, there's a lot of fluff: s.getInputStream() can never be null; s.isClosed() can never be true in the same block you called s.getInputStream() in; any IOException other than SocketTimeoutException is fatal and must result in you closing and a 'break'; there are far too many inner try/catch blocks; and you should use the same streams for the life of the socket, not create new ones for every message. This last is the cause of the problem, as you are releasing streams to be closed by GC, which closes the socket.
    – user207421
    Oct 10, 2012 at 1:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.