簡體   English   中英

Java,服務器客戶端TCP通信以RST結束

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

我試圖弄清楚這是否正常。 因為沒有錯誤,連接應該通過以下方式終止:

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

我在 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

客戶端正常退出並到達socket.close ,那么不應該正常關閉連接,而不需要重置嗎?

我在谷歌上找不到任何關於 java 的 TCP 流的信息......

這是我的代碼:

服務器:

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

客戶:

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

非常感謝,在非 SSL 連接上,我現在可以有序地關閉連接而無需重置。

但是顯然SSLSocket不支持這種方法? 當我在我的 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)

我的新代碼如下所示:

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

我沒有正確使用它嗎? 還是我的 SSLSocket 不正確? 因為使用非 SSL 連接沒有問題。

您的數據包日志顯示客戶端正在發送其最后一位數據,並通知服務器連接關閉。

然后服務器發回它的最后一位數據(大概是最后轉換的整數)。 客戶端以RST響應,這表明客戶端套接字已關閉以進行讀寫 - 客戶端說“對不起,我沒有捕捉到最后一點數據,我不再聽了”。

我不是 Java 專家,但似乎調用套接字OutputStream的 .close( .close()方法會導致在套接字本身上.close()方法。 您實際上想要在這里發生的事情(在客戶端寫入其最后一個整數之后)是在套接字上調用shutdownOutput()方法。

正如 caf 指出的那樣,您的問題是您在應該調用shutdownOutput時調用了close 這是因為您嘗試實現的是所謂的半關閉,其中連接的每一端都關閉它的輸出流,以便向對等方指示沒有更多的待處理數據( EOF )。

有關關閉和關閉之間區別的解釋,請參閱這個簡短的 SO 答案“ 關閉與關閉”和這篇較長的文章“ 套接字關閉與套接字關閉”。 另請參閱Java 中的有序與中止連接發布

這里真正的問題是,在另一端已經關閉了它的連接端之后,一端正在寫入。 這是一個應用程序協議錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM