TCP 服务端Service

package com.java;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class Server {


/**
* @param args
*/
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(7777);
System.out.println("·þÎñÆ÷¶ËÔÚ7777¶Ë¿Ú¼àÌý¡­¡­");
Socket s = ss.accept();
System.out.println("ÒÑÓпͻ§¶ËÁ¬½Ó¹ýÀ´£¬¿ªÊ¼½øÐÐͨѶ¡­¡­");
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
PrintWriter pw = new PrintWriter(out);
System.out.println("ÕýÔÚÏò¿Í»§¶Ë·¢ËÍÏûÏ¢¡­¡­");
pw.write("¿Í»§¶Ë£¬ÄãºÃ£¬ÕâÀïÊÇ·þÎñÆ÷¶Ë¡£\r\n");
pw.flush();


System.out.println("Ïò¿Í»§¶Ë·¢ËÍÏûÏ¢Íê³É¡£");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String response = br.readLine();
System.out.println("======ÏÂÃæÊÇ¿Í»§¶Ë·¢À´µÄÏûÏ¢======");
System.out.println(response);
System.out.println("================================");
br.close();
pw.close();
s.close();
ss.close();
} catch (UnknownHostException e) {
System.out.println("ÎÞ·¨ÕÒµ½ÏàÓ¦µÄ»úÆ÷£¬´íÎóÐÅÏ¢£º" + e.getMessage());
} catch (IOException e) {
System.out.println("Êý¾Ý´«Êä³öÏÖÒì³££º" + e.getMessage());
}
}


}
在Spring Boot中使用Netty连接多个TCP服务端可以通过以下步骤实现: 1. 首先,确保你已经在Spring Boot项目中引入了Netty的依赖。可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.65.Final</version> </dependency> ``` 2. 创建一个Netty客户端的处理器类,用于处理接收到的消息。可以继承`SimpleChannelInboundHandler`类,并重写`channelRead0`方法来处理接收到的消息。 ```java public class NettyClientHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { // 处理接收到的消息 System.out.println("Received message: " + msg); } } ``` 3. 创建一个Netty客户端的启动类,用于配置和启动Netty客户端。 ```java @Configuration public class NettyClientConfig { @Value("${netty.server.host}") private String serverHost; @Value("${netty.server.port}") private int serverPort; @Bean public EventLoopGroup eventLoopGroup() { return new NioEventLoopGroup(); } @Bean public Bootstrap bootstrap(EventLoopGroup eventLoopGroup, NettyClientHandler nettyClientHandler) { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new StringEncoder()); pipeline.addLast(new StringDecoder()); pipeline.addLast(nettyClientHandler); } }); return bootstrap; } @Bean public ChannelFuture channelFuture(Bootstrap bootstrap) throws InterruptedException { return bootstrap.connect(serverHost, serverPort).sync(); } } ``` 4. 在配置文件中配置要连接的多个TCP服务端的主机和端口。 ```properties # application.properties netty.server.host=127.0.0.1 netty.server.port=8080 ``` 5. 在需要使用Netty客户端的地方注入`ChannelFuture`对象,并使用它来发送消息给服务端。 ```java @Service public class NettyClientService { @Autowired private ChannelFuture channelFuture; public void sendMessage(String message) { if (channelFuture.channel().isActive()) { channelFuture.channel().writeAndFlush(message); } else { // 连接未建立或已断开,处理相应逻辑 } } } ``` 这样,你就可以在Spring Boot项目中使用Netty连接多个TCP服务端了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只是代号而已

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值