73 lines
1.9 KiB
C++
73 lines
1.9 KiB
C++
|
|
#include "Test.h"
|
||
|
|
|
||
|
|
Test::Test() {
|
||
|
|
socket.Init(CTL::IPV6,CTL::UDP);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Test::test() {
|
||
|
|
auto list = CTL::Socket::GetLocalIP(CTL::IPV6);
|
||
|
|
bool F = socket.Bind(list[0].c_str(),8080);
|
||
|
|
if (F) {
|
||
|
|
CTL::Thread th(&Test::udp_socket,this);
|
||
|
|
th.Start();
|
||
|
|
}
|
||
|
|
// bool F = socket.Connect(list[0].c_str(), 8080);
|
||
|
|
// if (F) {
|
||
|
|
// auto clientinfo = socket.GetLocalHost();
|
||
|
|
// CC::Println(clientinfo.IPAddress.c_str());
|
||
|
|
// CC::Println(clientinfo.Port);
|
||
|
|
// CTL::Thread th(&Test::client_socket,this,socket);
|
||
|
|
// th.Start();
|
||
|
|
// }
|
||
|
|
// bool F = socket.Bind(list[0].c_str(),8080);
|
||
|
|
// if (F) {
|
||
|
|
// bool F = socket.Listen();
|
||
|
|
// if (F) {
|
||
|
|
// thread.SetThread(&Test::test_socket,this);
|
||
|
|
// thread.Start();
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
}
|
||
|
|
|
||
|
|
void Test::test_socket() {
|
||
|
|
while (thread.Sign()) {
|
||
|
|
auto sock = socket.Accept();
|
||
|
|
CTL::Thread th(&Test::client_socket,this,sock);
|
||
|
|
th.Start();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Test::client_socket(CTL::Socket& socket) {
|
||
|
|
while (true) {
|
||
|
|
char buffer[1024] = {};
|
||
|
|
auto length = socket.RecvData(buffer,sizeof(buffer));
|
||
|
|
if (length > 0) {
|
||
|
|
// auto clientinfo = socket.GetClientHost();
|
||
|
|
// CC::Println(clientinfo.IPAddress.c_str());
|
||
|
|
// CC::Println(clientinfo.Port);
|
||
|
|
CC::Println(buffer);
|
||
|
|
socket.Send(buffer);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Test::udp_socket() {
|
||
|
|
while (true) {
|
||
|
|
char buffer[1024] = {};
|
||
|
|
CCHostInfo client_info{};
|
||
|
|
auto length = socket.UDPRecvData(buffer,sizeof(buffer),&client_info);
|
||
|
|
if (length > 0) {
|
||
|
|
CC::Println(client_info.IPAddress.c_str());
|
||
|
|
CC::Println(client_info.Port);
|
||
|
|
CC::Println(buffer);
|
||
|
|
socket.UDPSend(buffer,client_info.IPAddress.c_str(),client_info.Port);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|