在springboot中处理UDP流

发布时间 2023-09-15 18:44:46作者: huorexiaji

配置: 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-integration</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-ip</artifactId>
    <version>5.1.0.RELEASE</version>
</dependency>

 

服务端:

@Bean
  public IntegrationFlow processUniCastUdpMessage() {
    return IntegrationFlows
      .from(new UnicastReceivingChannelAdapter(11111))
      .handle("UDPServer", "handleMessage")
      .get();
  }

@Service
public class UDPServer
{
  public void handleMessage(Message message)
  {
    String data = new String((byte[]) message.getPayload());
    System.out.print(data);
  }
}

客户端:

UnicastSendingMessageHandler handler =
      new UnicastSendingMessageHandler("localhost", 11111);

String payload = "Hello world";
handler.handleMessage(MessageBuilder.withPayload(payload).build());

还没有测试行不行,不过感觉应该没问题