0

I'm using Netty to build a client-server network communication. Is it possible to find out to which app a client has connected to in case of success?

It's the following problem I try to solve: If a Tomcat Server is listening to port 8080 my client app successfully connects to the "server". Looks like it doesn't matter who is listening to the port. How can I find out if my server app is currently started and listening to the port instead of e.g. Tomcat?

This is the connection code of the client:

public void run(){
    //disconnectTest();
    createBootstrap( new Bootstrap(), new NioEventLoopGroup(), true);
}

public void createBootstrap( Bootstrap b, EventLoopGroup eventLoop, boolean initialAttempt){
mWorkerGroup = eventLoop;

try {
    b.group(mWorkerGroup)
            .channel(NioSocketChannel.class)
            .option(ChannelOption.SO_KEEPALIVE, true)  
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
            .handler(new ClientChannelInitializer());

        logger.info("Connecting client...");
        b.connect(mHost, mPort)
            .addListener( new ConnectionListener(this, initialAttempt));
    } catch (Exception e) {
        logger.error("Failed to connect client to server '" +mHost +": " +mPort +". Error: ", e);
    }
}

Snippet from the ConnectionListener:

public void operationComplete(ChannelFuture future) throws Exception {
    if (future.isSuccess()) {  
       System.out.println("success");
    }else{
       System.out.println("error");
    }
}
KayJ
  • 273
  • 4
  • 15
  • 1
    lett your webapp on the tomcat provide an url with some status infos about it. Then you can access this url from your client and inspect the answer. If it's answering on that url it seams to be your webapp and if the content is the expected one it is the webapp. – Rene M. Feb 27 '15 at 15:28

1 Answers1

0

EDIT:

If you want check the availability of the server withing the client App, you can use certain tools that Java7 can give us, for example, using this code:

private static boolean available(int port) {
    try (Socket ignored = new Socket("localhost", port)) {
        return false;
    } catch (IOException ignored) {
        return true;
    }
}

This does not have to be a specific function Netty. More info here: Sockets: Discover port availability using Java

Enjoy.

To check it outside you client app:

To test the server status I use the Hercules software client.If you know that server will respond someting, using hercules you can send a dummy data y wait the server response.

enter image description here

How you can see, Hercules, allows too makes a ping to the server :)

Hope it helps.

Community
  • 1
  • 1
hcarrasko
  • 2,320
  • 6
  • 33
  • 45
  • Unfortunately I have to check it from within the ClientApplication. So this is not an option... Off course, the server could send a "connected" message back to the client to confirm a correct connection and the client can time out while waiting for this. But I was hoping for a different solution, provided by Netty. – KayJ Mar 02 '15 at 15:04