简体   繁体   English

Java-关闭套接字会产生不同的结果

[英]Java - closing socket gives different results

I'm getting different results while trying to read from a socket while the other socket is closed. 在关闭另一个套接字的情况下尝试从套接字读取时,我得到了不同的结果。

I have two sockets A and B. 我有两个插座A和B。

1)B sent some data to A --> A has read the data --> A closes --> When B tries to read some data from A, it is getting -1(or EOF). 1)B向A发送了一些数据-> A已经读取了数据-> A关闭->当B尝试从A读取一些数据时,它的值为-1(或EOF)。

2)B sent some data to A --> A closes even before reading the data --> Now B tries to read from A, an exception is thrown(java.net.SocketException "Software caused connection abort.") 2)B向A发送了一些数据-> A甚至在读取数据之前就关闭了->现在B尝试从A读取数据,引发了异常(java.net.SocketException“软件导致连接中止。”)

please excuse me, if you can't understand my question. 如果您无法理解我的问题,请原谅。 Please see the code 请看代码

Server.java 服务器.java

import java.io.*;
import java.net.*;


class SocketCloser extends Thread
{
    private Socket c;
    public SocketCloser(Socket c) {
        this.c = c;
    }

    public void run()  {

        try{
            this.c.close();
        } catch (Exception e) {}
    }
}


public class Server
{
    public static void main(String argv[]) throws Exception {
        ServerSocket listen = new ServerSocket(6789);

        Socket socket = listen.accept();
        SocketCloser sc = new SocketCloser(socket);
        InputStream is = socket.getInputStream();
        // uncomment below line to get "Software caused connection abort" on client
        //sc.start();
        try {
            Thread.sleep(1000);
            int i = is.read();
            System.out.println("read returned: " + i);
            socket.close();
        } catch (Exception e) {
            System.out.println(e.toString() + " thrown");
        }
    }
}

Client.java 客户端.java

import java.io.*;
import java.net.*;

public class Client
{
    public static void main(String argv[])
    {
        Socket cSocket;
        try {
            cSocket = new Socket("localhost", 6789);
            InputStream is = cSocket.getInputStream();
            OutputStream os = cSocket.getOutputStream();
            Thread.sleep(1000);
            os.write(200);
            Thread.sleep(1000);
            int i = is.read();
            System.out.println("read returned: " + i);
            cSocket.close();
        } catch (Exception e) {
            System.out.println(e.toString() + " thrown");
        }
    }
}

Can someone please help me figure out why there is an exception in one case and -1 in another. 有人可以帮我弄清楚为什么在一种情况下有例外,而在另一种情况下为-1。 Interestingly on linux both the cases resulted in -1. 有趣的是,在Linux上,这两种情况均得出-1。

1) Because B established a connection, A goes to CLOSE_WAIT. 1)因为B建立了连接,所以A进入CLOSE_WAIT。 It will be in that state until B closes the connection. 在B关闭连接之前,它将一直处于该状态。 There is nothing to read, so the read() call on B's InputStream returns -1. 没有要读取的内容,因此B的InputStream上的read()调用返回-1。

2) A is blocked in the accept call. 2)A在接受呼叫中被阻止。 The other thread is trying to close the socket, but it can't because accept is blocking it. 另一个线程正在尝试关闭套接字,但是不能这样做,因为accept阻止了它。 When B connects, accept unblocks and the socket closes outright. 当B连接时,接受解锁并完全关闭插座。 When B tries to read, the socket is not there anymore so you get the exception. 当B尝试读取时,套接字不再存在,因此您会收到异常。

I'm simplifying a bit, but that's the gist of it. 我在简化一点,但这就是要点。

1)B sent some data to A --> A has read the data --> A closes --> When B tries to read some data from A, it is getting -1(or EOF). 1)B向A发送了一些数据-> A已经读取了数据-> A关闭->当B尝试从A读取一些数据时,它的值为-1(或EOF)。

I agree. 我同意。 What did you expect? 您期望什么? This is the expected behaviour. 这是预期的行为。

2)B sent some data to A --> A closes even before reading the data --> Now B tries to read from A, an exception is thrown(java.net.SocketException "Software caused connection abort.") 2)B向A发送了一些数据-> A甚至在读取数据之前就关闭了->现在B尝试从A读取数据,引发了异常(java.net.SocketException“软件导致连接中止。”)

I agree. 我同意。 This is one of the expected behaviours in this incorrect situation. 这是在这种错误情况下的预期行为之一。 What did you expect? 您期望什么?

please excuse me, if you can't understand my question. 如果您无法理解我的问题,请原谅。

There is no question here to understand. 这里没有问题可以理解。 You haven't asked a question. 您还没问过问题。 You close a socket without sending any data and the peer gets EOS without receiving any data. 关闭套接字而不发送任何数据,并且对等方获得EOS而不接收任何数据。 You close a socket while the peer is sending and the peer gets an exception. 在对等方发送消息时关闭套接字,对等方会收到异常。 System is working as designed. 系统正在按设计工作。

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

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