55 lines
1.2 KiB
C++
55 lines
1.2 KiB
C++
|
#include "Test.h"
|
||
|
|
||
|
#include "TCP.h"
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
void Tcp_Test() {
|
||
|
// AsyncTCPClient client;
|
||
|
// client.connect("127.0.0.1", 9999);
|
||
|
// client.start_async();
|
||
|
// while (client.is_connected()) {
|
||
|
// std::vector<uint8_t> str;
|
||
|
// if (client.try_pop(str)) {
|
||
|
// cout << string(str.begin(), str.end()) << endl;
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
try {
|
||
|
TCPClient client;
|
||
|
client.connect("192.168.80.1", 9999);
|
||
|
while (client.is_connected()) {
|
||
|
vector<uint8_t> buffer(1024);
|
||
|
auto size = client.receive(buffer.data(), buffer.size());
|
||
|
string info;
|
||
|
if (size > 0) {
|
||
|
info = std::string(buffer.begin(), buffer.begin() + static_cast<int>(size));
|
||
|
cout << info << endl;
|
||
|
}
|
||
|
if (info.find("stop") != string::npos) {
|
||
|
client.close_socket();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (std::exception &e) {
|
||
|
cerr << e.what() << endl;
|
||
|
}
|
||
|
|
||
|
|
||
|
TCPServer tcpServer;
|
||
|
tcpServer.listen(9998);
|
||
|
auto client = tcpServer.accept();
|
||
|
while (client->is_connected()) {
|
||
|
client->send("hello");
|
||
|
vector<uint8_t> buffer(1024);
|
||
|
auto size = client->receive(buffer.data(), buffer.size());
|
||
|
string info;
|
||
|
if (size > 0) {
|
||
|
info = std::string(buffer.begin(), buffer.begin() + static_cast<int>(size));
|
||
|
cout << info << endl;
|
||
|
}
|
||
|
if (info.find("stop") != string::npos) {
|
||
|
client->close_socket();
|
||
|
}
|
||
|
}
|
||
|
}
|