From 7248be8217488cfdced129bbeac57e2a00be9087 Mon Sep 17 00:00:00 2001 From: LikhenkoVG Date: Wed, 2 Apr 2025 22:00:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=20c++?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Подключение, отправка и получение данных --- TestClientConnection/TestClientConnection.cpp | 47 ++++++++++++++++--- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/TestClientConnection/TestClientConnection.cpp b/TestClientConnection/TestClientConnection.cpp index 2401cf6..b22cd32 100644 --- a/TestClientConnection/TestClientConnection.cpp +++ b/TestClientConnection/TestClientConnection.cpp @@ -1,9 +1,22 @@ #include #include #include +#include #pragma comment(lib, "Ws2_32.lib") +struct DataIn +{ + float AccX, AccY, AccZ; + float GyrX, GyrY, GyrZ; + float PosX, PosY, LaserRange; +} dataIn; + +struct DataOut +{ + float MotorUL, MotorUR, MotorDL, MotorDR; +} dataOut; + int main() { WSAData wsaData; @@ -16,16 +29,36 @@ int main() } SOCKADDR_IN addr; - addr.sin_family = AF_INET; - addr.sin_port = htons(1001); + int sizeofaddr = sizeof(addr); - // Use inet_pton instead of inet_addr - if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) <= 0) + inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); + addr.sin_port = htons(1001); + addr.sin_family = AF_INET; + + SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL); + if (connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) { - std::cout << "Error converting IP address\n"; - exit(1); + std::cout << "Error: failed connect to server \n"; + return 1; } + dataOut.MotorUL = 10; + dataOut.MotorUR = 10; + dataOut.MotorDL = 10; + dataOut.MotorDR = 10; + + while (1) + { + send(Connection, (char*)&(dataOut), sizeof(dataOut), 0); + recv(Connection, (char*)&(dataIn), sizeof(dataIn), 0); + //std::cout << dataIn.GyrX << " " << dataIn.GyrY << " " << dataIn.GyrZ << std::endl; + //std::cout << dataIn.PosX << " " << dataIn.PosY << " " << dataIn.LaserRange << std::endl; + //std::cout << dataIn.AccX << " " << dataIn.AccY << " " << dataIn.AccZ << std::endl; + std::cout << dataIn.LaserRange << std::endl; + std::cout << "-----------------------------------------------------------" << std::endl; + //std::this_thread::sleep_for(std::chrono::seconds(1)); + } + + system("pause"); - std::cout << "Hello World!\n"; }