gpt4 book ai didi

java - 套接字和 Spring 集成

转载 作者:太空宇宙 更新时间:2023-11-04 08:22:37 26 4
gpt4 key购买 nike

我有一个小程序,监听端口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 上下文,旨在实现相同的目标:

<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 端口上提供数据。

希望能帮到你。

谢谢

最佳答案

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

为了“监听”端口“30003”,我必须生成一个“client”类型的connectionFactory:

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

关于java - 套接字和 Spring 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9236499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com