简体   繁体   English

Java,服务器客户端TCP通信以RST结束

[英]Java, server client TCP communication ends with RST

I'm trying to figure out if this is normal.我试图弄清楚这是否正常。 Because without errors, a connection should be terminated by:因为没有错误,连接应该通过以下方式终止:

FIN ->
<- ACK
<- FIN
ACK ->

I get this at the end of a TCP connection (over SSL, but i also get it with non-encrypted):我在 TCP 连接结束时得到这个(通过 SSL,但我也得到它没有加密):

         From     To
1494    server  client  TCP search-agent > 59185 [PSH, ACK] Seq=25974 Ack=49460 Win=63784 Len=50
1495    client  server  TCP 59185 > search-agent [ACK] Seq=49460 Ack=26024 Win=63565 Len=0
1496    client  server  TCP 59185 > search-agent [PSH, ACK] Seq=49460 Ack=26024 Win=63565 Len=23
1497    client  server  TCP 59185 > search-agent [FIN, ACK] Seq=49483 Ack=26024 Win=63565 Len=0
1498    server  client  TCP search-agent > 59185 [PSH, ACK] Seq=26024 Ack=49484 Win=63784 Len=23
1499    client  server  TCP 59185 > search-agent [RST, ACK] Seq=49484 Ack=26047 Win=0 Len=0

The client exits normally and reaches socket.close , shouldn't then the connection be shut down normally, without a reset?客户端正常退出并到达socket.close ,那么不应该正常关闭连接,而不需要重置吗?

I can't find anything about the TCP streams of java on google...我在谷歌上找不到任何关于 java 的 TCP 流的信息......

Here is my code:这是我的代码:

Server:服务器:

package Security;

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

import javax.net.ServerSocketFactory;
import javax.net.ssl.*;
import java.util.*;

public class SSLDemoServer
{
    private static ServerSocket serverSocket;
    private static final int PORT = 1234;

    public static void main(String[] args) throws IOException
    {
        int received = 0;
        String returned; 
        ObjectInputStream input = null;
        PrintWriter output = null;
        Socket client;

        System.setProperty("javax.net.ssl.keyStore", "key.keystore");
        System.setProperty("javax.net.ssl.keyStorePassword", "vwpolo");
        System.setProperty("javax.net.ssl.trustStore", "key.keystore");
        System.setProperty("javax.net.ssl.trustStorePassword", "vwpolo");

        try
        {
            System.out.println("Trying to set up server ...");
            ServerSocketFactory factory = SSLServerSocketFactory.getDefault();
            serverSocket = factory.createServerSocket(PORT);

            System.out.println("Server started!\n");
        }
        catch (IOException ioEx)
        {
            System.out.println("Unable to set up port!");
            ioEx.printStackTrace();
            System.exit(1);
        }

        while(true)
        {
            client = serverSocket.accept();
            System.out.println("Client trying to connect...");

            try
            {
                System.out.println("Trying to create inputstream...");
                input = new ObjectInputStream(client.getInputStream());
                System.out.println("Trying to create outputstream...");
                output = new PrintWriter(client.getOutputStream(), true);
                System.out.println("Client successfully connected!");

                while( true )
                {
                    received = input.readInt();
                    returned = Integer.toHexString(received);
                    System.out.print(" " + received);
                    output.println(returned.toUpperCase());
                }
            }
            catch(SSLException sslEx)
            {
                System.out.println("Connection failed! (non-SSL connection?)\n");
                client.close();
                continue;
            }
            catch(EOFException eofEx)
            {
                System.out.println("\nEnd of client data.\n");
            }
            catch(IOException ioEx)
            {
                System.out.println("I/O problem! (correct inputstream?)");
            }

            try {
                input.close();
                output.close();
            }
            catch (Exception e) {
            }
            client.close();
            System.out.println("Client closed.\n");
        }
    }
}

Client:客户:

package Security;

import java.io.*;
import java.net.*;
import javax.net.ssl.*;

import java.util.*;

public class SSLDemoClient
{
    private static InetAddress host;
    private static final int PORT = 1234;

