使Java IO套接字像ServerSocket一样监听

时间:2019-05-09 15:03:15

标签: java sockets io

我正在按如下方式进行金融消息集成:

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

以下内容运行良好:

  1. 客户端套接字(Java IO的Socket对象)成功将请求发送到服务器接口
  2. 客户端套接字成功收到每个请求的响应
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的流量。

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

更新

这可能有助于澄清有关此集成的一些信息:

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

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

c。通过创建我自己的服务器套接字(已排除)或使用非常客户端套接字<,我的实现监听了来自第三方系统服务器的响应和请求 / strong>我要发送的邮件(我正在寻找的解决方案)

2 个答案:

答案 0 :(得分:0)

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

这会将一条线发送到另一个套接字。

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

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

答案 1 :(得分:-1)

这是解决这一挑战的方法:

  • 创建一个服务器线程,该线程将使用客户端套接字进行侦听。以下是侦听服务器线程的代码:
public class MessageServer extends Thread {

    private Socket clientSocket;

    // The constructor takes in the client socket
    public MessageServer(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    public void run() {
        while(true) {
            try {
                BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                BufferedWriter output = new BufferedWriter(new OutputStreamWriter(serverSocket.getOutputStream()));
                // Read from BufferedReader
                // Logic to check if message is a response or new request and handle appropriately
                // Server posts responses to requests using the same socket
                output.write("response here...");
                output.flush();
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }
    }
}
  • 在客户端套接字的构造函数中,运行服务器线程,并为其提供客户端套接字,如代码所示:
public class MessageClient {

    public Socket clientSocket;

    public MessageClient(Socket clientSocket) {
        this.clientSocket = clientSocket;
        // Run the server thread, supply it with the client socket
        new MessageServer(clientSocket).start();
    }

    public void send(String data) {
        try {   
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            output.write(data);
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

效果很好。