-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.h
163 lines (120 loc) · 3.16 KB
/
Socket.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// This file is a derivative work of Rob Tougher's
// c++ sockets implementation. Please see socket.LICENSE
// and AUTHORS for details.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#ifndef SOCKET
#define SOCKET
// Don't forget to make sure MAXCONNECTIONS is ALWAYS high enough...
const int MAXHOSTNAME = 200;
const int MAXCONNECTIONS = 1000;
const int MAXRECV = 4096;
class Socket
{
public:
Socket();
virtual ~Socket();
// Server initialization
bool create();
bool bind ( const int port );
bool listen() const;
bool accept ( Socket& ) const;
// Client initialization
bool connect ( const std::string host, const int port );
// Data Transimission
bool send ( const std::string ) const;
int recv ( std::string& ) const;
void set_non_blocking ( const bool );
bool is_valid() const { return m_sock != -1; }
int timeout;
private:
int m_sock;
sockaddr_in m_addr;
};
#include "SocketFunctions.h"
class ClientSocket : private Socket
{
public:
ClientSocket ( std::string host, int port ) {
Socket::create();
Socket::connect(host,port);
timeout = 0;
}
~ClientSocket(){};
void setRecvTimeout(int milliseconds) { timeout = milliseconds; }
const ClientSocket& operator << ( const std::string& s ) const
{
Socket::send ( s ) ;
return *this;
}
const ClientSocket& operator >> ( std::string& s ) const
{
Socket::recv ( s );
return *this;
}
};
class ServerSocket : private Socket
{
public:
ServerSocket ( int port );
ServerSocket (){};
~ServerSocket();
void setRecvTimeout(int milliseconds) { timeout = milliseconds; }
const ServerSocket& operator << ( const std::string& ) const;
const ServerSocket& operator >> ( std::string& ) const;
void accept ( ServerSocket& );
};
ServerSocket::ServerSocket ( int port )
{
if ( ! Socket::create() )
{
// throw SocketException ( "Could not create server socket." );
std::cerr << "Could not create server socket.";
}
if ( ! Socket::bind ( port ) )
{
// throw SocketException ( "Could not bind to port." );
std::cerr << "Could not bind to port.";
}
if ( ! Socket::listen() )
{
// throw SocketException ( "Could not listen to socket." );
std::cerr << "Could not listen to socket.";
}
}
ServerSocket::~ServerSocket()
{
}
const ServerSocket& ServerSocket::operator << ( const std::string& s ) const
{
if ( ! Socket::send ( s ) )
{
// throw SocketException ( "Could not write to socket." );
std::cerr << "Could not write to socket.";
}
return *this;
}
const ServerSocket& ServerSocket::operator >> ( std::string& s ) const
{
if ( ! Socket::recv ( s ) )
{
// throw SocketException ( "Could not read from socket." );
// std::cerr << "Could not read from socket.";
}
return *this;
}
void ServerSocket::accept ( ServerSocket& sock )
{
if ( ! Socket::accept ( sock ) )
{
// throw SocketException ( "Could not accept socket." );
std::cerr << "Could not accept socket.";
}
}
// void ServerSocket::setRecvTimeout(int milliseconds) { timeout = milliseconds; }
#endif