-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskSocket.hpp
180 lines (171 loc) · 4.01 KB
/
TaskSocket.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
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
#ifndef _TASKSOCKET_H_
#define _TASKSOCKET_H_
#include <iostream>
#include <arpa/inet.h>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
using namespace std;
class TaskSocket
{
public:
TaskSocket()
{
tfd = socket(AF_INET, SOCK_STREAM, 0);
};
TaskSocket(int fd) : tfd(fd) {}
TaskSocket(string msg)
{
if (msg == "receive")
{
tfd = socket(AF_INET, SOCK_STREAM, 0);
recv_fd = socket(AF_INET, SOCK_STREAM, 0);
}
}
~TaskSocket() {};
int Send(string msg)
{
int msg_size = msg.size();
int biglen = htonl(msg_size);
char *data = new char[msg_size + 4];
// 数据头
memcpy(data, &biglen, 4);
// 数据体
memcpy(data + 4, msg.data(), msg_size);
// 发送数据
const char *buf = data;
int cnt = msg_size + 4;
while (cnt > 0)
{
int len = send(tfd, buf, cnt, 0);
if (len == -1)
{
close(tfd);
perror("write error");
delete[] data;
exit(0);
}
else if (len == 0)
{
continue;
}
buf += len;
cnt -= len;
}
delete[] data;
return msg_size + 4;
}
string Receive_client()
{
// 数据头
int len = 0;
char *buf = new char[4];
int cnt = 4;
char *pt = (char *)&len;
while (cnt > 0)
{
int ret = recv(tfd, pt, cnt, 0);
if (ret == -1)
{
close(tfd);
perror("read error");
exit(0);
}
else if (ret == 0)
{
cout << "连接已结束" << endl;
close(tfd);
delete[] buf;
return "close";
}
pt += ret;
cnt -= ret;
}
len = ntohl(len);
delete[] buf;
buf = new char[len + 1];
cnt = len;
pt = buf;
while (cnt > 0)
{
int ret = recv(tfd, pt, cnt, 0);
if (ret == -1)
{
close(tfd);
perror("read error");
delete[] buf;
exit(0);
}
else if (ret == 0)
{
cout << "连接已结束" << endl;
close(tfd);
delete[] buf;
return "close";
}
pt += ret;
cnt -= ret;
}
buf[len] = '\0';
string msg(buf);
delete[] buf;
return msg;
}
int Receive_server(int cfd, char **msg)
{
int len = 0;
Read(cfd, (char *)&len, 4);
len = ntohl(len);
// printf("数据大小为%d\n",len);
char *buf = (char *)malloc(len + 1);
int ret = Read(cfd, buf, len);
if (ret != len)
{
printf("数据接收失败\n");
}
else if (ret == 0)
{
cout << to_string(cfd) << "断开链接" << endl;
close(cfd);
}
buf[len] = '\0';
*msg = buf;
return ret;
}
ssize_t Read(int fd, void *aptr, size_t n)
{
size_t leftn;
ssize_t readn;
char *ptr;
ptr = (char *)aptr;
leftn = n;
while (leftn > 0)
{
if ((readn = read(fd, ptr, leftn)) < 0)
{
if (errno == EINTR || EWOULDBLOCK)
{
readn = 0;
}
else
{
return -1;
}
}
else if (readn == 0)
{
break;
}
leftn -= readn;
ptr += readn;
}
return n - leftn;
}
int getfd() { return tfd; }
int get_recvfd() { return recv_fd; }
private:
int tfd = -1;
int recv_fd = -1;
};
#endif