將 Socket 程式碼從 Windows 移植至 Linux,需注意以下關鍵差異:
winsock.h / winsock2.hsys/socket.h、errno.hWSAStartup()closesocket()close()SOCKETint跨平台宏定義建議:
#ifdef WIN32
typedef int socklen_t;
typedef int ssize_t;
#endif
#ifdef __LINUX__
typedef int SOCKET;
typedef unsigned char BYTE;
typedef unsigned long DWORD;
#define FALSE 0
#define SOCKET_ERROR (-1)
#endif
GetLastError() / WSAGetLastError()errno 全域變數ioctlsocket()fcntl()0MSG_NOSIGNAL,否則對方關閉連線時程序可能因 SIGPIPE 異常終止GetTickCount()gettimeofday()| Windows | Linux |
|---|---|
process.h | pthread.h |
_beginthread | pthread_create |
_endthread | pthread_exit |