Pages - Menu

標籤

AWS (1) bash (1) Boost (2) C (2) CMake (2) Concurrency_Programming (3) CPP (37) Database (2) DNS (1) Docker (4) Docker-Compose (1) ELK (1) emacs (4) gcp (1) gdrive (1) git (1) gitbash (2) gitlab (1) kvm (4) Linux (5) MT4 (4) MT5 (4) Multicast (2) MySQL (2) Nijatrader8 (1) OpenCV (1) Python (4) QT5 (1) R (1) rdp (3) screenshot (1) ssh (3) Tabnine (1) TCP (1) TensorFlow (1) Tools (12) Ubuntu_1904 (11) Ubuntu_20_04 (5) UDP (1) VS2010 (1) VS2015 (1) VS2019 (1) WebServer (1) Win10 (1) winmerge (1) WSL (1) xrdp (1)

搜尋此網誌

2020年5月1日星期五

TCP Server and Client example for Linux

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

沒有留言:

發佈留言