在客户端服务器套接字程序中,clientsocket无法读取输入流时获取套接字异常

时间:2018-09-11 13:09:23

标签: java sockets socketexception

在下面的客户端服务器套接字程序中,客户端正在向正在侦听端口3500的服务器发送数据,但是这里服务器没有将任何数据发送回客户端,因此在客户端套接字程序中,我不读取从那时输入流然后得到套接字异常,套接字写入错误。 下面是服务器代码:

    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();
        }

    }

下面的客户端套接字代码:

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();
        }
    }

在上面的代码中,如果删除了注释,则代码可以正常工作,但困惑为什么在没有数据要读取的情况下应该读取输入流。

如上所述对输入流部分进行注释后,获得以下异常。

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)

1 个答案:

答案 0 :(得分:0)

错误是因为在服务器有机会将数据写入流之前,您在客户端上关闭了连接。

如果您删除

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

该错误应该得到解决,因为服务器不再尝试在封闭的连接上进行写操作。

如果让客户端读取代码中的一部分,则整个过程将正常进行,因为客户端会等待连接中的新数据。这导致当服务器尝试写入时连接仍处于打开状态,并且一切都按预期从Java站点运行。