send function(Windows)

SOCKET

實質(zhì)是一個內(nèi)核對象的句柄. 創(chuàng)建一個socket,就是在系統(tǒng)內(nèi)部創(chuàng)建了一個內(nèi)核對象.

send

The Windows Sockets send function sends data on a connected socket.

int send (SOCKET s, const char FAR * buf, int len, int flags); 

Parameters

  • s [in]
    A descriptor identifying a connected socket.
  • buf [in]
    A buffer containing the data to be transmitted.
  • len [in]
    The length of the data in buf.
  • flags [in]
    An indicator specifying the way in which the call is made.

Remarks

  • outgoing data
    The send function is used to write outgoing data on a connected socket.

  • send completion
    The successful completion of a send does not indicate that the data was successfully delivered.
    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 a 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 client and server machines.

  • message data is too long
    For message-oriented sockets, care must be taken not to exceed the maximum packet size of the underlying provider, which can be obtained by using getsockopt to retrieve the value of socket option SO_MAX_MSG_SIZE.
    If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned, and no data is transmitted.

Return Values

If no error occurs, send returns the total number of bytes sent, which can be less than the number indicated by len for nonblocking sockets.
Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Error Codes

  • WSANOTINITIALISED
    A successful WSAStartup must occur before using this function.
  • WSAEWOULDBLOCK
    The socket is marked as nonblocking and the requested operation would block.
  • WSAEMSGSIZE
    The socket is message oriented, and the message is larger than the maximum supported by the underlying transport.
  • WSAETIMEDOUT
    The connection has been dropped, because of a network failure or because the system on the other end went down without notice.

Using Example

int iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0); 
if (SOCKET_ERROR == iResult) 
{ 
    wprintf(L"send failed with error: %d\n", WSAGetLastError()); 
    closesocket(ConnectSocket); 
    WSACleanup(); 
    return 1;
 }

References
https://msdn.microsoft.com/en-us/library/ms740149(VS.85).aspx

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容