-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmySocket.cpp
267 lines (225 loc) · 4.71 KB
/
mySocket.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*************************************************************************
> File Name: mySocket.cpp
> Author: tww
> Mail: [email protected]
> Created Time: Tue 22 Jul 2014 06:44:42 AM PDT
************************************************************************/
#include <iostream>
#include "mySocket.h"
using namespace std;
BaseSock::BaseSock()
{
m_bUDP = false;
m_nSockfd = 1;
m_nPort = 0;
m_bConnected = false;
for(int i = 0; i < BUFFER_SIZE; i++)
{
m_cHost[i] = 0;
}
}
BaseSock::~BaseSock()
{
cout << "~BaseSock() is called" << endl;
Close();
}
bool BaseSock::Create(bool bUDP)
{
m_bUDP = bUDP;
if(!m_bUDP)
{
m_nSockfd = socket(AF_INET, SOCK_STREAM, 0);
}
else
{
m_nSockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(m_nSockfd != -1)
{
m_bConnected = true;
}
}
int on =1;
setsockopt( m_nSockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
return (m_nSockfd != -1);
}
bool BaseSock::Bind(unsigned short nPort)
{
if(m_nSockfd == -1)
{
return false;
}
struct sockaddr_in sin;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(nPort);
sin.sin_addr.s_addr = htons(INADDR_ANY);
if(bind(m_nSockfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
cout << "bind to port" << nPort << "failure" << endl;
return false;
}
return true;
}
void BaseSock::Close()
{
close(m_nSockfd);
}
bool BaseSock::Send(const char *buf, long buflen)
{
if(m_nSockfd == -1)
{
return false;
}
send(m_nSockfd, buf, buflen, 0);
return true;
}
long BaseSock::Recv(char *buf, long buflen)
{
int length = 0;
if(m_nSockfd == -1)
{
return false;
}
length = recv(m_nSockfd, buf, buflen, 0);
if(length < 0)
{
cout << "recieve error"<<length << endl;
return false;
}
// cout << "recieve from IP: " << m_cHost << endl;
// cout << "Data : " << buf << endl;
return length;
}
bool BaseSock::Sendto(const char *buf, int len, const struct sockaddr_in *toaddr)
{
if(m_nSockfd == -1 || len <= 0)
{
return false;
}
if(sendto(m_nSockfd, buf, len, 0, (struct sockaddr *)toaddr, sizeof(struct sockaddr_in)) != len)
{
cout << "sendto fail" << endl;
Close();
}
return true;
}
long BaseSock::RecvFrom(char *buf, int len, struct sockaddr_in *fromaddr)
{
int length = 0;
socklen_t addrLen = sizeof(struct sockaddr_in);
if((length = recvfrom(m_nSockfd, buf, len, 0, (struct sockaddr *)fromaddr,&addrLen)) < 0)
{
cout << "recvfrom error" << endl;
Close();
return -1;
}
else
{
cout << "FROM IP : " << inet_ntoa(fromaddr->sin_addr) << endl;
cout << "DATA :" << buf << endl << endl;
}
return length;
}
/*
* 在此处完成构造函数定义
*/
ServerSock::ServerSock(): BaseSock()
{
}
/*
* 在此处完成析构函数定义
*/
ServerSock::~ServerSock()
{
Close();
}
/*
* 完成RecvFrom的重定义
*/
long ServerSock::RecvFrom(char *buf, int len, struct sockaddr_in *fromaddr)
{
int length = 0;
return length;
}
/*
* 完成Sendto的重定义
*/
bool ServerSock::Sendto(const char *buf, int len, const struct sockaddr_in *toaddr)
{
return true;
}
bool ServerSock::Listen()
{
/* 完成监听函数
* 可以man listen查看具体的参数说明
* 代码如:if(listen() < 0)
*/
if(listen(m_nSockfd ,10)<0)
{
cout << "call listen failure" << endl;
return false;
}
return true;
}
bool ServerSock::Accept(ClientSock &client)
{
if(m_nSockfd == -1)
{
return false;
}
struct sockaddr_in cliaddr;
/* 此处完成接受连接请求
* 获取客户机的Socket文件描述符,发送数据
* 具体可参考man accept
* 如:client.m_nSockfd = accept();
*/
socklen_t len=sizeof(client);
client.m_nSockfd=accept(m_nSockfd,(struct sockaddr *)&cliaddr,&len);
client.m_bConnected = true;
strcpy(client.m_cHost, inet_ntoa(cliaddr.sin_addr));
client.m_nPort = ntohs(cliaddr.sin_port);
return (client.m_nSockfd != -1);
}
ClientSock::ClientSock() : BaseSock()
{
}
ClientSock::~ClientSock()
{
Close();
}
bool ClientSock::Connect(const char *host, unsigned short port)
{
if(m_nSockfd == -1)
{
return false;
}
strcpy(m_cHost,host);
m_nPort = port;
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_aton(host, &servaddr.sin_addr);
/* 服务器的端口地址,注意网络字节序与本机字节序问题
* 可参考函数:htons()
*/
servaddr.sin_port =htons(port);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_addr.s_addr = inet_addr(host);
/*
* 连接请求
* 可根据man connect填入适当参数
* 如: if(connect())
*/
if(connect(m_nSockfd,(struct sockaddr *)&servaddr,sizeof(sockaddr))==0)
{
cout << "connect to " << host << "success"<<endl;
}
else
{
cout << "can't connect to " << host << endl;
Close();
return false;
}
m_bConnected = true;
return true;
}