Skip to content

Instantly share code, notes, and snippets.

@RklAlx
Created September 27, 2013 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RklAlx/6727398 to your computer and use it in GitHub Desktop.
Save RklAlx/6727398 to your computer and use it in GitHub Desktop.
Sender
/*NOTA:
1) tem que se garantir que o windows.h é compilado sempre após o ws2tcpip.h,
caso contrário começa a indicar que há referencias duplicadas.
2) Quando se trabalha com sockets tem sempre que se inicializar a WinSockDLL -> tag WSAStartup
Send function:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740149%28v=vs.85%29.aspx
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.
*/
#include <ws2tcpip.h>
#include <windows.h>
bool CClient::SendLine(std::string& rMessage)
{
int bytes_sent = 0;
int total_bytes_sent = 0;
while(total_bytes_sent < rMessage.size())
{
bytes_sent = send(m_Socket, (rMessage.c_str() + total_bytes_sent), rMessage.size() - total_bytes_sent, 0);
if(bytes_sent == SOCKET_ERROR)
{
cout << "Failed. Error: " << WSAGetLastError() << endl;
return false;
}
total_bytes_sent += bytes_sent;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment