CAsyncSocket::SendTo

int SendTo( const void* lpBuf, int nBufLen, UINT nHostPort, LPCTSTR lpszHostAddress = NULL, int nFlags = 0 );

int SendTo( const void* lpBuf, int nBufLen, const SOCKADDR* lpSockAddr, int nSockAddrLen, int nFlags = 0 );

Return Value

If no error occurs, SendTo returns the total number of characters sent. (Note that this can be less than the number indicated by nBufLen.) Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function:

Parameters

lpBuf

A buffer containing the data to be transmitted.

nBufLen

The length of the data in lpBuf in bytes.

nHostPort

The port identifying the socket application.

lpszHostAddress

The network address of the socket to which this object is connected: a machine name such as “ftp.microsoft.com,” or a dotted number such as “128.56.22.8”.

nFlags

Specifies the way in which the call is made. The semantics of this function are determined by the socket options and the nFlags parameter. The latter is constructed by combining any of the following values with the C++ OR operator:

lpSockAddr

A pointer to a SOCKADDR structure that contains the address of the target socket.

nSockAddrLen

The length of the address in lpSockAddr in bytes.

Remarks

Call this member function to send data to a specific destination. SendTo is used on datagram or stream sockets and is used to write outgoing data on a socket. For datagram sockets, care must be taken not to exceed the maximum IP packet size of the underlying subnets, which is given by the iMaxUdpDg element in the WSADATA structure filled out by AfxSocketInit. If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned, and no data is transmitted.

Note that the successful completion of a SendTo does not indicate that the data was successfully delivered.

SendTo is only used on a SOCK_DGRAM socket to send a datagram to a specific socket identified by the lpSockAddr parameter.

To send a broadcast (on a SOCK_DGRAM only), the address in the lpSockAddr parameter should be constructed using the special IP address INADDR_BROADCAST (defined in the Windows Sockets header file WINSOCK.H) together with the intended port number. Or, if the lpszHostAddress parameter is NULL, the socket is configured for broadcast. It is generally inadvisable for a broadcast datagram to exceed the size at which fragmentation can occur, which implies that the data portion of the datagram (excluding headers) should not exceed 512 bytes.

CAsyncSocket OverviewClass MembersHierarchy Chart

See Also   CAsyncSocket::Create, CAsyncSocket::Receive, CAsyncSocket::ReceiveFrom, CAsyncSocket::Send