Skip to content

Instantly share code, notes, and snippets.

@RklAlx
RklAlx / 0_reuse_code.js
Created September 27, 2013 10:38
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@RklAlx
RklAlx / ChangeSocketTimeout.cpp
Created September 27, 2013 10:50
Change Socket Timeout
bool ChangeSocketTimeout(int nHeartBeat) // nHeartBeat - value in milliseconds
{
if(setsockopt(m_Socket, SOL_SOCKET, SO_RCVTIMEO, (const char *) &nHeartBeat, sizeof(nHeartBeat)) == SOCKET_ERROR)
{
printf("Error: %d\n", WSAGetLastError());
return false;
}
return true;
}
@RklAlx
RklAlx / InitWinsockDll.cpp
Created September 27, 2013 11:04
WSAStart Initialize WinsockDll
#include <winsock2.h>
#include <iostream>
using namespace std;
bool InitWinsockDLL()
{
WSADATA wsaData;
//
///* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
@RklAlx
RklAlx / SocketServer.cpp
Created September 27, 2013 11:45
Socket Listener
#include "StdAfx.h"
#include "SocketServer.h"
#include <iostream>
using namespace std;
SocketServer::SocketServer()
: m_AccepterSocket(INVALID_SOCKET),
m_nPort(0),
m_dwAccepterThreadID(0)
@RklAlx
RklAlx / Receiver.cpp
Created September 27, 2013 11:46
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
@RklAlx
RklAlx / Sender.cpp
Created September 27, 2013 11:47
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.
@RklAlx
RklAlx / ShowInfo.cpp
Created September 27, 2013 11:49
Hostname
/*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>
void ShowInfo(struct addrinfo *res)
@RklAlx
RklAlx / SocketClient.cpp
Created September 27, 2013 11:55
Socket Client
CMyClient::CMyClient(const char* pIP, unsigned int port)
{
m_Socket = INVALID_SOCKET;
_snprintf_s(this->m_IP,IP_SIZE,_TRUNCATE,pIP);
this->InitSockAddrIn();
}
void CMyClient::InitSockAddrIn()
{
@RklAlx
RklAlx / CC.cpp
Created September 27, 2013 12:00
Private Constructor and make_shared: Solution: Using Pass-Key-Idiom Pattern URL: http://anthony-arnold.com/2012/04/05/three-fun-cpp-techniques/
#include "C.h"
std::shared_ptr<CC> CC::CreateCC(int x)
{
return std::make_shared<CC>(CC::Key(),x);
}
CC::CC(const Key& rk, int xy)
{
y = xy;
@RklAlx
RklAlx / UniquePtr.cpp
Created September 27, 2013 12:01
std::unique_ptr
std::unique_ptr<int> p1(new int(5));
std::unique_ptr<int> p2 = p1; //Compile error.
std::unique_ptr<int> p3 = std::move(p1); //Transfers ownership. p3 now owns the memory and p1 is rendered invalid.
p3.reset(); //Deletes the memory.
p1.reset(); //Does nothing.