-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
205 lines (166 loc) · 4.62 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <Ws2tcpip.h>
#include <string>
#include <thread>
#include <queue>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)
#include "gpt_ans_get.h"
using namespace std;
const int MAX_HANDLE_NUM = 100;
queue<int> thread_num;
char recbuf[2048];//接受数据缓冲区
const int BUFF_SIZE = 2048;
const char log_ui_filename[] = "./log_ui.html";
const char chat_ui_filename[] = "./chat_ui.html";
string html_head = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n";
string html_end = "\r\n\0";
string log_ui_s;
string chat_ui_s;
const string GET = "GET";
const string POST = "POST";
void thread_s(string _in,string* _out)
{
*_out = gpt3_5_ans_get(_in,1);
return;
}
void handle_request(int client_fd,int _thread_num)
{
int numbytes;
char buff[2048];
if ((numbytes = recv(client_fd, buff, BUFF_SIZE, 0)) == -1)
{
cerr << "Failed to receive data: " << WSAGetLastError() << endl;
closesocket(client_fd);
}
buff[numbytes] = 0;
cout << buff << endl;
string buff_s = buff;
string http_type = buff_s.substr(0, buff_s.find(' '));
if (http_type == GET)
{
string post_sent = html_head + chat_ui_s + html_end;
cout << send(client_fd, post_sent.c_str(), post_sent.size(), 0) << " Bytes has sent" << endl;
return;
}
else if (http_type == POST)
{
cout << "POST received" << endl;
string post_rec = buff_s.substr(buff_s.find('{'));
string post_sent = "HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\n";
string result_s;
result_s = gpt3_5_ans_get(post_rec, _thread_num);
Sleep(2500);
post_sent = post_sent + result_s + html_end;
cout << "post_sent:" << post_sent << endl;
int bytesRead;
int bytesSent = send(client_fd, post_sent.c_str(), post_sent.length(), 0);
cout << bytesSent << endl;
if (bytesSent == SOCKET_ERROR)
{
std::cerr << "Failed to send data: " << WSAGetLastError() << std::endl;
}
else {
cout << "post sent successfully" << endl;
}
return;
}
else
{
cerr << "http type error!" << endl;
return;
}
return;
}
void thread_handle_catch(int _client_fd, int _tnum)
{
handle_request(_client_fd, _tnum);
closesocket(_client_fd);
thread_num.push(_tnum);
return;
}
void thread_handle(int _client_fd)
{
int tnum = thread_num.front();
cout << "thread start with: " << tnum << endl;
thread_num.pop();
thread t(thread_handle_catch, _client_fd,tnum);
t.detach();
return;
}
void file_input(string& _in, istream& ss)
{
string s;
while (!ss.eof())
{
s.clear();
getline(ss, s);
_in = _in + s + '\n';
}
return;
}
int main() {
//初始化线程管理队列
for (int i = 1; i <= MAX_HANDLE_NUM; i++)
{
thread_num.push(i);
}
//读取ui文件
fstream fs;
fs.open(log_ui_filename);
if (!fs.is_open())
{
cerr << "failed to open source file" << endl;
}
file_input(log_ui_s, fs);
fs.close();
fs.open(chat_ui_filename);
if (!fs.is_open())
{
cerr << "failed to open source file" << endl;
}
file_input(chat_ui_s, fs);
fs.close();
WSADATA wsadata;//用于初始化winsock库
int iResult = WSAStartup(MAKEWORD(2, 2), &wsadata);
SOCKADDR_IN svr_addr, cli_addr;//服务器地址与客户端地址
int sin_len = sizeof(cli_addr);
//套接字与端口设置
int my_socket = socket(AF_INET, SOCK_STREAM, 0);
int my_port = 1181;
//服务器地址设置
svr_addr.sin_family = AF_INET;
svr_addr.sin_addr.s_addr = INADDR_ANY;
svr_addr.sin_port = htons(my_port);
//绑定套接字到服务器地址
if (bind(my_socket, (SOCKADDR*)&svr_addr, sizeof(svr_addr)) == -1)
{
closesocket(my_socket);
cerr << "bind error" << endl;
}
listen(my_socket, MAX_HANDLE_NUM);//对最多MAX_HANDLE_NUM个客户端进行监听
//进入循环接受用户accept
while (1)
{
int len = sizeof(SOCKADDR);
int _client_fd = accept(my_socket, (SOCKADDR*)&cli_addr, &sin_len);
cout << "\n\naccept received in " << inet_ntoa(cli_addr.sin_addr) << endl;//显示接受到的用户ip地址
if (_client_fd == -1)
{
cerr << "failed to connect accept" << endl;
continue;
}
thread_handle(_client_fd);
}
closesocket(my_socket);
WSACleanup();
return 0;
}