-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_server_v3.cpp
253 lines (189 loc) · 4.93 KB
/
chat_server_v3.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
/*
* NCYU 109 Network Programming chat server v3
* Created by linwebs on 2021/4/12.
*/
#include <iostream>
#include <cstring>
#include <winsock.h>
#define MAXLINE 1024
using namespace std;
struct translate_word {
string english;
string chinese;
};
struct bad_word {
string origin;
string cover;
};
const int translate_word_num = 4;
const int bad_word_num = 4;
translate_word translate_word_list[translate_word_num] = {
{"hello", "你好"},
{"hi", "嗨"},
{"how are you", "你好嗎"},
{"I'm find", "我很好"}
};
bad_word bad_word_list[bad_word_num] = {
{"bullshit", "bullsxxt"},
{"bitch", "bixxh"},
{"fuck", "fxxk"},
{"suck", "sxxk"}
};
string cover_bad(const string &input);
string translate(const string &input);
string get_user(const string &input);
string get_room(const string &input);
string get_msg(const string &input);
int main() {
// client count
int cli_num = 3;
// chat room name
string now_room;
string now_user;
string now_msg;
string tmp;
cout << "歡迎使用聊天室伺服器" << endl;
cout << "請輸入使用人數: ";
cin >> cli_num;
SOCKET serv_sd;
SOCKET clis_sd;
SOCKET cli_sd[cli_num];
int clis_len;
int cli_len[cli_num];
char str_recv[MAXLINE];
char str_send[MAXLINE];
sockaddr_in serv{};
sockaddr_in clis{};
sockaddr_in cli[cli_num];
WSADATA wsadata;
// server's ip address
const char server_ip[16] = "127.0.0.1";
// server's port number
u_short server_port = 5678;
// receive bytes
int rec_len;
// send bytes
int send_len;
// bind status
int bind_status;
// Call WSAStartup() to Register "WinSock DLL"
WSAStartup(0x101, (LPWSADATA) &wsadata);
// Open a TCP socket
serv_sd = socket(AF_INET, SOCK_STREAM, 0);
// Prepare for connect.
// Include sockaddr_ing struct (serv)
serv.sin_family = AF_INET;
// server's ip address
serv.sin_addr.s_addr = inet_addr(server_ip);
// server's port number
// htons: host to network
serv.sin_port = htons(server_port);
// bind
bind_status = bind(serv_sd, (LPSOCKADDR) &serv, sizeof(serv));
if (bind_status == SOCKET_ERROR) {
cout << "bind function failed with error: " << WSAGetLastError() << endl;
closesocket(serv_sd);
WSACleanup();
return 1;
}
// call listen() function to let socket enter listen mode
listen(serv_sd, 5);
for (int i = 0; i < cli_num; i++) {
cli_len[i] = sizeof(cli[i]);
// accept connect
cout << "[訊息] 等待 client " << i + 1 << " 連線" << endl;
cli_sd[i] = accept(serv_sd, (LPSOCKADDR) &cli[i], &cli_len[i]);
cout << "[訊息] client " << i + 1 << " 已連線" << endl;
}
clis_len = sizeof(clis);
while (true) {
clis_sd = accept(serv_sd, (LPSOCKADDR) &clis, &clis_len);
if (clis_sd == SOCKET_ERROR) {
cout << "[錯誤] 無法接受訊息" << endl;
cout << "get hp error, code:" << WSAGetLastError() << endl;
break;
}
//cout << "[訊息] client 已連線" << endl;
rec_len = recv(clis_sd, str_recv, MAXLINE, 0);
if (rec_len <= 0) {
cout << "[錯誤] 接收訊息錯誤" << endl;
break;
}
closesocket(clis_sd);
//cout << "[接收] " << str_recv << endl;
now_room = get_room(str_recv);
now_user = get_user(str_recv);
now_msg = get_msg(str_recv);
cout << "[room]: " << now_room << "[user]: " << now_user << "[msg] : " << now_msg << endl;
// cover bad text
tmp = cover_bad(string(now_msg));
if (!tmp.empty()) {
now_msg = tmp;
}
// translate
tmp = translate(string(now_msg));
if (!tmp.empty()) {
now_msg = tmp;
}
tmp = now_user;
tmp.append(",");
tmp.append(now_room);
tmp.append(",");
tmp.append(now_msg);
strcpy(str_send, tmp.c_str());
for (int i = 0; i < cli_num; i++) {
// send msg from server to client 1
send_len = send(cli_sd[i], str_send, int(strlen(str_send) + 1), 0);
if (send_len == SOCKET_ERROR) {
cout << "[錯誤] 傳送訊息到" << i << "失敗" << endl;
}
}
if (now_msg == "bye") {
break;
}
}
// close TCP socket
closesocket(serv_sd);
closesocket(clis_sd);
for (int i = 0; i < cli_num; i++) {
closesocket(cli_sd[i]);
}
// finish "WinSock DLL"
WSACleanup();
//system("pause");
return 0;
}
string cover_bad(const string &input) {
for (auto &i : bad_word_list) {
if (input == i.origin) {
return i.cover;
}
}
return "";
}
string translate(const string &input) {
for (auto &i : translate_word_list) {
if (input == i.english) {
return "(英文 -> 中文) 原文: " + input + " 譯文: " + i.chinese;
} else if (input == i.chinese) {
return "(中文 -> 英文) 原文: " + input + " 譯文: " + i.english;
}
}
return "";
}
string get_user(const string &input) {
return input.substr(0, input.find(','));
}
string get_room(const string &input) {
// no room
string no = input.substr(input.find(',') + 1);
return no.substr(0, no.find(','));
}
string get_msg(const string &input) {
string no;
// no room
no = input.substr(input.find(',') + 1);
// no user
no = no.substr(no.find(',') + 1);
return no.substr(0, no.find(','));
}