Skip to content

Instantly share code, notes, and snippets.

@RklAlx
Created September 27, 2013 11:46
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/6727394 to your computer and use it in GitHub Desktop.
Save RklAlx/6727394 to your computer and use it in GitHub Desktop.
Receiver
/*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 */
#include <ws2tcpip.h>
#include <windows.h>
DWORD WINAPI CMyObjec::Receiver(LPVOID arg) // Receive From Client
{
CMyObject* _this = (CMyObject *) arg;
char buffer[1000] = "";
int n_bytes_rcv = 0;
while(_this->m_Connected)
{
n_bytes_rcv = recv(_this->m_Socket,buffer,sizeof(buffer),0);
if(n_bytes == 0)
{
// Connection was closed gracefully
return 0;
}
if(n_bytes_rcv < 0)
{
cout << "Error: " << WSAGetLastError() << endl;
return 1;
}
buffer[n_bytes_rcv] = '\0';
_this->HandleMessage(buffer);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment