How to send data on a particular Netty Client instance running on different port

hema chandra

I have written a basic Netty Client that sends and receives data from a server. But I have started two instances of the Client on ports 8080 and 8081. Now how can I send a particular string on port 8080 and another string on 8081. I am confused in how to send the data on a particular port. I can able to send all the strings to the server. But I want to specify which string to be sent on which port to which server. Like I want to send "Hello server1" to server1 on port 8080 and "Hello server2" to server2 on port 8081. How can I do that?

My Netty Client:

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

}

Client Handler

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...");
    }
}
Isuru

First you need to understand how ports work. Only servers will listen on specific ports. In your example server will listen from port 8080. Then multiple clients can connect to port 8080. Since you haven't given specific details, I suggest you to try one of the following.

  • Run two servers, one listens for port 8080 and other listens for port 8081. Then use two clients as you suggested.
  • Run only one server on port 8080 and connect two similar clients If you want to differentiate between clients use some kind of a message (e.g. send client id to server)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to kill a process running on particular port in Linux?

From Java

How to send a message to a particular client with socket.io

From Java

How to send data from client to server in Meteor?

From Dev

How to send nessage to a client in netty

From Dev

How to manage list of clients to send data in netty

From Dev

How do I connect to the server socket using the ip address and port number? (client is running on a different machine than server)

From Dev

how to fetch data for particular values from a column among different values

From Dev

how to send data to client with expressJS

From Dev

Scala Netty How to create a simple client for byte data based protocol?

From Dev

How to share port 80 for applications running on different platforms on Windows 2008

From Dev

Phoenix channels: send push to a particular client

From Dev

How does the server find out what client port to send to?

From Dev

Is there a way to intercept data on a particular port?

From Dev

Returning data to a client through a different port

From Dev

How do I plug a live data service (REST APIs) running on a different port to my app built with yeoman(angular)?

From Dev

How to read and send data to client from server?

From Dev

Netty 'handshake' decoder, better way to tell the client to send data

From Dev

Android how to send data to a running fragment

From Dev

How to send -u data of Curl in Rest client

From Dev

how to fetch data for particular values from a column among different values

From Dev

How to send data through parallel port in Python?

From Dev

How to check whether a service in running on particular port using adb

From Dev

How to communicate to one particular ESP8266 in a network of multiple ESP8266, where every module is running a server instance listening on port 80

From Dev

How to run exactly 1 instance of a program. Or how to test for particular running program

From Dev

How I can send message from server to client using the port?

From Dev

Camel Client for netty tcp port using

From Dev

Netty as "proxy": outbound channel with different port number

From Dev

Send different data with multiple instance of a form

From Dev

Kubernetes - How to list all pods running in a particular instance group?

Related Related

  1. 1

    How to kill a process running on particular port in Linux?

  2. 2

    How to send a message to a particular client with socket.io

  3. 3

    How to send data from client to server in Meteor?

  4. 4

    How to send nessage to a client in netty

  5. 5

    How to manage list of clients to send data in netty

  6. 6

    How do I connect to the server socket using the ip address and port number? (client is running on a different machine than server)

  7. 7

    how to fetch data for particular values from a column among different values

  8. 8

    how to send data to client with expressJS

  9. 9

    Scala Netty How to create a simple client for byte data based protocol?

  10. 10

    How to share port 80 for applications running on different platforms on Windows 2008

  11. 11

    Phoenix channels: send push to a particular client

  12. 12

    How does the server find out what client port to send to?

  13. 13

    Is there a way to intercept data on a particular port?

  14. 14

    Returning data to a client through a different port

  15. 15

    How do I plug a live data service (REST APIs) running on a different port to my app built with yeoman(angular)?

  16. 16

    How to read and send data to client from server?

  17. 17

    Netty 'handshake' decoder, better way to tell the client to send data

  18. 18

    Android how to send data to a running fragment

  19. 19

    How to send -u data of Curl in Rest client

  20. 20

    how to fetch data for particular values from a column among different values

  21. 21

    How to send data through parallel port in Python?

  22. 22

    How to check whether a service in running on particular port using adb

  23. 23

    How to communicate to one particular ESP8266 in a network of multiple ESP8266, where every module is running a server instance listening on port 80

  24. 24

    How to run exactly 1 instance of a program. Or how to test for particular running program

  25. 25

    How I can send message from server to client using the port?

  26. 26

    Camel Client for netty tcp port using

  27. 27

    Netty as "proxy": outbound channel with different port number

  28. 28

    Send different data with multiple instance of a form

  29. 29

    Kubernetes - How to list all pods running in a particular instance group?

HotTag

Archive