Commit c6eaca57 authored by Davis King's avatar Davis King

Changed code so that the proper error code is always returned even in the event

that closesocket() fails.
parent 7e09a085
...@@ -205,7 +205,7 @@ namespace dlib ...@@ -205,7 +205,7 @@ namespace dlib
hostent* address; hostent* address;
unsigned long ipnum = inet_addr(ip.c_str()); unsigned long ipnum = inet_addr(ip.c_str());
// if inet_addr coudln't convert ip then return an error // if inet_addr couldn't convert ip then return an error
if (ipnum == INADDR_NONE) if (ipnum == INADDR_NONE)
{ {
return OTHER_ERROR; return OTHER_ERROR;
...@@ -693,7 +693,7 @@ namespace dlib ...@@ -693,7 +693,7 @@ namespace dlib
{ {
// if there is a specific ip to listen on // if there is a specific ip to listen on
sa.sin_addr.S_un.S_addr = inet_addr(ip.c_str()); sa.sin_addr.S_un.S_addr = inet_addr(ip.c_str());
// if inet_addr cound't convert the ip then return an error // if inet_addr couldn't convert the ip then return an error
if ( sa.sin_addr.S_un.S_addr == INADDR_NONE ) if ( sa.sin_addr.S_un.S_addr == INADDR_NONE )
{ {
closesocket(sock); closesocket(sock);
...@@ -707,11 +707,13 @@ namespace dlib ...@@ -707,11 +707,13 @@ namespace dlib
// bind the new socket to the requested port and ip // bind the new socket to the requested port and ip
if (bind(sock,reinterpret_cast<sockaddr*>(&sa),sizeof(sockaddr_in))==SOCKET_ERROR) if (bind(sock,reinterpret_cast<sockaddr*>(&sa),sizeof(sockaddr_in))==SOCKET_ERROR)
{ // if there was an error {
const int err = WSAGetLastError();
// if there was an error
closesocket(sock); closesocket(sock);
// if the port is already bound then return PORTINUSE // if the port is already bound then return PORTINUSE
if (WSAGetLastError() == WSAEADDRINUSE) if (err == WSAEADDRINUSE)
return PORTINUSE; return PORTINUSE;
else else
return OTHER_ERROR; return OTHER_ERROR;
...@@ -721,11 +723,12 @@ namespace dlib ...@@ -721,11 +723,12 @@ namespace dlib
// tell the new socket to listen // tell the new socket to listen
if ( listen(sock,SOMAXCONN) == SOCKET_ERROR) if ( listen(sock,SOMAXCONN) == SOCKET_ERROR)
{ {
const int err = WSAGetLastError();
// if there was an error return OTHER_ERROR // if there was an error return OTHER_ERROR
closesocket(sock); closesocket(sock);
// if the port is already bound then return PORTINUSE // if the port is already bound then return PORTINUSE
if (WSAGetLastError() == WSAEADDRINUSE) if (err == WSAEADDRINUSE)
return PORTINUSE; return PORTINUSE;
else else
return OTHER_ERROR; return OTHER_ERROR;
...@@ -810,7 +813,7 @@ namespace dlib ...@@ -810,7 +813,7 @@ namespace dlib
foreign_sa.sin_port = htons(foreign_port); foreign_sa.sin_port = htons(foreign_port);
foreign_sa.sin_addr.S_un.S_addr = inet_addr(foreign_ip.c_str()); foreign_sa.sin_addr.S_un.S_addr = inet_addr(foreign_ip.c_str());
// if inet_addr cound't convert the ip then return an error // if inet_addr couldn't convert the ip then return an error
if ( foreign_sa.sin_addr.S_un.S_addr == INADDR_NONE ) if ( foreign_sa.sin_addr.S_un.S_addr == INADDR_NONE )
{ {
closesocket(sock); closesocket(sock);
...@@ -852,11 +855,13 @@ namespace dlib ...@@ -852,11 +855,13 @@ namespace dlib
sizeof(sockaddr_in) sizeof(sockaddr_in)
) == SOCKET_ERROR ) == SOCKET_ERROR
) )
{ // if there was an error {
const int err = WSAGetLastError();
// if there was an error
closesocket(sock); closesocket(sock);
// if the port is already bound then return PORTINUSE // if the port is already bound then return PORTINUSE
if (WSAGetLastError() == WSAEADDRINUSE) if (err == WSAEADDRINUSE)
return PORTINUSE; return PORTINUSE;
else else
return OTHER_ERROR; return OTHER_ERROR;
...@@ -870,9 +875,10 @@ namespace dlib ...@@ -870,9 +875,10 @@ namespace dlib
) == SOCKET_ERROR ) == SOCKET_ERROR
) )
{ {
const int err = WSAGetLastError();
closesocket(sock); closesocket(sock);
// if the port is already bound then return PORTINUSE // if the port is already bound then return PORTINUSE
if (WSAGetLastError() == WSAEADDRINUSE) if (err == WSAEADDRINUSE)
return PORTINUSE; return PORTINUSE;
else else
return OTHER_ERROR; return OTHER_ERROR;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment