• linux环境下C++写TCP通信(一)


    #include<stdio.h>
    #include<string.h>
    //tcp
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<errno.h>
    //endtcp
    
    class CTcpSocket
    {
        public:
            CTcpSocket(void);
            virtual ~CTcpSocket(void);
            int ConnectServer(const char*szHost,long port);
            void Disconnect();
            
            int sendMessage();
            int recvMessage();
        private:
            struct sockaddr_in m_ConnAddr;
            int clientSock;
            bool isRev;
            bool m_bIsConnected;
    };
    
    class TCPModbusChannel
    {
        public:
            TCPModbusChannel();
            ~TCPModbusChannel();
    
            CTcpSocket tmpTCPobj;        
    
            bool GetTransducerData();
            int ReadHldReg();
            int ProcessTransducerReceivedRTU();
    };
    
    int main()
    {
        printf("Transducer Version 1.1
    ");
        int iRet = 0;
        char szHost[256] = "192.168.5.7";
        int nPort = 26;    
    
        CTcpSocket tmpTcp;
        iRet = tmpTcp.ConnectServer(szHost,nPort);
        if(iRet < 0)
        {
            printf("ConnectServer IP = %s, port = %d failed
    ",szHost,nPort);
        }
        return 0;
    }
    
    int CTcpSocket :: ConnectServer(const char*szHost,long port)
    {
        int iRet = -1;
        int iFlag = 0;
        int iLen = 0;
        char sendBuf[256];
        unsigned char reveBuf[256];
        int iDataNum;
        bool m_flag;
        do
        {
            if(NULL == szHost)
            {
                break;
            }        
            clientSock = socket(AF_INET,SOCK_STREAM,0);
            if(clientSock < 0)
            {
                perror("sock
    ");
            }
            m_ConnAddr.sin_family = AF_INET;
            m_ConnAddr.sin_port = htons(port);
            m_ConnAddr.sin_addr.s_addr = inet_addr(szHost);
    
            iLen = sizeof(m_ConnAddr);
            iRet = connect(clientSock,(struct sockaddr *)&m_ConnAddr,iLen);
            if(iRet < 0)
            {
                perror("connect
    ");
                return 1;
            }
            else
            {
                printf("ConnectServer IP = %s, port = %ld sucess
    ",szHost,port);
            }
            if(m_bIsConnected)
            {
                iRet = 1;
                break;
            }
    
            m_bIsConnected = true;
    
            m_flag = false;
            while(!m_flag)
            {
                iRet = sendMessage();
                if(iRet > 0) m_flag = true;
            }
            
            m_flag = false;
            while(!m_flag)
            {
                iRet = recvMessage();
                if(iRet > 0) m_flag = true;
            }
            
        }while(1 == iFlag);
        close(clientSock);
        return iRet;
    }
    
    
    //class CTcpSocket
    CTcpSocket::CTcpSocket(void)
    {
            //m_ConnAddr = -1;
            clientSock = -1;
            isRev = true;
            m_bIsConnected = false;
    }
    
    CTcpSocket::~CTcpSocket(void)
    {
            Disconnect();
    }
    
    void CTcpSocket::Disconnect()
    {
        close(clientSock);
        clientSock = -1;
        isRev = false;
    }
    
    int CTcpSocket::sendMessage()
    {
        int iRet ;
        char szSendbuf[256];
        sprintf(szSendbuf,"01 03 09 00 00 06 C6 54");
        if(m_bIsConnected)
        {
            iRet = send(clientSock,szSendbuf,sizeof(szSendbuf),0);
            if(iRet > 0)
            {
                printf("send message %s success
    ",szSendbuf);
            }
            else
            {
                printf("send message %s failed
    ",szSendbuf);
            }
        }
        return iRet;
    }
    
    int CTcpSocket::recvMessage()
    {
        int iRet;
        unsigned char szRecvbuf[256];
        iRet = recv(clientSock,szRecvbuf,sizeof(szRecvbuf),0);
        szRecvbuf[iRet] = 0;
        if(m_bIsConnected)
        {
            if(iRet < 0)
            {
                printf("recv data failed
    ");
            }
            else
            {
                printf("recv %d data  success
    ",iRet);    
                int iDataNum = 0;
                while(szRecvbuf[iDataNum])
                {
                    printf("%02X ",szRecvbuf[iDataNum++]);
                }
                printf("
    ");
            }
        }
        return iRet;
    }
    
    //end  class CTcpSocket

    makefile文件:

    test: test.o
        g++ test.o -o test
    test.o : test.cpp
        g++ -c test.cpp 
    
    clean:
        rm  test.o test

    make结果:

    exbot@ubuntu:~/wangqinghe/Transducer/20190611$ make
    g++ -c test.cpp
    g++ test.o -o test

    程序运行结果:

    运行结果需要用服务器程序互相通信或者使用模拟软件来互相通信。

    此处不展示了。

  • 相关阅读:
    vue 根据时间时间区间搜索功能
    vue 分页
    ubuntu18 vscode ros 配置
    在ubuntu16上用vscode编译ros历程记录
    word:页眉头部出现一条横线
    word:设置基偶页不同和页眉页脚
    多级标题
    添加论文应用
    添加论文尾注2(交叉引用)
    三线表
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11067036.html
Copyright © 2020-2023  润新知