SOCKET
实质是一个内核对象的句柄. 创建一个socket,就是在系统内部创建了一个内核对象.
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