socket在Linux與windows下的差異

如無其它說明,本文所指Linux均表示2.6內核Linux,GCC編譯器,Windows均表示Windows XP系統,Visual Studio 2005 sp1編譯環境。

Linux要包含

[cpp] 
#include <sys/socket.h>   
#include <netinet/in.h>   
#include <netdb.h>   
#include <arpa/inet.h>  
等頭文件,而windows下則是包含
[cpp] 

#include <winsock.h>  

Linux中socket爲整形,Windows中爲一個SOCKET。
Linux中關閉socket爲close,Windows中爲closesocket。
Linux中有變量socklen_t,Windows中直接爲int。
因爲linux中的socket與普通的fd一樣,所以可以在TCP的socket中,發送與接收數據時,直接使用read和write。而windows只能使用recv和send。
設置socet選項,比如設置socket爲非阻塞的。Linux下爲

[cpp] 
flag = fcntl (fd, F_GETFL);  
fcntl (fd, F_SETFL, flag | O_NONBLOCK);  
,Windows下爲
[cpp] 
flag = 1;  
ioctlsocket (fd, FIONBIO, (unsigned long *) &flag);  

當非阻塞socket的TCP連接正在進行時,Linux的錯誤號爲EINPROGRESS,Windows的錯誤號爲WSAEWOULDBLOCK。


file
Linux下面,文件換行是"\n",而windows下面是"\r\n"。
Linux下面,目錄分隔符是"/",而windows下面是"\"。
Linux與Windows下面,均可以使用stat調用來查詢文件信息。但是,Linux只支持2G大小,而Windows只支持4G大小。爲了支持更大的文件查詢,可以在Linux環境下加


_FILE_OFFSET_BITS=64定義,在Windows下面使用_stat64調用,入參爲struct __stat64。
Linux中可根據stat的st_mode判斷文件類型,有S_ISREG、S_ISDIR等宏。Windows中沒有,需要自己定義相應的宏,如


[cpp] 
#define S_ISREG(m) (((m) & 0170000) == (0100000))   
#define S_ISDIR(m) (((m) & 0170000) == (0040000))  
Linux中刪除文件是unlink,Windows中爲DeleteFile。

time

Linux中,time_t結構是長整形。而windows中,time_t結構是64位的整形。如果要在windows始time_t爲32位無符號整形,可以加宏定義,_USE_32BIT_TIME_T。
Linux中,sleep的單位爲秒。Windows中,Sleep的單位爲毫秒。即,Linux下sleep (1),在Windows環境下則需要Sleep (1000)。
Windows中的timecmp宏,不支持大於等於或者小於等於。
Windows中沒有struct timeval結構的加減宏可以使用,需要手動定義:


[cpp] 
#define MICROSECONDS (1000 * 1000)   
  
#define timeradd(t1, t2, t3) do {                                                          \   
  (t3)->tv_sec = (t1)->tv_sec + (t2)->tv_sec;                                              \  
  (t3)->tv_usec = (t1)->tv_usec + (t2)->tv_usec % MICROSECONDS;                            \  
  if ((t1)->tv_usec + (t2)->tv_usec > MICROSECONDS) (t3)->tv_sec ++;                       \  
} while (0)  
  
#define timersub(t1, t2, t3) do {                                                          \   
  (t3)->tv_sec = (t1)->tv_sec - (t2)->tv_sec;                                              \  
  (t3)->tv_usec = (t1)->tv_usec - (t2)->tv_usec;                                           \  
  if ((t1)->tv_usec - (t2)->tv_usec < 0) (t3)->tv_usec --, (t3)->tv_usec += MICROSECONDS;  \  
} while (0)  


調用進程

Linux下可以直接使用system來調用外部程序。Windows最好使用WinExec,因爲WinExec可以支持是打開還是隱藏程序窗口。用WinExec的第二個入參指明,如

SW_SHOW/SW_HIDE。

雜項

Linux爲srandom和random函數,Windows爲srand和rand函數。
Linux爲snprintf,Windows爲_snprintf。
同理,Linux中的strcasecmp,Windows爲_stricmp。


錯誤處理

Linux下面,通常使用全局變量errno來表示函數執行的錯誤號。Windows下要使用GetLastError ()調用來取得。


