gpt4 book ai didi

java - 如何在不同端口上运行的特定 Netty 客户端实例上发送数据

转载 作者:行者123 更新时间:2023-12-02 11:59:58 30 4
gpt4 key购买 nike

我编写了一个基本的 Netty 客户端,用于从服务器发送和接收数据。但是我已经在端口 8080 和 8081 上启动了客户端的两个实例。现在我如何在端口 8080 上发送特定字符串并在 8081 上发送另一个字符串。我对如何在特定端口上发送数据感到困惑。我可以将所有字符串发送到服务器。但我想指定哪个字符串在哪个端口上发送到哪个服务器。就像我想在端口 8080 上向 server1 发送“Hello server1”,在端口 8081 上向 server2 发送“Hello server2”。我怎样才能做到这一点?

我的 Netty 客户端:

public class Client implements  Runnable {

public int port;

private Channel channel;
public ChannelFuture channelFuture = null;
private String message;
int rcvBuf, sndBuf, lowWaterMark, highWaterMark;

public Client(int port) {

this.port = port;
rcvBuf = Integer.MAX_VALUE;
sndBuf = Integer.MAX_VALUE;
lowWaterMark = 2048;
highWaterMark = 3048;

}

@Override
public void run() {

try {
connectLoop();
} catch (Exception ex) {

System.err.println("Exception raised in Client class" + ex);
}

}

public final void connectLoop() throws InterruptedException {
EventLoopGroup workGroup = new NioEventLoopGroup();

try {
Bootstrap bs = new Bootstrap();
bs.group(workGroup);
bs.channel(NioSocketChannel.class);
bs.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.SO_RCVBUF, rcvBuf)
.option(ChannelOption.SO_SNDBUF, sndBuf)
.option(ChannelOption.SO_LINGER, 0)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(lowWaterMark, highWaterMark))
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline()
.addLast("patternDecoder", new ClientDecoder())
.addLast("Response Handler", new ClientHandler())// for receiving from Server
.addLast("exception Handler", new ClientExceptionHandler(port));
}
});

channelFuture = bs.connect("127.0.0.1", port).sync();
this.channel = channelFuture.channel();
if (channelFuture.isSuccess()) {
sendMessage("Hello server");
}
} catch (Exception ex) {

workGroup.shutdownGracefully();
System.err.println("ERROR : Server Not In Connection");
System.err.println("Connecting to Server...");
reconnect();
}

}

public void reconnect() throws InterruptedException {
Thread.sleep(10000);
connectLoop();

}

public void sendMessage(String data){
if (data != null)
{
channelFuture.channel().writeAndFlush(Unpooled.copiedBuffer(data.getBytes()));
System.out.println("Outgoing To Server >> " + data);
}
}
public static void main(String[] args){
Thread t = new Thread(new Client(8080));
t.start();
Thread t1 = new Thread(new Client(8081));
t1.start();
}

}

客户端处理程序

public class ClientHandler extends SimpleChannelInboundHandler<String> {

@Override
protected void channelRead0(ChannelHandlerContext ctx, final String message) throws Exception {

System.out.println("Incoming From Server >> " + message);
ctx.channel().writeAndFlush(Unpooled.wrappedBuffer("HELLO".getBytes()));
}
}

ClientExceptionHandler

 public class ClientExceptionHandler extends ChannelInboundHandlerAdapter {

private int port;
public ClientExceptionHandler(int port){
this.port = port;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

ctx.deregister();
ctx.disconnect();
ctx.close();
System.err.println("ERROR : Server Disconnected");
System.err.println("Reconnecting to Server...");
}
}

最佳答案

首先您需要了解端口如何工作。只有服务器才会监听特定端口。在您的示例中,服务器将从端口 8080 进行监听。然后多个客户端可以连接到端口 8080。由于您没有提供具体详细信息,我建议您尝试以下操作之一。

  • 运行两台服务器,一台监听端口 8080,另一台监听端口 8081。然后按照您的建议使用两个客户端。
  • 在端口 8080 上仅运行一台服务器并连接两个相似的客户端如果您想区分客户端,请使用某种消息(例如,将客户端 ID 发送到服务器)

关于java - 如何在不同端口上运行的特定 Netty 客户端实例上发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47343800/

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