1. 程式人生 > >linux 和 windows 下的socket之間的差別

linux 和 windows 下的socket之間的差別

1、socket描述符的定義不同
windows:
套接字描述符為一個局柄SOCKET。
Linux:
套接字描述符為一個int型整數,與其他的檔案描述符沒有差異。

2、錯誤判斷的方式不同
windows:
需要使用WSAGetLastError獲取錯誤碼。
linux:
使用全域性變數errno獲取。

3、connect返回值的不同
windows下:
If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.
On a blocking socket, the return value indicates success or failure of the connection attempt.
With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK.
Linux下:
阻塞模式下,connect連線成功則返回0,不成功則返回-1。
非阻塞模式下,connect直接返回-1,並且errno會被設定為EINPROGRESS。

4、設定阻塞的方式不同
windows下:

ioctlsocket(clientfd, FIONBIO, &arg);

linux下:

flags = fcntl(clientfd, F_GETFL, 0);
flags |= O_NONBLOCK;
iRet = fcntl(clientfd, F_SETFL, flags);

5、send返回值的不同
windows下:
If no error occurs, send returns the total number of bytes sent, which can be less than the number requested to be sent in the len parameter. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode. On nonblocking stream oriented sockets, the number of bytes written can be between 1 and the requested length, depending on buffer availability on both the client and server computers.
linux下:
立即返回,同時向網路中傳送資料;否則,send向網路傳送快取中不能容納的那部分資料,並等待對端確認後再返回(接收端只要將資料收到接收快取中,就會確認,並不一定要等待應用程式呼叫recv);

在非阻塞模式下,send函式的過程僅僅是將資料拷貝到協議棧的快取區而已,如果快取區可用空間不夠,則盡能力的拷貝,返回成功拷貝的大小;如快取區可用空間為0,則返回-1,同時設定errno為EAGAIN.

6.recv返回值的不同
windows:
If no error occurs, recv returns the number of bytes received and the buffer pointed to by the buf parameter will contain this data received. If the connection has been gracefully closed, the return value is zero.
Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
f the datagram or message is larger than the buffer specified, the buffer is filled with the first part of the datagram, and recv generates the error WSAEMSGSIZE. For unreliable protocols (for example, UDP) the excess data is lost; for reliable protocols, the data is retained by the service provider until it is successfully read by calling recv with a large enough buffer.
If no incoming data is available at the socket, the recv call blocks and waits for data to arrive according to the blocking rules defined for WSARecv with the MSG_PARTIAL flag not set unless the socket is nonblocking. In this case, a value of SOCKET_ERROR is returned with the error code set to WSAEWOULDBLOCK. The select, WSAAsyncSelect, or WSAEventSelect functions can be used to determine when more data arrives.
If the socket is connection oriented and the remote side has shut down the connection gracefully, and all data has been received, a recv will complete immediately with zero bytes received. If the connection has been reset, a recv will fail with the error WSAECONNRESET.
linux:

0,獲取成功,會返回讀取到的值大小
== 0, 對端已經優雅的關閉
< 0,socket連接出錯
如果是非阻塞模式的話,返回值小於0,errno被設定為EAGAIN:套接字已標記為非阻塞,而接收操作被阻塞或者接收超時。

7.select的不同

windows:
nfds [in] Ignored. The nfds parameter is included only for compatibility with Berkeley sockets.
The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.
linux:
nfds為select中監視的檔案控制代碼數,一般設為要監視的檔案中的最大檔案號加一。
==0, 超時

0, 可讀寫的檔案總數
<0,select錯誤