    public static void main(String[] args)
    {
        System.setProperty("javax.net.ssl.keyStore", "key.keystore");
        System.setProperty("javax.net.ssl.keyStorePassword", "vwpolo");
        System.setProperty("javax.net.ssl.trustStore", "key.keystore");
        System.setProperty("javax.net.ssl.trustStorePassword", "vwpolo");

        System.out.println("\nCreating SSL socket ...");
        SSLSocket socket = null;
        try
        {
            host = InetAddress.getByName("192.168.56.101");
            SSLSocketFactory factory = (SSLSocketFactory)
            SSLSocketFactory.getDefault();
            socket = (SSLSocket) factory.createSocket(host, PORT);
            socket.startHandshake();
        }
        catch(UnknownHostException uhEx)
        {
            System.out.println("\nHost ID not found!\n");
            System.exit(1);
        }
        catch(SSLException sslEx)
        {
            System.out.println("\nHandshaking unsuccessful ...");
            System.exit(1);
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("\nHandshaking succeeded ...\n");

        SSLClientThread client = new SSLClientThread(socket);
        SSLReceiverThread receiver = new SSLReceiverThread(socket);

        client.start();
        receiver.start();

        try
        {
            client.join();
            receiver.join();
            System.out.println("Trying to close...");
            socket.close();
        }
        catch(InterruptedException iEx)
        {
            iEx.printStackTrace();
        } 
        catch(IOException ioEx)
        {
            ioEx.printStackTrace();
        }

        System.out.println("\nClient finished.");
    }
}


class SSLClientThread extends Thread
{
    private SSLSocket socket;

    public SSLClientThread(SSLSocket s)
    {
        socket = s;
    }

    public void run() 
    {
        try
        {
            ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());

            for( int i = 1; i < 1025; i++)
            {
                output.writeInt(i);
                sleep(10);
                output.flush();
            } 

            output.flush();
            sleep(1000);
            output.close();
        }
        catch(IOException ioEx)
        {
            System.out.println("Socket closed or unable to open socket.");
        }
        catch(InterruptedException iEx)
        {
            iEx.printStackTrace();
        }
    }
}


class SSLReceiverThread extends Thread
{
    private SSLSocket socket;

    public SSLReceiverThread(SSLSocket s)
    {
        socket = s;
    }

    public void run()
    {
        String response = null;
        BufferedReader input = null;

        try
        {
            input = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));

            try
            {
                response = input.readLine();
                while(!response.equals(null)) 
                {
                    System.out.print(response + " ");
                    response = input.readLine();
                } 
            }
            catch(Exception e)
            {
                System.out.println("\nEnd of server data.\n");
            }    

            input.close();

        }
        catch(IOException ioEx)
        {
            ioEx.printStackTrace();
        }
    }
}

Thanks a lot, on a non-SSL connection, I now get a nice orderly connection shutdown without a reset.非常感谢,在非 SSL 连接上,我现在可以有序地关闭连接而无需重置。

BUT apparently this method is not supported by SSLSocket ?但是显然SSLSocket不支持这种方法? When I try it in my SSL client, I get the following exception:当我在我的 SSL 客户端中尝试它时,我得到以下异常:

Exception in thread "Thread-0" java.lang.UnsupportedOperationException: The method shutdownOutput() is not supported in SSLSocket
    at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.shutdownOutput(Unknown Source)
    at Security.SSLClientThread.run(SSLDemoClient.java:99)

My new code looks like this:我的新代码如下所示:

package Security;

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

import javax.net.SocketFactory;
import javax.net.ssl.*;

public class SSLDemoClient
{
    private static InetAddress host;
    private static Socket socket;
    private static final int PORT = 1234;

    public static void main(String[] args)
    {
        System.setProperty("javax.net.ssl.keyStore", "key.keystore");
        System.setProperty("javax.net.ssl.keyStorePassword", "vwpolo");
        System.setProperty("javax.net.ssl.trustStore", "key.keystore");
        System.setProperty("javax.net.ssl.trustStorePassword", "vwpolo");

        System.out.println("\nCreating SSL socket ...");

        try
        {
            host = InetAddress.getByName("192.168.56.101");
            SocketFactory socketFactory = SSLSocketFactory.getDefault();
            socket = socketFactory.createSocket(host, PORT);
        }
        catch(UnknownHostException uhEx)
        {
            System.out.println("\nHost ID not found!\n");
            System.exit(1);
        }
        catch(SSLException sslEx)
        {
            System.out.println("\nHandshaking unsuccessful ...");
            System.exit(1);
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("\nHandshaking succeeded ...\n");

        SSLClientThread client = new SSLClientThread(socket);
        SSLReceiverThread receiver = new SSLReceiverThread(socket);

        client.start();
        receiver.start();

        try
        {
            client.join();
            receiver.join();
            System.out.println("Trying to close...");

            socket.close();
        }
        catch(InterruptedException iEx)
        {
            iEx.printStackTrace();
        } 
        catch(IOException ioEx)
        {
            ioEx.printStackTrace();
        }

        System.out.println("\nClient finished.");
    }
}


class SSLClientThread extends Thread
{
    private Socket socket;

