-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.cpp
172 lines (133 loc) · 3.39 KB
/
socket.cpp
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
164
165
166
167
168
169
170
171
172
#include "socket.h"
Socket::Socket()
{
m_sock = FAIL; //sock is failed until you initialization
memset ( &m_addr,0,sizeof ( m_addr ) ); //reset the sock's address to zero
}
Socket::~Socket()
{
if ( is_valid() )
::close ( m_sock );
}
bool Socket::create()
{
struct timeval timeout;
timeout.tv_sec = socketTimeOut;
timeout.tv_usec = 0;
m_sock = socket ( AF_INET, SOCK_STREAM, 0 );
if ( ! is_valid() )
return false;
int on = 1;
if (setsockopt (m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&on,sizeof(on)) == FAIL )
return false;
if (setsockopt (m_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(timeout)) == FAIL)
return false;
return true;
}
bool Socket::connect ( const std::string host, const int port )
{
struct hostent *he;
if ((he = gethostbyname(host.c_str())) == NULL)
return false;
if ( ! is_valid() ) return false;
m_addr.sin_family = AF_INET;
m_addr.sin_port = htons ( port );
m_addr.sin_addr.s_addr = *(long*)(he->h_addr);
int status = inet_pton ( AF_INET, host.c_str(), &m_addr.sin_addr );
if ( errno == EAFNOSUPPORT ) return false;
status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) );
if ( status != FAIL )
return true;
else
return false;
}
void Socket::close()
{
shutdown(m_sock,SHUT_RDWR);
if (is_valid())
std::cout << "couldnt close the socket" << m_sock << std::endl;
else
std::cout << "socket is close" << std::endl;
}
bool Socket::send ( const std::string s ) const
{
int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );
if ( status == FAIL )
{
return false;
}
else
{
return true;
}
}
bool Socket::recv() const
{
const int buffsize = 1024;
char buf[buffsize];
std::string p;
p.clear();
bzero(buf, sizeof(buf));
int status;
std::cout << "recieving..." << std::endl;
while ((status = ::recv(m_sock, buf, buffsize, 0)) > 0)
{
p = buf;
std::cout << p << std::endl;
bzero(buf, sizeof(buf));
}
if (status != FAIL)
{
std::cout << "recieved" << std::endl;
return true;
}
return false;
}
bool Socket::bind ( const int port )
{
if ( ! is_valid() ) { return false; }
m_addr.sin_family = AF_INET;
m_addr.sin_addr.s_addr = INADDR_ANY;
m_addr.sin_port = htons ( port );
int bind_return = ::bind ( m_sock, ( struct sockaddr * ) &m_addr, sizeof ( m_addr ) );
if ( bind_return == FAIL ){ return false; }
return true;
}
bool Socket::listen() const
{
if ( ! is_valid() )
{
return false;
}
int listen_return = ::listen ( m_sock, MAXCONNECTIONS );
if ( listen_return == FAIL )
{
return false;
}
return true;
}
bool Socket::accept ( Socket& new_socket ) const
{
int addr_length = sizeof ( m_addr );
new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );
if ( new_socket.m_sock <= 0 )
return false;
else
return true;
}
void Socket::set_non_blocking ( const bool b )
{
int opts;
opts = fcntl ( m_sock,
F_GETFL );
if ( opts < 0 )
{
return;
}
if ( b )
opts = ( opts | O_NONBLOCK );
else
opts = ( opts & ~O_NONBLOCK );
fcntl ( m_sock,
F_SETFL,opts );
}