简体   繁体   中英

In Client server socket program, getting socket exception when input stream is not read by clientsocket

In the client-server socket program below, the client is sending data to server which is listening on port 3500, but here the server is not sending any data back to client, so in the client socket program, i do not read from then input stream then am getting socket exception, socket write error. below is the server code:

    private static ServerSocket sc = null;

    private static final int port = 3500;

    public static void main(String args[]) throws IOException{
        try {
             sc = new ServerSocket(port);
             while(true){   
                Socket socket = sc.accept();
                System.out.println("Listening on port <=> "+port);
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                String message = (String) ois.readObject();
                System.out.println("Received Request <=>"+message);
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject("Hi Client received your message<=>"+message);
                ois.close();
                oos.close();
                socket.close();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

Client socket code below:

try {

            ObjectOutputStream oos = null;
            ObjectInputStream ois = null;
            Socket clientsocket = null;

            for(int i=0;i<2;i++){
            clientsocket = new Socket(InetAddress.getLocalHost().getHostName(), 3500);
            oos = new ObjectOutputStream(clientsocket.getOutputStream());
            oos.writeObject("Client Request: "+i);
            //ois = new ObjectInputStream(clientsocket.getInputStream());
            //String message = (String) ois.readObject();
            //System.out.println("Message sent by Server: " + message);
            //ois.close();
            oos.close();    
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

In the above code, if the comments are removed then the code is working fine, but confused why the input stream should be read when there is no data to be read.

Getting below exception after commenting the input stream section as done above.

Listening on port <=> 3500
Received Request <=>Client Request: 0
java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
    at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
    at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at com.springexercise.demo.ServerTest.main(ServerTest.java:28)

The error is because you close the connection on the client side before the server has a chance to write data into the stream.

If you remove the

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject("Hi Client received your message<=>"+message);
....
oos.close();

part the error should be resolved because the server is no longer trying to write on a closed connection.

If you have the client reading part in the code the whole process works because the client wait for new data in the connection. That leads to the situation where the connection is still open when the server tries to write and everything is working as expected from the java site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM