Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created May 19, 2014 00:36
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 johnhmj/c9df188c8b925152bd61 to your computer and use it in GitHub Desktop.
Save johnhmj/c9df188c8b925152bd61 to your computer and use it in GitHub Desktop.
client of Winsock localhost connection in May 19, 2014
#include "w32socket.h"
#include <iostream>
#include <Windows.h>
//
int main(int argc, char* argv[])
{
char domain[] = "localhost";
char ip[] = "127.0.0.1";
char request[] = "from client\n";
char buffer[BUF_SIZE];
CSocketNode client;
client.Initialize();
//
//client.getIPFromHost(domain, sizeof(domain) / sizeof(char));
//
client.Socket(ip, sizeof(ip)/sizeof(char));
while (! client.Connect())
{
int n = 0;
std::cout << "LOG: connection successful with server" << std::endl;
while (1)
{
n = client.Send(request, sizeof(request) / sizeof(char));
if (n < 0)
{
break;
}
n = client.Recv(buffer, sizeof(buffer) / sizeof(char));
if (n < 0)
{
break;
}
if (n > 0)
{
std::cout << "MESSAGE: " << buffer;
}
Sleep(500);
}
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
std::cout << std::endl;
std::cout << "Press any key to continue ...";
std::cin.get();
return 0;
}
#include "w32socket.h"
//
CSocketNode::CSocketNode()
{
memset((void*)&this->m_wsadata, 0, sizeof(struct WSAData));
memset((void*)&this->m_saddr, 0, sizeof(struct sockaddr_in));
this->m_socket = INVALID_SOCKET;
this->m_AFamily = AF_INET;
this->m_Port = 0;
this->m_Hostent = NULL;
this->m_InitializeFail = -1;
this->m_IP = new char[BUF_SIZE];
this->m_Domain = new char[BUF_SIZE];
memset((void*)this->m_IP, 0, sizeof(char)*BUF_SIZE);
memset((void*)this->m_Domain, 0, sizeof(char)*BUF_SIZE);
}
CSocketNode::~CSocketNode()
{
if (this->m_socket != INVALID_SOCKET)
{
closesocket(this->m_socket);
}
if (this->m_IP != NULL)
{
delete [] this->m_IP;
}
if (this->m_Domain != NULL)
{
delete [] this->m_Domain;
}
WSACleanup();
}
int CSocketNode::Initialize(void)
{
int n = WSAStartup(MAKEWORD(2,2), &(this->m_wsadata));
if (n != 0)
{
this->m_InitializeFail = -1;
}
else
{
this->m_InitializeFail = 0;
}
return this->m_InitializeFail;
}
int CSocketNode::Socket(unsigned short port)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
this->m_Port = port;
this->m_saddr.sin_family = this->m_AFamily;
this->m_saddr.sin_port = htons(this->m_Port);
this->m_socket = socket(this->m_saddr.sin_family, SOCK_STREAM, IPPROTO_TCP);
if (this->m_socket == INVALID_SOCKET)
{
return -1;
}
return 0;
}
int CSocketNode::Socket(const char* buffer, unsigned int length, unsigned short port)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
memcpy((void*)this->m_IP, (void*)buffer, length);
this->m_Port = port;
this->m_saddr.sin_family = this->m_AFamily;
this->m_saddr.sin_addr.S_un.S_addr = inet_addr(this->m_IP);
this->m_saddr.sin_port = htons(this->m_Port);
this->m_socket = socket(this->m_saddr.sin_family, SOCK_STREAM, IPPROTO_TCP);
if (this->m_socket == INVALID_SOCKET)
{
return -1;
}
return 0;
}
int CSocketNode::Bind(void)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
if (this->m_socket == INVALID_SOCKET)
{
return -1;
}
int n = bind(this->m_socket, (const sockaddr*)&this->m_saddr, sizeof(sockaddr_in));
if (n != 0)
{
return -1;
}
return 0;
}
int CSocketNode::Listen(void)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
if (this->m_socket == INVALID_SOCKET)
{
return -1;
}
int n = listen(this->m_socket, SOMAXCONN);
if (n != 0)
{
return -1;
}
return 0;
}
int CSocketNode::Accept(CSocketNode& client)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
if (this->m_socket == INVALID_SOCKET)
{
return -1;
}
int addrlen = sizeof(sockaddr_in);
client.m_socket = accept(this->m_socket, (sockaddr*)&client.m_saddr, &addrlen);
if (client.m_socket == INVALID_SOCKET)
{
return -1;
}
return 0;
}
int CSocketNode::Connect(void)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
if (this->m_socket == INVALID_SOCKET)
{
return -1;
}
int n = connect(this->m_socket, (SOCKADDR*)&this->m_saddr, sizeof(struct sockaddr_in));
if (n != 0)
{
return -1;
}
return 0;
}
int CSocketNode::Send(const char* buffer, unsigned int length)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
if (this->m_socket == INVALID_SOCKET)
{
return -1;
}
int n = send(this->m_socket, buffer, length, 0);
if (n < 0)
{
return -1;
}
if (n > 0)
{
return 1;
}
return 0;
}
int CSocketNode::Send(SOCKET client, const char* buffer, unsigned int length)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
if (client == INVALID_SOCKET)
{
return -1;
}
int n = send(client, buffer, length, 0);
if (n < 0)
{
return -1;
}
if (n > 0)
{
return 1;
}
return 0;
}
int CSocketNode::Recv(char* buffer, unsigned int length)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
if (this->m_socket == INVALID_SOCKET)
{
return -1;
}
memset((void*)buffer, 0, length);
int n = recv(this->m_socket, buffer, length, 0);
if (n < 0)
{
return -1;
}
if (n > 0)
{
return 1;
}
return 0;
}
int CSocketNode::Recv(SOCKET client, char* buffer, unsigned int length)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
if (client == INVALID_SOCKET)
{
return -1;
}
memset((void*)buffer, 0, length);
int n = recv(client, buffer, length, 0);
if (n < 0)
{
return -1;
}
if (n > 0)
{
return 1;
}
return 0;
}
int CSocketNode::getIPFromHost(const char* buffer, unsigned int length)
{
if (this->m_InitializeFail == -1)
{
return -1;
}
char* p = NULL;
unsigned int len = 0;
memcpy((void*)this->m_Domain, (void*)buffer, length);
this->m_Hostent = gethostbyname(this->m_Domain);
if (this->m_Hostent == NULL)
{
return -1;
}
memcpy((void*)&this->m_saddr.sin_addr, (void*)this->m_Hostent->h_addr_list[0], this->m_Hostent->h_length);
p = inet_ntoa(this->m_saddr.sin_addr);
while (p[len ++] != NULL);
memcpy((void*)this->m_IP, (void*)p, len);
return 0;
}
SOCKET CSocketNode::getSocket(void)
{
return this->m_socket;
}
#ifndef _W32SOCKET_H_
#define _W32SOCKET_H_
#pragma comment(lib, "Ws2_32.lib")
#define FD_SETSIZE 128
#include <WinSock2.h>
#include <list>
typedef std::list<SOCKET> ListSocket;
//
// buffer size = 4096 bytes
//
#define BUF_SIZE 4096
//
// default port = 10
// port = 0 ~ 65535
//
#define PORT 10
//
// Client: Initialize -> Socket -> Connect -> {Send -> Recv}(loop)
// Server: Initialize -> Socket -> Bind -> Listen -> {Accept -> Recv -> Send}(loop)
//
// Client: Initialize -> GetIPFromHost -> Socket -> Connect -> {Send -> Recv}(loop)
// Server: Initialize -> GetIPFromHost -> Socket -> Bind -> Listen -> {Accept -> Recv -> Send}(loop)
//
class CSocketNode
{
public:
//
// Constructor
//
CSocketNode();
//
// Destructor
//
~CSocketNode();
//
// Initialize -> Socket
// Initialize -> GetIPFromHost
// Return value 0 = No error
// Return value -1 = Error
//
int Initialize(void);
//
// Socket(unsigned short port)
// GetIPFromHost -> Socket
// default port = 10
// port = 0 ~ 65535
// Return value 0 = No error
// Return value -1 = Error
//
int Socket(unsigned short port = PORT);
//
// Socket(const char* buffer, unsigned int length, unsigned short port)
// buffer[] = "192.168.0.1" or "127.0.0.1"
// length = sizeof(buffer[]) / sizeof(char)
// default port = 10
// port = 0 ~ 65535
// Return value 0 = No error
// Return value -1 = Error
//
int Socket(const char* buffer, unsigned int length, unsigned short port = PORT);
//
// Server: Socket -> Bind
// Return value 0 = No error
// Return value -1 = Error
//
int Bind(void);
//
// Server: Bind -> Listen
// Return value 0 = No error
// Return value -1 = Error
//
int Listen(void);
//
// Server: Listen -> Accept(SOCKET& client, CSocketNode& csocketnode)
// client = Return value reference CSocketNode for a client
// Return value 0 = No error
// Return value -1 = Error
//
int Accept(CSocketNode& client);
//
// Client: Socket -> Connect
// Return value 0 = No error
// Return value -1 = Error
//
int Connect(void);
//
// Client: Send(const char* buffer, unsigned int length)
// length = sizeof(buffer[]) / sizeof(char)
// Return value 0 = No error
// Return value -1 = Error
//
int Send(const char* buffer, unsigned int length);
//
// Server: Send(SOCKET client, const char* buffer, unsigned int length)
// length = sizeof(buffer[]) / sizeof(char)
// Return value 1 = Transmitted data
// Return value 0 = No data
// Return value -1 = Error
//
int Send(SOCKET client, const char* buffer, unsigned int length);
//
// Client: Recv(char* buffer, unsigned int length)
// length = sizeof(buffer[]) / sizeof(char)
// Return value 1 = Transmitted data
// Return value 0 = No error
// Return value -1 = Error
//
int Recv(char* buffer, unsigned int length);
//
// Server: Recv(SOCKET client, const char* buffer, unsigned int length)
// length = sizeof(buffer[]) / sizeof(char)
// Return value 1 = Transmitted data
// Return value 0 = No error
// Return value -1 = Error
//
int Recv(SOCKET client, char* buffer, unsigned int length);
//
// getIPFromHost(const char* buffer, unsigned int length)
// buffer[] = "www.google.com"
// length = sizeof(buffer[]) / sizeof(char)
// Return value 1 = Transmitted data
// Return value 0 = No error
// Return value -1 = Error
//
int getIPFromHost(const char* buffer, unsigned int length);
//
// getSocket(void)
// Return value SOCKET m_socket of this class
//
SOCKET getSocket(void);
private:
struct WSAData m_wsadata;
SOCKET m_socket;
struct sockaddr_in m_saddr;
short m_AFamily;
char* m_IP;
unsigned short m_Port;
char* m_Domain;
struct hostent *m_Hostent;
int m_InitializeFail;
ListSocket m_ListSocket;
};
//
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment