MyLib
This commit is contained in:
55
Test/TcpTest.cpp
Normal file
55
Test/TcpTest.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user