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错误

猜你喜欢

转载自blog.csdn.net/xiaokaige198747/article/details/78620438
今日推荐