-
Notifications
You must be signed in to change notification settings - Fork 9
/
csocket.h
60 lines (46 loc) · 1.24 KB
/
csocket.h
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
#ifndef CSOCKET_H
#define CSOCKET_H
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#else
#include <netinet/in.h>
#include <asm/errno.h>
#include <unistd.h>
#include <cstring>
#endif
#include <stdio.h>
#include <string>
#define SUCCESS 0
#define FAILURE 1
#define INFINITY -1
class csocket
{
public:
enum SocketState
{
CLOSED,
CONNECTED,
ERRORED,
};
csocket();
~csocket();
static int resolveHost( const std::string& szRemoteHostName, struct hostent** pHostEnt );
int connect( const char* remoteHost, unsigned int remotePort );
int canRead( bool* readyToRead, float waitTime = INFINITY );
virtual int read( char* pDataBuffer, unsigned int numBytesToRead, bool bReadAll );
virtual int write( const char* pDataBuffer, unsigned int numBytesToWrite );
SocketState getState( void ) const;
private:
#ifdef _WIN32
SOCKET m_socket;
#else
int m_socket;
#endif
struct sockaddr_in m_localSocketAddr;
struct sockaddr_in m_remoteSocketAddr;
SocketState m_socketState;
std::string m_strRemoteHost;
unsigned int m_remotePort;
};
#endif // CSOCKET_H