我正在进行财务消息传递集成,如下所示:


服务器具有一个接口,用于侦听来自特定IP和端口上的客户端套接字的请求
服务器将每个请求的响应发送回客户端套接字
另外,服务器将请求发送到同一客户端套接字


以下是完美的工作方式:


客户端套接字(Java IO的Socket对象)成功将请求发送到服务器接口
客户端套接字成功收到每个请求的响应


try {
    Socket clientSocket = new Socket("example.com", 8888);
    BufferedWriter output = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
    output.write(data);
    output.flush();
    BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    // Read responses or new request from input
} catch (IOException e) {
    e.printStackTrace();
}


客户端套接字应该可以接收来自服务器的任何请求,就像接收来自同一服务器的响应一样。但是,当服务器向客户端套接字发起请求时,永远不会收到该请求。但是,我们可以在客户端环境中跟踪来自tcpdump的流量。

如何使客户端套接字监听服务器发出的请求,而不仅仅是响应?

更新资料

可能有助于阐明有关此集成的一些信息:

一种。在这种情况下,“服务器”是第三方系统,具有集成规则

b。我的客户端套接字将消息发布到服务器(上面)

C。我的实现侦听来自第三方系统服务器的响应和请求,方法是创建自己的服务器套接字(排除),或者使用与我一起发送的客户端套接字(我正在寻找的解决方案)

最佳答案

这是一个非常常见的错误,您在编写消息时没有在末尾写“ \ n”(结束行标识符),因此不会读取任何消息。要解决此问题,请在PrintWriter中使用println

这会将一条线发送到另一个插座。

这是服务器的每客户端线程模型的示例

//create a new server socket with the port as a parameter, this will bind it to the specified port: 6000
        ServerSocket server = new ServerSocket(6000);
        System.out.println("Binded");
        //create a while loop accepting sockets
        while(true)
        {
            //accept a socket
            Socket client = server.accept();
            System.out.println("Client has connected");
            //create a new thread for this socket
            new Thread(() ->
            {
                try
                {
                    /*
                    create a print writer so you can write a line, not only a message like BufferedWriter does,
                    if for some reason you still want to use BufferedWriter use
                    writer.write(message + "\n");
                     */
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
                    //Create a new reader
                    BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    System.out.println("Waiting for requests...");
                    //create a while loop reading requests (lines)
                    String request;
                    while((request = reader.readLine()) != null)
                    {
                        System.out.println("Received message: " + request);
                        //here find the correct response and return it, I just sent a message, replace it with the correct response
                        writer.println("Hello there! How are you today?");
                        //flush, flushing will write the data to the client
                        writer.flush();
                    }
                } catch(IOException e)
                {
                    //print an exception if it occurred, if an exception occurrs its most likely just a disconnection exception
                    e.printStackTrace();
                }
            }).start();
        }


这是一个客户的例子

//connect to the server at "localhost" on port 6000
        Socket client = new Socket("localhost", 6000);
        System.out.println("Connected");
        /*
        create a print writer so you can write a line, not only a message like BufferedWriter does,
        if for some reason you still want to use BufferedWriter use
        writer.write(message + "\n");
        */
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
        //Create a new reader
        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
        //write a request
        writer.println("Hi there!");
        //flush, flushing will write the data to the server
        writer.flush();
        System.out.println("Written");
        System.out.println("Waiting for responses...");
        //create a while loop reading responses (lines)
        //you may want to do this while loop in another thread
        String response;
        while((response = reader.readLine()) != null)
        {
            System.out.println("Received response: " + response);
        }


另外,如果这与财务信息有关,我建议使用TLS (SSL)

您不必担心Java已经实现并使其易于使用,这是服务器的示例

//create a new SSL server socket with the port as a parameter, this will bind it to the specified port: 6000
        //you create it by getting the default SSLServerSocketFactory which will create a new SSLServerSocket
        //you need to cast it since it actually returns ServerSocket but SSLServerSocket extends ServerSocket and this returns SSLServerSocket so it is safe
        SSLServerSocket server = (SSLServerSocket) SSLServerSocketFactory.getDefault().createServerSocket(6000);
        System.out.println("Binded");
        //set the enabled ciphersuites, until you buy a certificate set only to ciphersuites with "anon" more info on ciphersuites on https://en.wikipedia.org/wiki/Cipher_suite
        server.setEnabledCipherSuites(new String[]{"TLS_ECDH_anon_WITH_AES_256_CBC_SHA"});
        //create a while loop accepting sockets
        while(true)
        {
            //accept a socket a SSLSocket
            SSLSocket client = (SSLSocket) server.accept();
            System.out.println("Client has connected");
            //create a new thread for this socket
            new Thread(() ->
            {
                try
                {
                    //begin a handshake more info about handshakes in https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.1.0/com.ibm.mq.doc/sy10660_.htm
                    client.startHandshake();
                    /*
                    create a print writer so you can write a line, not only a message like BufferedWriter does,
                    if for some reason you still want to use BufferedWriter use
                    writer.write(message + "\n");
                     */
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
                    //Create a new reader
                    BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    System.out.println("Waiting for requests...");
                    //create a while loop reading requests (lines)
                    String request;
                    while((request = reader.readLine()) != null)
                    {
                        System.out.println("Received message: " + request);
                        //here find the correct response and return it, I just sent a message, replace it with the correct response
                        writer.println("Hello there! How are you today?");
                        //flush, flushing will write the data to the client
                        writer.flush();
                    }
                } catch(IOException e)
                {
                    //print an exception if it occurred, if an exception occurrs its most likely just a disconnection exception
                    e.printStackTrace();
                }
            }).start();
        }


这是一个客户的例子

//connect to the server at "localhost" on port 6000
        //you create a SSLSocket by getting the default SSLSocketFactory which will create a new SSLSocket
        //you need to cast it since it actually returns Socket but SSLSocket extends Socket and this returns SSLSocket so it is safe
        SSLSocket client = (SSLSocket) SSLSocketFactory.getDefault().createSocket("localhost", 6000);
        System.out.println("Connected");
        //set the enabled ciphersuites to everything supported so the server can decide the ciphersuite, you can modify this to specified ciphersuites
        client.setEnabledCipherSuites(client.getSupportedCipherSuites());
        //begin a handshake more info about handshakes in https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.1.0/com.ibm.mq.doc/sy10660_.htm
        client.startHandshake();
        /*
        create a print writer so you can write a line, not only a message like BufferedWriter does,
        if for some reason you still want to use BufferedWriter use
        writer.write(message + "\n");
        */
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
        //Create a new reader
        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
        //write a request
        writer.println("Hi there!");
        //flush, flushing will write the data to the server
        writer.flush();
        System.out.println("Written");
        System.out.println("Waiting for responses...");
        //create a while loop reading responses (lines)
        //you may want to do this while loop in another thread
        String response;
        while((response = reader.readLine()) != null)
        {
            System.out.println("Received response: " + response);
        }

07-24 18:43