1

我有一个监听端口 30003 的小程序。它是一个处理字节流数据的服务器类,如下所示:

sbsSocket = new Socket(sbsHost, sbsPort);
sbsReader = new BufferedReader(new InputStreamReader(sbsSocket.getInputStream()));

private void startSBSMessageReceiverThread() {
    new Thread(new Runnable() {
        public void run() {
            while(true) {
                try {
                    String message = sbsReader.readLine();
                    // System.out.println(message);
                    tracker.handleSBSMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

这很好用 - 但是我想转向使用 Spring Integration。这是我的 tcp-context 用于实现相同目的的东西:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:ip="http://www.springframework.org/schema/integration/ip"
    xsi:schemaLocation="http://www.springframework.org/schema/integration 
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/integration/ip
http://www.springframework.org/schema/integration/ip/spring-integration-ip-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


<context:component-scan base-package="com.atlaschase.falcon.integration"></context:component-scan>

<int:channel id="heathrowChannel"></int:channel>

<ip:tcp-connection-factory id="heathrowConnectionFactory" type="server" host="127.0.0.1" port="30003"/>

<ip:tcp-inbound-channel-adapter id="heathrowInboundAdapter" channel="heathrowChannel" connection-factory="heathrowConnectionFactory"/>

<int:service-activator id="adsbHandler" input-channel="heathrowChannel" ref="sbsListener"/>

</beans>

当我使用 Spring Integration 方法启动程序时,我得到以下堆栈跟踪 - 告诉我套接字地址已在使用中。

SEVERE: Error on ServerSocket
java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
    at java.net.ServerSocket.bind(ServerSocket.java:328)
    at java.net.ServerSocket.<init>(ServerSocket.java:194)
    at java.net.ServerSocket.<init>(ServerSocket.java:150)
    at javax.net.DefaultServerSocketFactory.createServerSocket(ServerSocketFactory.java:163)
    at org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory.run(TcpNetServerConnectionFactory.java:61)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

我不明白为什么当我的标准套接字程序运行良好时 - 为什么集成方法会失败。我的机器上有一些软件可以在 30003 端口上提供数据。

我希望你能帮忙。

谢谢

4

1 回答 1

1

这是通过清除服务器和客户端之间的混淆来解决的。我无法生成服务器套接字,因为应用程序已经绑定到该端口号。

要“监听”端口“30003”,我必须生成一个“客户端”类型的 connectionFactory:

<ip:tcp-connection-factory id="heathrowConnectionFactory" type="server" host="127.0.0.1" port="30003"/>
于 2012-04-18T11:49:08.910 回答