Linux環境下僅有的
這些函數或者宏,Windows中完全沒有,需要用戶手動實現。
atoll

[cpp] 
long long  
atoll (const char *p)  
{  
  int minus = 0;  
  long long value = 0;  
  if (*p == '-')  
    {  
      minus ++;  
      p ++;  
    }  
  while (*p >= '0' && *p <= '9')  
    {  
      value *= 10;  
      value += *p - '0';  
      p ++;  
    }  
  return minus ? 0 - value : value;  
}  
gettimeofday


[cpp] 
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)   
#define EPOCHFILETIME  11644473600000000Ui64   
#else   
#define EPOCHFILETIME  11644473600000000ULL   
#endif   
  
struct timezone  
{  
  int tz_minuteswest;  
  int tz_dsttime;  
};  
  
int  
gettimeofday (struct timeval *tv, struct timezone *tz)  
{  
  FILETIME ft;  
  LARGE_INTEGER li;  
  __int64 t;  
  static int tzflag;  
  
  if (tv)  
    {  
      GetSystemTimeAsFileTime (&ft);  
      li.LowPart = ft.dwLowDateTime;  
      li.HighPart = ft.dwHighDateTime;  
      t = li.QuadPart;      /* In 100-nanosecond intervals */  
      t -= EPOCHFILETIME;   /* Offset to the Epoch time */  
      t /= 10;          /* In microseconds */  
      tv->tv_sec = (long) (t / 1000000);  
      tv->tv_usec = (long) (t % 1000000);  
    }  
  
  if (tz)  
    {  
      if (!tzflag)  
    {  
      _tzset ();  
      tzflag++;  
    }  
      tz->tz_minuteswest = _timezone / 60;  
      tz->tz_dsttime = _daylight;  
    }  
  
  return 0;  
}  

編譯相關
當前函數,Linux用__FUNCTION__表示,Windows用__func__表示。
--------------------------------------------------------------------------------
Socket 編程 windows到Linux代碼移植遇到的問題
1)頭文件  
windows下winsock.h/winsock2.h  
linux下sys/socket.h  
錯誤處理:errno.h  


2)初始化  
windows下需要用WSAStartup  
linux下不需要  


3)關閉socket  
windows下closesocket(...)  
linux下close(...)  

4)類型  
windows下SOCKET  
linux下int  
如我用到的一些宏:  
#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  


5)獲取錯誤碼  
windows下getlasterror()/WSAGetLastError()  
linux下errno變量  


6)設置非阻塞  
windows下ioctlsocket()  
linux下fcntl() <fcntl.h>  

7)send函數最後一個參數  
windows下一般設置爲0  
linux下最好設置爲MSG_NOSIGNAL,如果不設置,在發送出錯後有可 能會導致程序退出。  


8)毫秒級時間獲取  
windows下GetTickCount()  
linux下gettimeofday()  


3、多線程  
多線程: (win)process.h --〉(linux)pthread.h  
_beginthread --> pthread_create  
_endthread --> pthread_exit
-----------------------------------------------------------------
windows與linux平臺使用的socket均繼承自Berkeley socket(rfc3493),他們都支持select I/O模型,均支持使用getaddrinfo與getnameinfo實現協議無關編程。但存在細微差別,

主要有:

頭文件及類庫。windows使用winsock2.h(需要在windows.h前包含),並要鏈接庫ws2_32.lib;linux使用netinet/in.h, netdb.h等。
windows下在使用socket之前與之後要分別使用WSAStartup與WSAClean。
關閉socket,windows使用closesocket,linux使用close。
send*與recv*函數參數之socket長度的類型,windows爲int,linux爲socklen_t,可預編譯指令中處理這一差異,當平臺爲windows時#define socklen_t unsigned int。
select函數第一個參數,windows忽略該參數,linux下該參數表示集合中socket的上限值,一般設爲sockfd(需select的socket) + 1。
windows下socket函數返回值類型爲SOCKET(unsigned int),其中發生錯誤時返回INVALID_SOCKET(0),linux下socket函數返回值類型int, 發生錯誤時返回-1。
另外,如果綁定本機迴環地址,windows下sendto函數可以通過,linux下sendto回報錯:errno=22, Invalid arguement。一般情況下均綁定通配地址。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章