简体   繁体   English

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

[英]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. 在下面的客户端服务器套接字程序中,客户端正在将数据发送到正在侦听端口3500的服务器,但是这里服务器没有将任何数据发送回客户端,因此在客户端套接字程序中,我不会从输入中读取流然后获取套接字异常,套接字写入错误。 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. 这导致当服务器尝试写入时连接仍处于打开状态,并且一切都按预期从Java站点正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 套接字编程中“ helloworld”服务器和客户端程序中的异常 - Exception in “helloworld” server and client program in socket programming 获取java.io.IOException:在客户端服务器Java套接字程序中激活流 - Getting java.io.IOException: stream active in a client server Java socket program 仅当客户端套接字已为服务器套接字发送了某些内容时,才从客户端套接字读取信息 - How to read from Client socket only when Client socket has sent something for Server Socket Java服务器套接字数据输入流读取超时 - Java server socket data input stream read timeout 使用Java中的套接字编程将数据流从客户端程序(在VM中运行)发送到服务器程序(在Host OS上) - Send stream of Data from client program (running in VM) to server program(on Host OS) using socket programming in Java 一个简单的客户端服务器套接字程序的PrintWriter和OutputStream - PrintWriter and OutputStream for a simple client server socket program 简单的套接字级程序 - 客户端/服务器 - Simple socket-level program - client/server 无法通过客户端/服务器程序的套接字连接 - Cannot connect through socket for client/server program 多线程客户端/服务器,套接字异常 - Multi-threading client/server, Socket Exception 服务器客户端套接字中的空闲超时异常 - Idle time out exception in Server Client socket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM