TCP server : socket not closing (already in use)

Hi everyone,

I’m trying to set up a TCP server application using ofxNetwork.
In the ofApp::exit function I use tcpServer.close() to free the socket.
It usually works but I’m getting problems in a particular situation :
if a client is still connected before the server leaves, the socket is left open (next time I start the server it says “socket already in use”) although close() returns true. In any other situation, everything goes fine.
I tried disconnectAllClients() before close(), with no results.

Is there any other operation I should do before trying to close the socket ?
Thank you for reading,

François

I happen to have the same problem as well. I even tried building logic into the TCP.Setup to cycle through ports until it found one free, and its as if it won’t connect on any. Eventually if I wait long enough before re-launching the application, the ports open up again. This is on OSX.

port = 14000;
do {
TCP.setup(port);
port++;
}
while (!TCP.isConnected() && port < 16000);
port–;

Would be curious to hear if you find anything.

it would help a lot to have a minimal example with code that demos the problem.

Shure, here it is:

ofApp.h server

ofxTCPServer server;
void exit();

ofApp.cpp server

void ofApp::setup(){
    server.setup(10121);
    ofBackground(0);
}

void ofApp::update(){
    if(server.isConnected()){
        for(int i=0; i<server.getLastID(); i++){
            if(server.isClientConnected(i)){
                string rx = server.receive(i);
                if(rx!="")
                cout<<rx<<endl;
            }
        }
    }
    else{
        static int time = ofGetElapsedTimeMillis();
        if(ofGetElapsedTimeMillis()-time>5000){
            server.close();
            server.setup(10121);
            time = ofGetElapsedTimeMillis();
        }
    }
}

void ofApp::exit(){
    if(server.isConnected()){
        server.close();
    }
}

ofApp.h client

ofxTCPClient client;

ofApp.cpp client

void ofApp::setup() {
    client.setup("127.0.0.1", 10121);
}

void ofApp::update() {
    if (client.isConnected()) {
        static int time = ofGetElapsedTimeMillis();
        if (ofGetElapsedTimeMillis() - time > 1000) {
            client.send("test");
            time = ofGetElapsedTimeMillis();
        }
    }
    else {
        static int time = ofGetElapsedTimeMillis();
        if (ofGetElapsedTimeMillis() - time > 5000) {
            client.setup("127.0.0.1", 10121);
            time = ofGetElapsedTimeMillis();
        }
    }
}

in which platform is this happening?

also there were a lot of fixes for ofxNetwork in 0.9.1 so be sure you are using the very latest version of OF

this happens on ubuntu 14.04 x64 and of_v0.9.2. Should I upgrade to 0.9.3 ?
I didn’t see any change in ofxNetwork between 9.2 and 9.3.

no 0.9.2 should be ok

In my case, I have no issues with ofxTCPServer creating a socket in Windows after an immediate re-launch of the application that had been previously closed with connected clients. I only have this issue under OSX (El Capitan if that helps in any way). And if you wait long enough after closing the application, then the re-launch will connect to a socket just fine.