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
hostent* address;
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)
{
return OTHER_ERROR;
......@@ -693,7 +693,7 @@ namespace dlib
{
// if there is a specific ip to listen on
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 )
{
closesocket(sock);
......@@ -707,11 +707,13 @@ namespace dlib
// bind the new socket to the requested port and ip
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);
// if the port is already bound then return PORTINUSE
if (WSAGetLastError() == WSAEADDRINUSE)
if (err == WSAEADDRINUSE)
return PORTINUSE;
else
return OTHER_ERROR;
......@@ -721,11 +723,12 @@ namespace dlib
// tell the new socket to listen
if ( listen(sock,SOMAXCONN) == SOCKET_ERROR)
{
const int err = WSAGetLastError();
// if there was an error return OTHER_ERROR
closesocket(sock);
// if the port is already bound then return PORTINUSE
if (WSAGetLastError() == WSAEADDRINUSE)
if (err == WSAEADDRINUSE)
return PORTINUSE;
else
return OTHER_ERROR;
......@@ -810,7 +813,7 @@ namespace dlib
foreign_sa.sin_port = htons(foreign_port);
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 )
{
closesocket(sock);
......@@ -852,11 +855,13 @@ namespace dlib
sizeof(sockaddr_in)
) == SOCKET_ERROR
)
{ // if there was an error
{
const int err = WSAGetLastError();
// if there was an error
closesocket(sock);
// if the port is already bound then return PORTINUSE
if (WSAGetLastError() == WSAEADDRINUSE)
if (err == WSAEADDRINUSE)
return PORTINUSE;
else
return OTHER_ERROR;
......@@ -870,9 +875,10 @@ namespace dlib
) == SOCKET_ERROR
)
{
const int err = WSAGetLastError();
closesocket(sock);
// if the port is already bound then return PORTINUSE
if (WSAGetLastError() == WSAEADDRINUSE)
if (err == WSAEADDRINUSE)
return PORTINUSE;
else
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