【发布时间】:2022-01-14 19:52:00
【问题描述】:
我有一台正在运行的机器,它通过 TCP 将状态信息发送到您在机器上设置的 IP 地址和端口。如果我在该 IP 地址上使用机器的命令行并运行“nc -l”,我会从机器获取状态数据。我正在尝试构建一个 Java Spring 应用程序来摄取它,但是所有 Java TCP 教程都在谈论设置通道名称和订阅通道?通道是建立在 TCP 之上的东西,而我的机器只是没有使用通道,还是在运行命令行“nc -l”命令时监听了一些默认通道?请帮助我很困惑
编辑 1:添加我无法与 Spring 应用程序集成的首次尝试代码,也无法将数据存储在 Spring JPA 中
public class EchoMultiServer {
private ServerSocket serverSocket;
public void start(int port) {
try {
serverSocket = new ServerSocket(port);
while (true)
new EchoClientHandler(serverSocket.accept()).start();
} catch (IOException e) {
e.printStackTrace();
} finally {
stop();
}
}
public void stop() {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static class EchoClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
@Autowired
PowerStationService powerStationService;
//this service connects to the repository to store the data
public EchoClientHandler(Socket socket) {
this.clientSocket = socket;
}
public JSONObject mapJsonInput(String incomingText){
try{
JSONObject json = new JSONObject(incomingText);
return json;
} catch (JSONException e){
System.out.println("JSONException " + e);
return null;
}
}
public JSONObject run() {
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
StringBuilder textBuilder = new StringBuilder();
int c = 0;
int leftCaratCount=0;
int rightCaratCount=0;
while ((c = in.read()) != -1) {
char character = (char) c;
textBuilder.append(character);
if (character == '{') {
leftCaratCount++;
} else if (character == '}') {
rightCaratCount++;
if (rightCaratCount == leftCaratCount) {
System.out.println(textBuilder);
JSONObject registrationJson = mapJsonInput(textBuilder.toString());
System.out.println("we got em");
powerStationService.save(new PowerStation(registrationJson.get("D").toString(), registrationJson.get("G").toString(), Integer.parseInt(registrationJson.get("Y").toString()), Integer.parseInt(registrationJson.get("S").toString()), registrationJson.get("C").toString(), registrationJson.get("Z").toString(), registrationJson.get("V").toString()));
out.println("000250{\"A\":\"45514\",\"C\":\""+registrationJson.get("Y")+"\",\"E\":\"30,5\",\"G\":\""+registrationJson.get("G")+"\",\"H\":\"0\",\"K\":\"1\",\"M\":\"123456\",\"N\":\"" + System.currentTimeMillis() + "\",\"O\":\"13371\",\"P\":\"" + clientSocket.getLocalAddress().getHostAddress() + "\",\"S\":\"60000\",\"U\":\"\",\"V\":\"\",\"W\":\"https://admin.chargenow.top/cdb-socket-api/v1/socketserver/common\",\"X\":\"0\",\"Y\":\"FJC\",\"Z\":\"\"}");
in.close();
out.close();
clientSocket.close();
}
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
EchoMultiServer server = new EchoMultiServer();
server.start(13370);
}
}
编辑 2:此外,我尝试使用 Spring 示例 Github 中的示例来查看它是否可以在我尝试的端口上接收消息。我可以使用 NetCat 查看 ServerOut 消息,但应用程序没有收到回复
@SpringBootApplication
@EnableConfigurationProperties(SampleProperties.class)
public class TcpAsyncBiDirectionalApplication {
public static void main(String[] args) {
SpringApplication.run(TcpAsyncBiDirectionalApplication.class, args);
}
}
@Configuration
class ServerPeer {
private final Set<String> clients = ConcurrentHashMap.newKeySet();
@Bean
public AbstractServerConnectionFactory server(SampleProperties properties) {
return Tcp.netServer(properties.getServerPort()).get();
}
@Bean
public IntegrationFlow serverIn(AbstractServerConnectionFactory server) {
return IntegrationFlows.from(Tcp.inboundAdapter(server))
.transform(Transformers.objectToString())
.log(msg -> "received by server: " + msg.getPayload())
.get();
}
@Bean
public IntegrationFlow serverOut(AbstractServerConnectionFactory server) {
return IntegrationFlows.fromSupplier(() -> "seed", e -> e.poller(Pollers.fixedDelay(5000)))
.split(this.clients, "iterator")
.enrichHeaders(h -> h.headerExpression(IpHeaders.CONNECTION_ID, "payload"))
.transform(p -> "sent by server Hello from server")
.handle(Tcp.outboundAdapter(server))
.get();
}
@EventListener
public void open(TcpConnectionOpenEvent event) {
if (event.getConnectionFactoryName().equals("server")) {
this.clients.add(event.getConnectionId());
}
}
@EventListener
public void close(TcpConnectionCloseEvent event) {
this.clients.remove(event.getConnectionId());
}
}
enter code here
enter code here
【问题讨论】:
标签: java spring tcp spring-integration