/* bsd2win.h Utility #defines that allow writing of BSD Socket code and having it compile on both Windows and Un*x/Linux, also a bit of code to allow you to write Windows styled code on Un*x/Linux. Author : Jimb Esser Version : 1.1 Windows users link with wsock32.lib (done automatically for you) Un*x users, may need to build with -lpthread */ #ifdef _MSC_VER /* Note that I'm not doing #ifdef _WIN32 here, because cygwin gets _WIN32 defined */ /* Windows only code Make sure to link with wsock32.lib INIT_SOCKETS(); must be the *very first* line after variable declarations and before anything else */ #pragma comment(lib, "wsock32.lib") #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */ #include #include /* For beginthread */ /* These two are needed because of lazy unix programmers not using */ /* unsigned 16-bit integers for addresses :) (often just int is used, which is *not* what an address is) */ #define htons(i) htons((unsigned short)i) #define ntohs(i) htons((unsigned short)i) #undef INIT_SOCKETS #define INIT_SOCKETS() \ { \ WSADATA wsaData; \ if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) \ { \ return -1; \ } \ } #undef DEINIT_SOCKETS #define DEINIT_SOCKETS() WSACleanup() #else /* Unix code */ #define INIT_SOCKETS() {} #define DEINIT_SOCKETS() {} #include #include #include #include /* For TCP_NODELAY */ #include #include #include #include /* for _beginthread equivalent */ typedef int SOCKET; typedef int HRESULT; typedef char *LPSTR; #define SOCKET_ERROR (-1) #define INVALID_SOCKET (~0) #define OutputDebugString(s) #define FAILED(Status) ((HRESULT)(Status)<0) #define Sleep(ms) usleep(ms*1000); #define stricmp strcasecmp /* Un*x doesn't call it stricmp, can you believe it? */ #ifndef _PTHREAD_GLOBAL_HACK /* Hack to make this a "global" (in fact it's a static variable in every file) */ static pthread_t glob_hack_tid; #define _PTHREAD_GLOBAL_HACK #endif /* Note this does *not* return a handle like _beginthread does! */ #define _beginthread(func, stack, data) pthread_create(&glob_hack_tid, NULL, (void*(*)(void*))func, data) #define recv(s, buf, len, ignore) read(s, buf, len) #define send(s, buf, len, ignore) write(s, buf, len) #define closesocket close #endif