Socket相關(guān)程序:從Windows移植到Linux
socket相關(guān)程序從windows移植到linux下需要注意的
1)頭文件
windows下winsock.h/winsock2.h
linux下sys/socket.h
錯誤處理:errno.h
2)初始化
windows下需要用WSAStartup
linux下不需要
3)關(guān)閉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)設(shè)置非阻塞
windows下ioctlsocket()
linux下fcntl()
7)send函數(shù)最后一個參數(shù)
windows下一般設(shè)置為0
linux下最好設(shè)置為MSG_NOSIGNAL,如果不設(shè)置,在發(fā)送出錯后有可 能會導(dǎo)致程序退出。
8)毫秒級時間獲取
windows下GetTickCount()
linux下gettimeofday()
多線程
多線程: (win)process.h --〉(linux)pthread.h
_beginthread --> pthread_create
_endthread --> pthread_exit
相關(guān)文章:
1. NT6 HDD Installer全攻略:如何硬盤安裝Windows 72. 被忽略的Windows Server 2008幾大特性3. Windows系統(tǒng)如何添加虛擬網(wǎng)卡?4. 當我們厭倦了Windows系統(tǒng)還能考慮什么樣的桌面操作系統(tǒng)?5. windows10系統(tǒng)經(jīng)常死機錯誤id60086. Windows7小技巧 教你玩轉(zhuǎn)系統(tǒng)時鐘7. 微軟Windows 8外泄文件透露 蘋果為其目標8. Windows7新建日記本文檔的方法9. Windows7系統(tǒng)如何防ARP攻擊?10. Windows10網(wǎng)絡(luò)診斷DNS服務(wù)器未響應(yīng)的解決辦法
