Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Good Day,

I was taking a look at this tutorial to do a TCP Threadpool server. http://tutorials.jenkov.com/java-multithreaded-servers/thread-pooled-server.html

It works great for listening/RECEIVING to clients and processing, and returning a response. There is a class inside that I pass in WorkerRunnable into, and that basically prints out the remote socket address (who it was sent from)

    public void run(){
    synchronized(this){
        this.runningThread = Thread.currentThread();
    }
    openServerSocket();
    while(! isStopped()){
        Socket clientSocket = null;
        try {
            clientSocket = this.serverSocket.accept();
        } catch (IOException e) {
            if(isStopped()) {
                System.out.println("Server Stopped.") ;
                return;
            }
            throw new RuntimeException(
                "Error accepting client connection", e);
        }
        this.threadPool.execute(
            new WorkerRunnable(clientSocket,
                "Thread Pooled Server"));
    }
    this.threadPool.shutdown();
    System.out.println("Server Stopped.") ;
}

The problem is. The remote address is supposed to stay fixed (I am working within my own home wifi router). However, the IP address of the sender stays the same, but the port keeps changing!!

This is a big problem for me..as I need to be able to return a response to the user for future tasks and I actually save this address to use again to send data. When I ran this in a single TCP thread..it stayed fixed (the port).

Why does the threadpool cause the TCP remote address port to keep changing?

share|improve this question
add comment

1 Answer

With TCP, the client socket port is most of the time (almost 99%, except for specific protocols) randomly chosen. But to you don't have to know it, the only thing you have to do is to keep the clientSocket reference to write back data to the client. If you want to send data to the other host after that the connection is closed, you have to start a ServerSocket on both sides with a fixed port.

share|improve this answer
 
Can I have more detail on this ServerSocket? What is the difference. I want my server to send data back to the client but I don't know it's IP. And I cant hold a TCP connection open forever can i.. –  raaj Jun 10 '13 at 16:12
 
@user1436508, forever probably not (due to socket timeouts, firewalls, ...) but if you exchange data regularly you can keep it quite a long time, maybe a full day long. You can also set keepalive to true on client side socket.setKeepAlive(true) . If you really want to be able to join the client at any time, you will have to implement your TCP Server on both sides, and i.e. tell the client at the first connection to send to the server the port number it is using. –  gma Jun 11 '13 at 7:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.