diff --git a/exec/cnex/lib/net.c b/exec/cnex/lib/net.c index 38205be32..9d76ecf0c 100644 --- a/exec/cnex/lib/net.c +++ b/exec/cnex/lib/net.c @@ -58,6 +58,25 @@ static SOCKET *check_socket(TExecutor *exec, Object *o) return o->ptr; } +static const char *socket_error_message(int err) +{ +#ifdef _WIN32 + static char msgbuf[256]; + DWORD r = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + msgbuf, + sizeof(msgbuf), + NULL); + if (r == 0) { + snprintf(msgbuf, sizeof(msgbuf), "(No Error Text) - %d", err); + } + return msgbuf; +#endif + return strerror(err); +} + #ifdef _WIN32 static void initWinsock() @@ -251,15 +270,14 @@ void net_socket_recv(TExecutor *exec) } int r = recv(s, (char*)buf, n, 0); if (r < 0) { - perror("recv"); - int err = errno; + int err = socket_error(); Cell *ret = cell_createArrayCell(2); Cell *t = cell_arrayIndexForWrite(ret, 0); t->type = cNumber; t->number = number_from_uint32(CHOICE_RecvResult_error); t = cell_arrayIndexForWrite(ret, 1); t->type = cString; - t->string = string_createCString(strerror(err)); + t->string = string_createCString(socket_error_message(err)); push(exec->stack, ret); return; } diff --git a/exec/cnex/lib/socketx.h b/exec/cnex/lib/socketx.h index 65fb0eb61..2bfd4897d 100644 --- a/exec/cnex/lib/socketx.h +++ b/exec/cnex/lib/socketx.h @@ -7,6 +7,7 @@ #pragma comment(lib, "ws2_32.lib") // Include the wsock32 (version 2) library, automatically on Windows builds. typedef int socklen_t; #pragma warning(disable: 4127) // incompatible with FD_SET() +#define socket_error() WSAGetLastError() #else @@ -19,6 +20,7 @@ typedef int socklen_t; #define closesocket(x) close(x) #define INVALID_SOCKET -1 +#define socket_error() errno typedef int SOCKET;