Added SO_REUSEADDR and SO_REUSEPORT for JSON & MJPEG streams

This commit is contained in:
AlexeyAB
2019-01-11 23:11:25 +03:00
parent 937306f0c9
commit eb64e2b13c

View File

@ -24,7 +24,7 @@ struct _INIT_W32DATA
// Graceful closes will first close their output channels and then wait for the peer
// on the other side of the connection to close its output channels. When both sides are done telling
// each other they won<EFBFBD>t be sending any more data (i.e., closing output channels),
// each other they won,t be sending any more data (i.e., closing output channels),
// the connection can be closed fully, with no risk of reset.
static int close_socket(SOCKET s) {
int close_output = ::shutdown(s, 1); // 0 close input, 1 close output, 2 close both
@ -153,6 +153,14 @@ public:
address.sin_addr.s_addr = INADDR_ANY;
address.sin_family = AF_INET;
address.sin_port = htons(port); // ::htons(port);
int reuse = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0)
cerr << "setsockopt(SO_REUSEADDR) failed" << endl;
#ifdef SO_REUSEPORT
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0)
cerr << "setsockopt(SO_REUSEPORT) failed" << endl;
#endif
if (::bind(sock, (SOCKADDR*)&address, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
cerr << "error MJPG_sender: couldn't bind sock " << sock << " to port " << port << "!" << endl;
@ -252,6 +260,10 @@ public:
}
}
}
if (close_all_sockets) {
int result = close_socket(sock);
printf("MJPG_sender: close acceptor: %d \n\n", result);
}
return true;
}
};
@ -320,6 +332,14 @@ public:
address.sin_addr.s_addr = INADDR_ANY;
address.sin_family = AF_INET;
address.sin_port = htons(port); // ::htons(port);
int reuse = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0)
cerr << "setsockopt(SO_REUSEADDR) failed" << endl;
#ifdef SO_REUSEPORT
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0)
cerr << "setsockopt(SO_REUSEPORT) failed" << endl;
#endif
if (::bind(sock, (SOCKADDR*)&address, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
cerr << "error JSON_sender: couldn't bind sock " << sock << " to port " << port << "!" << endl;
@ -419,6 +439,10 @@ public:
}
}
}
if (close_all_sockets) {
int result = close_socket(sock);
printf("JSON_sender: close acceptor: %d \n\n", result);
}
return true;
}
};