Skip to content

Commit

Permalink
Add SO_REUSEADDR option to ofxTCPManager and ofxTCPServer. (#5011)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafirstfruit authored and arturoc committed Oct 12, 2019
1 parent 7c06a8f commit d5678cd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
10 changes: 9 additions & 1 deletion addons/ofxNetwork/src/ofxTCPManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool ofxTCPManager::Listen(int iMaxConnections)
return ret;
}

bool ofxTCPManager::Bind(unsigned short usPort)
bool ofxTCPManager::Bind(unsigned short usPort, bool bReuse)
{
struct sockaddr_in local;
memset(&local, 0, sizeof(sockaddr_in));
Expand All @@ -155,6 +155,14 @@ bool ofxTCPManager::Bind(unsigned short usPort)
//Port MUST be in Network Byte Order
local.sin_port = htons(usPort);

if (bReuse) {
int enable = 1;
if (setsockopt(m_hSocket,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0){
ofxNetworkCheckError();
return false;
}
}

if (::bind(m_hSocket,(struct sockaddr*)&local,sizeof(local))){
ofxNetworkCheckError();
return false;
Expand Down
2 changes: 1 addition & 1 deletion addons/ofxNetwork/src/ofxTCPManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class ofxTCPManager
bool Create();
bool Listen(int iMaxConnections);
bool Connect(const char *pAddrStr, unsigned short usPort);
bool Bind(unsigned short usPort);
bool Bind(unsigned short usPort, bool bReuse = false);
bool Accept(ofxTCPManager& sock);
//sends the data, but it is not guaranteed that really all data will be sent
int Send(const char* pBuff, const int iSize);
Expand Down
2 changes: 1 addition & 1 deletion addons/ofxNetwork/src/ofxTCPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool ofxTCPServer::setup(const ofxTCPSettings &settings){
ofLogError("ofxTCPServer") << "setup(): couldn't create server";
return false;
}
if( !TCPServer.Bind(settings.port) ){
if( !TCPServer.Bind(settings.port, settings.reuse) ){
ofLogError("ofxTCPServer") << "setup(): couldn't bind to port " << settings.port;
return false;
}
Expand Down
1 change: 1 addition & 0 deletions addons/ofxNetwork/src/ofxTCPSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ofxTCPSettings {
std::string address;
int port;
bool blocking = false;
bool reuse = false;

std::string messageDelimiter = "[/TCP]";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void ofApp::setup(){

// set other options
//settings.blocking = false;
//settings.reuse = true;
//settings.messageDelimiter = "\n";

TCP.setup(settings);
Expand Down

0 comments on commit d5678cd

Please sign in to comment.