-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSocket.hpp
63 lines (54 loc) · 1.22 KB
/
Socket.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <string>
#include <iostream> //stdout
#include <errno.h>
#ifdef __linux__
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>
#include <netdb.h>
#include <cstring>
#include <unistd.h>
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif // !INVALID_SOCKET
typedef unsigned int SOCKET;
#elif _WIN32
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
#else
#error Platform not supported
#endif
class SocketClient
{
public:
SocketClient(int domain, int type, int protocol, std::string IPaddress, std::string hostname, int port, int timeout = -1);
void Connect();
size_t Send(std::string &data);
size_t Recv(std::string &data, bool read_to_close = false);
size_t _devNullRecv();
void setTimeout(int second);
~SocketClient();
private:
#ifdef _WIN32
SOCKET m_socSocket = INVALID_SOCKET;
#elif __linux__
int m_socSocket;
#endif
char m_cBuffer[10240];
struct hostent *host;
bool bGetAddrInfoStatus;
int m_iFamily_domain;
int m_iSocket_type;
int m_iProtocol;
std::string m_sIPaddress;
std::string m_sHostname;
int m_iPort;
int m_iTimeout;
fd_set m_fdsetSet;
sockaddr_in m_SockAddr;
};