Skip to content

Commit 4ce303b

Browse files
committed
Add (hacky) possibility of handling of EADDRINUSE
1 parent fe1676b commit 4ce303b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

netsock.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static const char* inet_ntop(int af, const void* src, char* dst, DWORD cnt) {
9292
# define WSAEINTR EINTR
9393
# define WSAEINPROGRESS EINPROGRESS
9494
# define WSAEMSGSIZE EMSGSIZE
95+
# define WSAEADDRINUSE EADDRINUSE
9596
#endif
9697

9798
static const char* error_string(int err = last_error) {
@@ -116,6 +117,8 @@ static bool have_inited_sockets = false;
116117
static WSADATA wsaData;
117118
#endif
118119

120+
const std::string Sock::ADDRESS_IN_USE = "Unable to bind: Address already in use";
121+
119122
static void init_sockets() {
120123
have_inited_sockets = true;
121124
#ifdef __WIN32__
@@ -593,7 +596,11 @@ bool ServerSock::SubBind(std::string& error_out, const char* bind_address,
593596
case IPVersion::V6: addr.in6.sin6_port = htons(port); break;
594597
}
595598
if(bind(sock, &addr.faceless, addr.Length())) {
596-
error_out = std::string("Unable to bind: ") + error_string();
599+
int err = last_error;
600+
if(err == WSAEADDRINUSE)
601+
error_out = ADDRESS_IN_USE;
602+
else
603+
error_out = std::string("Unable to bind: ") + error_string(last_error);
597604
Close();
598605
return false;
599606
}

netsock.hh

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ namespace Net {
123123
bool blocking = false);
124124
void Become(SOCKET sock, bool blocking = false);
125125
public:
126+
// If a Bind function puts this exact value into error_out, EADDRINUSE has
127+
// occurred. The bind may succeed if you wait and retry.
128+
// Special handling of this is NOT required.
129+
static const std::string ADDRESS_IN_USE;
126130
inline Sock(Sock&& other) {
127131
if(&other == this) return;
128132
sock = other.sock;

0 commit comments

Comments
 (0)