how to resolve "ofxNetwork socket address already in use"

I can only run my TCP application once and then it showed me the following error:
OF_ERROR: ofxNetwork:/Users/…/Code/of_preRelease_v0062_osxSL_FAT/apps/examples/…/ofxTCPManager.cpp: 104 EADDRINUSE: the socket address of the given addr is already in use
ofxTCPServer: bind(port = 1638) failed

I tried to close the socket on exit but it doesn’t help. it works again after i restart my laptop, but just once. is there anyway to resolve this? thanks.

Two quick things: a) If you can, you should probably update to 007, it’s available at http://openframeworks.cc/download, there’s a lot of cool new stuff in there and b) depending on how you’ve configured your server you might need to call ofxTCPManager::Close() to ensure that port isn’t in use after the application exit.

Thanks.

  1. probably can’t update it to 007 in a short time
  2. I did ofxTCPServer.close() and ofxTCPManager.close() on exit, but it still doesn’t work. it shows the same error. i run the networkTcpClientExample/networkTcpServerExample, it works fine with other ports. is there any other way to unbind those ports? or make sure socket address is not used after exit.

I’ve never run with that TCP server/client. UDP yes, TCP no. Could you post some of the code that shows how you’re setting up the socket?

It looks like you are on Mac so this is a trick I use as opposed to rebooting your machine.

  • Go to System Preferences>Network

  • In the Location dropdown select “Edit Locations”

  • Hit the + button to create a new Location and call it “DISABLED”

  • Select each Ethernet Adapter on the left and under the “Configure IPv4” drop down select off

So when you get the port error message you can basically reboot your ports by selecting “DISABLED” and Apply and then switching back to your regular Location (usually “Automatic”). Locations also available under the Apple Menu.

There probably is a quicker Terminal way of doing this but this has been the easiest way to show other people not Terminal-friendly

Thanks!
I tried it and also tested different port, but it still gives me the same error:
OF_ERROR: ofxNetwork:/Users/…/Code/of_preRelease_v0062_osxSL_FAT/apps/examples/…/ofxTCPManager.cpp: 104 EADDRINUSE: the socket address of the given addr is already in use
ofxTCPServer: bind(port = 50001) failed

will there be any other reason? I don’t have my firewall on.

here is how i initialize TCPsocket as server:
in testApp.h
ofxTCPServer TCP1;
in testApp.cpp
setup()
TCP1.setup(50001);
update()
// nothing, since i’m not sending anything
draw()
// basically same code from networkTcpServerExample, just print out whatever receives
for(int i = 0; i < TCP1.getNumClients(); i++){
string port = ofToString( TCP1.getClientPort(i) );
string ip = TCP1.getClientIP(i);
string info = "client “+ofToString(i)+” -connected from “+ip+” on port: "+port;
if(i >= storeText.size() ){
storeText.push_back( string() );
}
string str = TCP1.receive(i);
if(str.length() > 0){
storeText[i] = str;
}
char buffer[10];
if (str.length()>0) {
for (int j=0; j<10; j++) {
buffer[j] = str[j];
}
}
cout << str << endl;
}

any idea where might go wrong? it used to run okay without that error when I first finished my app for quite a while.

Hope you already found the solution.

Here is what I did for the same problem. It happens because the socket is still live in the kernel.

So, you can rewrite the Bind() function in ofxTCPManager.cpp to enable reusing a socket.

  
bool ofxTCPManager::Bind(unsigned short usPort)  
{  
	struct sockaddr_in local;  
	memset(&local, 0, sizeof(sockaddr_in));  
    
	local.sin_family = AF_INET;  
	local.sin_addr.s_addr = INADDR_ANY;  
	//Port MUST be in Network Byte Order  
	local.sin_port = htons(usPort);  
    
  int yes=1;  
    
  // lose the pesky "Address already in use" error message  
  if (setsockopt(m_hSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {  
    perror("setsockopt");  
    exit(1);  
  }   
    
	if (bind(m_hSocket,(struct sockaddr*)&local,sizeof(local))){  
		ofxNetworkCheckError();  
		return false;  
	}  
    
	return true;  
}  
  

It works for me pretty well.

And for reference, you can go to : http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#bind

4 Likes