Aim
An example to show how to do TCP communication. Support Linux only now.
Source
#include <iostream>
#include <string.h>
#include <thread>
#include <chrono>
#include "TCPCast.h"
#include "Logger.h"
std::string serverIP = "192.168.1.208";
short serverPort = 7788;
void ServerWorker()
{
std::vector<char> sendData;
std::vector<char> recvData;
TCPCast tcpServer;
if (tcpServer.InitComponent(serverIP, serverPort, false) == TCPCast::TCPStatus::SUCCESS)
{
int count = 0;
sendData.resize(4);
recvData.resize(4);
int clientHandle = tcpServer.Accept(); // waiting for client
while (true)
{
memcpy(&sendData[0], &count, sizeof(int));
TCPCast::TCPStatus retStatus = tcpServer.ServerSend(clientHandle, &sendData[0], sizeof(int));
if (retStatus == TCPCast::TCPStatus::SUCCESS)
{
LOGMSG_MSG_C("Send success! count: %d\n", count);
}
else
{
LOGMSG_ERR_C("Send fail!\n");
}
count++;
int byteRecv = 0;
retStatus = tcpServer.ServerRecv(clientHandle, recvData, byteRecv);
if (retStatus == TCPCast::TCPStatus::SUCCESS && byteRecv > 0)
{
int countRecv = -1;
memcpy(&countRecv, &recvData[0], sizeof(int));
LOGMSG_MSG_C("Received count: %d byteRecv: %d\n", countRecv, byteRecv);
}
}
}
else
{
LOGMSG_ERR_C("Cannot InitComponent\n");
}
}
void ClientWorker()
{
std::vector<char> sendData;
std::vector<char> recvData;
TCPCast tcpClient;
if (tcpClient.InitComponent(serverIP, 0, true) == TCPCast::TCPStatus::SUCCESS)
{
sendData.resize(4);
recvData.resize(4);
if (tcpClient.Connect(serverIP, serverPort) == TCPCast::TCPStatus::SUCCESS)
{
int count = 2000;
while (true)
{
int byteRecv = 0;
TCPCast::TCPStatus retStatus = tcpClient.ClientRecv(recvData, byteRecv);
if (retStatus == TCPCast::TCPStatus::SUCCESS && byteRecv > 0)
{
int countRecv;
memcpy(&countRecv, &recvData[0], sizeof(int));
LOGMSG_MSG_C("Received count: %d\n", countRecv);
}
memcpy(&sendData[0], &count, sizeof(int));
retStatus = tcpClient.ClientSend(&sendData[0], sizeof(int));
if (retStatus == TCPCast::TCPStatus::SUCCESS)
{
LOGMSG_MSG_C("Send success! count: %d\n", count);
}
else
{
LOGMSG_ERR_C("Send fail!\n");
}
count++;
usleep(1000000); // 1 sec
}
}
}
}
int main(int argc, char *argv[])
{
std::thread serverTH(ServerWorker);
serverTH.detach();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::thread clientTH(ClientWorker);
clientTH.detach();
std::this_thread::sleep_for(std::chrono::seconds(10));
return 0;
}
Output
$ ./src/TCPServerClient
20200502_104308_399 [MSG] TCPCast:: Accept: 121, 20204,Received client 192.168.1.208:36946
20200502_104308_400 [MSG] ServerWorker: 28, 20204,Send success! count: 0
20200502_104308_400 [MSG] ClientWorker: 71, 20206,Received count: 0
20200502_104308_400 [MSG] ClientWorker: 78, 20206,Send success! count: 2000
20200502_104308_400 [MSG] ServerWorker: 42, 20204,Received count: 2000 byteRecv: 4
20200502_104308_400 [MSG] ServerWorker: 28, 20204,Send success! count: 1
20200502_104309_400 [MSG] ClientWorker: 71, 20206,Received count: 1
20200502_104309_400 [MSG] ClientWorker: 78, 20206,Send success! count: 2001
20200502_104309_400 [MSG] ServerWorker: 42, 20204,Received count: 2001 byteRecv: 4
20200502_104309_400 [MSG] ServerWorker: 28, 20204,Send success! count: 2
沒有留言:
發佈留言