Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Last active August 29, 2015 14:01
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/04c434e401f06e1bc77a to your computer and use it in GitHub Desktop.
Save johnhmj/04c434e401f06e1bc77a to your computer and use it in GitHub Desktop.
Socket, client socket, HTTP request (class)
#include "W32Tester.h"
#include <iostream>
//
int main(int argc, char* argv[])
{
//
//char domain[] = "localhost";
//
char domain[] = "www.google.com";
//
//char ip[] = "127.0.0.1";
//
unsigned short port = 80;
char request[] = "GET / HTTP/1.0\r\n\r\n";
char buffer[BUF_SIZE];
CSocketClient client;
client.Initialize();
client.GetIPFromHost(domain, sizeof(domain) / sizeof(char));
client.Socket(80);
//
//client.Socket(ip, sizeof(ip) / sizeof(char), port);
//
client.Connect();
client.Send(request, sizeof(request) / sizeof(char));
while (! client.Recv(buffer, sizeof(buffer) / sizeof(char)))
{
std::cout << buffer;
}
std::cout << std::endl;
std::cout << "Press any key to continue ...";
std::cin.get();
return 0;
}
#include "W32Tester.h"
//
CSocketClient::CSocketClient()
{
memset((void*)&this->m_wsadata, 0, sizeof(struct WSAData));
memset((void*)&this->m_saddr, 0, sizeof(struct sockaddr_in));
this->m_s = INVALID_SOCKET;
this->m_AFamily = AF_INET;
this->m_Port = 0;
this->m_hostent = NULL;
this->m_FailConnect = 0;
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);
}
CSocketClient::~CSocketClient()
{
closesocket(this->m_s);
WSACleanup();
if (this->m_IP != NULL)
{
delete [] this->m_IP;
}
if (this->m_Domain != NULL)
{
delete [] this->m_Domain;
}
}
int CSocketClient::Initialize(void)
{
int n = WSAStartup(MAKEWORD(2,2), &(this->m_wsadata));
if (n != 0)
{
this->m_FailConnect = -1;
return this->m_FailConnect;
}
return 0;
}
int CSocketClient::Socket(unsigned short port)
{
if (this->m_FailConnect == -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_s = socket(this->m_saddr.sin_family, SOCK_STREAM, IPPROTO_TCP);
if (this->m_s == INVALID_SOCKET)
{
return -1;
}
return 0;
}
int CSocketClient::Socket(const char* buffer, unsigned int length, unsigned short port)
{
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_s = socket(this->m_saddr.sin_family, SOCK_STREAM, IPPROTO_TCP);
if (this->m_s == INVALID_SOCKET)
{
return -1;
}
return 0;
}
int CSocketClient::Connect()
{
if (this->m_s == INVALID_SOCKET)
{
return -1;
}
int n = connect(this->m_s, (SOCKADDR*)&this->m_saddr, sizeof(struct sockaddr_in));
if (n != 0)
{
this->m_FailConnect = -1;
return this->m_FailConnect;
}
return 0;
}
int CSocketClient::Send(const char* buffer, unsigned int length)
{
if (this->m_s == INVALID_SOCKET)
{
return -1;
}
if (this->m_FailConnect == -1)
{
return -1;
}
int n = send(this->m_s, buffer, length, 0);
if (n == SOCKET_ERROR)
{
return -1;
}
return 0;
}
int CSocketClient::Recv(char* buffer, unsigned int length)
{
if (this->m_s == INVALID_SOCKET)
{
return -1;
}
if (this->m_FailConnect == -1)
{
return -1;
}
memset((void*)buffer, 0, length);
int n = recv(this->m_s, buffer, length, 0);
if (n == SOCKET_ERROR)
{
return -1;
}
//
// The number of bytes received is zero
//
if (n == 0)
{
return -1;
}
return 0;
}
int CSocketClient::GetIPFromHost(const char* buffer, unsigned int length)
{
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)
{
this->m_FailConnect = -1;
return this->m_FailConnect;
}
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;
}
#ifndef _W32TESTER_H_
#define _W32TESTER_H_
#pragma comment(lib, "Ws2_32.lib")
#include <WinSock2.h>
#define BUF_SIZE 4096
//
// Initialize -> Socket -> Connect -> Send -> Recv
// Initialize -> GetIPFromHost -> Socket -> Connect -> Send -> Recv
//
class CSocketClient
{
public:
CSocketClient();
~CSocketClient();
//
// Initialize -> Socket
// Initialize -> GetIPFromHost
// Return value 0 = No error
// Return value -1 = Error
//
int Initialize(void);
//
// Socket(unsigned short port)
// GetIPFromHost -> Socket
// port = 0 ~ 65535
// Return value 0 = No error
// Return value -1 = Error
//
int Socket(unsigned short 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)
// port = 0 ~ 65535
// Return value 0 = No error
// Return value -1 = Error
//
int Socket(const char* buffer, unsigned int length, unsigned short port);
//
// Socket -> Connect
// Return value 0 = No error
// Return value -1 = Error
//
int Connect(void);
//
// 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);
//
// Recv(char* buffer, unsigned int length)
// length = sizeof(buffer[]) / sizeof(char)
// Return value 0 = No error
// Return value -1 = Error
//
int Recv(char* buffer, unsigned int length);
//
// GetIPFromHost(const char* buffer, unsigned int length)
// buffer[] = "www.google.com"
// length = sizeof(buffer[]) / sizeof(char)
// Return value 0 = No error
// Return value -1 = Error
//
int GetIPFromHost(const char* buffer, unsigned int length);
private:
struct WSAData m_wsadata;
SOCKET m_s;
struct sockaddr_in m_saddr;
short m_AFamily;
char* m_IP;
unsigned short m_Port;
char* m_Domain;
struct hostent *m_hostent;
int m_FailConnect;
};
//
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment