Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在不同端口上运行的特定Netty客户端实例上发送数据_Java_Sockets_Netty - Fatal编程技术网

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

Java 如何在不同端口上运行的特定Netty客户端实例上发送数据,java,sockets,netty,Java,Sockets,Netty,我已经编写了一个基本的Netty客户端,它从服务器发送和接收数据。但我已经在端口8080和8081上启动了两个客户端实例。现在,我如何在8080端口和8081上发送另一个字符串。我对如何在特定端口上发送数据感到困惑。我可以将所有字符串发送到服务器。但是我想指定在哪个端口将哪个字符串发送到哪个服务器。就像我想发送“Hello server1”到端口8080上的server1,“Hello server2”到端口8081上的server2一样。 我该怎么做 我的脾气暴躁的客户: public cla

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

我的脾气暴躁的客户

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();
}

首先,您需要了解端口是如何工作的。只有服务器将侦听特定端口。在您的示例中,服务器将从端口8080侦听。然后,多个客户端可以连接到端口8080。由于您没有给出具体的细节,我建议您尝试以下方法之一

  • 运行两台服务器,一台侦听端口8080,另一台侦听端口8081。然后按照您的建议使用两个客户机
  • 如果要区分客户端,请仅在端口8080上运行一台服务器,并连接两个类似的客户端。使用某种消息(例如,向服务器发送客户端id)
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()));
    }
}
 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...");
    }
}