I'm a socket beginner, I'd like to know the best way to send and receive data. I'd like a better explanation of how it works.
I'm a socket beginner, I'd like to know the best way to send and receive data. I'd like a better explanation of how it works.
(Based on data from MSDN )
send function
The send function sends data to a connected socket.
int send ( In SOCKET s, In const char * buf, In int len, In int flags );
s [in] An identification descriptor for the connected socket
buf [in] A pointer to the buffer containing the data to be transmitted
len [in] The length, in bytes, of the data contained in the buffer pointed to by the buf parameter
flags [in] Specifies the manner in which the call is made.
Return Value: If no error occurs, the send function returns the total number of bytes sent. Otherwise, a SOCKET_ERROR value is returned, the error code can be retrieved by calling WSAGetLastError.
Example:
int iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR)
{
wprintf(L"Send Falhou, codigo de erro %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
recv function
int recv ( In SOCKET s, Out char * buf, In int len, In int flags );
s [in] An identification descriptor for the connected socket
buf [out] A pointer to the buffer to receive the input data.
len [in] The length, in bytes, of the data contained in the buffer pointed to by the buf parameter
flags [in] A set of flags that influence the behavior of this function.
Return Value:
If there is no error, the recv function returns the number of bytes received and the buffer pointed to the buf parameter will contain the received data. If the connection was terminated normally, the return value is zero.
Otherwise, a SOCKET_ERROR value is returned, the error code can be retrieved by calling WSAGetLastError.
Example:
int iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes Recebidos: %d\n", iResult);
else if ( iResult == 0 )
printf("Conexao encerrada.\n");
else
printf("recv falhou, codigo de erro: %d\n", WSAGetLastError());