    public SSLClientThread(Socket s)
    {
        socket = s;
    }

    public void run() 
    {
        try
        {
            ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());

            for( int i = 1; i < 1025; i++)
            {
                output.writeInt(i);
                sleep(10);
                output.flush();
            } 

            output.flush();
            sleep(1000);
            socket.shutdownOutput();
        }
        catch(IOException ioEx)
        {
            System.out.println("Socket closed or unable to open socket.");
        }
        catch(InterruptedException iEx)
        {
            iEx.printStackTrace();
        }
    }
}


class SSLReceiverThread extends Thread
{
    private Socket socket;

    public SSLReceiverThread(Socket s)
    {
        socket = s;
    }

    public void run()
    {
        String response = null;
        BufferedReader input = null;

        try
        {
            input = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));

            try
            {
                response = input.readLine();
                while(!response.equals(null)) 
                {
                    System.out.print(response + " ");
                    response = input.readLine();
                } 
            }
            catch(Exception e)
            {
                System.out.println("\nEnd of server data.\n");
            }    

            socket.shutdownInput();

        }
        catch(IOException ioEx)
        {
            ioEx.printStackTrace();
        }
    }
}

Am I not using it correcty?我没有正确使用它吗? Or is my SSLSocket not correct?还是我的 SSLSocket 不正确? Because with a non-SSL connection there is no problem.因为使用非 SSL 连接没有问题。

Your packet log shows that the client is sending its last bit of data, and notifying the server of the connection closing.您的数据包日志显示客户端正在发送其最后一位数据,并通知服务器连接关闭。

The server then sends back its last bit of data (the last converted integer, presumably).然后服务器发回它的最后一位数据(大概是最后转换的整数)。 The client responds with a RST , which indicates that the client socket was closed for both reading and writing - the client is saying "sorry, I didn't catch that last bit of data, I'm not listening anymore".客户端以RST响应,这表明客户端套接字已关闭以进行读写 - 客户端说“对不起,我没有捕捉到最后一点数据,我不再听了”。

I am not a Java guru, but it appears that calling the .close() method of the socket's OutputStream is resulting in the .close() method being called on the socket itself.我不是 Java 专家,但似乎调用套接字OutputStream的 .close( .close()方法会导致在套接字本身上.close()方法。 What you actually want to happen here (after the client has written its last integer) is that the shutdownOutput() method is called on the socket.您实际上想要在这里发生的事情(在客户端写入其最后一个整数之后)是在套接字上调用shutdownOutput()方法。

As caf points out, your problem is that you are calling close when you should be calling shutdownOutput .正如 caf 指出的那样,您的问题是您在应该调用shutdownOutput时调用了close This is because what you are trying to implement is what's called a half-close, in which each side of the connection closes it's output stream in order to indicate to the peer that there is no more data pending ( EOF ).这是因为您尝试实现的是所谓的半关闭,其中连接的每一端都关闭它的输出流,以便向对等方指示没有更多的待处理数据( EOF )。

For an explanation of the difference between close and shutdown see this short SO answer " close vs. shutdown " and this longer article, " Socket shutdown versus socket close ".有关关闭和关闭之间区别的解释,请参阅这个简短的 SO 答案“ 关闭与关闭”和这篇较长的文章“ 套接字关闭与套接字关闭”。 Also see, Orderly Versus Abortive Connection Release in Java .另请参阅Java 中的有序与中止连接发布

The real problem here is that one end is writing after the other end has already closed its end of the connection.这里真正的问题是,在另一端已经关闭了它的连接端之后,一端正在写入。 This is an application protocol error.这是一个应用程序协议错误。

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

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