Tcp 我们可以在QNX C中运行多套接字客户端和线程中的每个客户端吗

Tcp 我们可以在QNX C中运行多套接字客户端和线程中的每个客户端吗,tcp,server,client,Tcp,Server,Client,我正在做一个项目,所有3台设备都在QNX中运行 2个套接字客户端(非阻塞)在一个CPU中运行,其余2个CPU作为服务器运行 当我测试运行时,我发现客户端出现了一些问题,在一定时间内没有收到来自服务器的回复,然后我不得不中断连接并重新建立 我怀疑在同一进程下是否有任何限制,2个客户端套接字线程是否不能运行?QNX中的esp。如果是这样,如何为每个客户端创建进程和进程上的线程 //客户端代码 int CNoseCapUnitECUCLient::iStartTCPIPHandler(eNCUOpti

我正在做一个项目,所有3台设备都在QNX中运行

2个套接字客户端(非阻塞)在一个CPU中运行,其余2个CPU作为服务器运行

当我测试运行时,我发现客户端出现了一些问题,在一定时间内没有收到来自服务器的回复,然后我不得不中断连接并重新建立

我怀疑在同一进程下是否有任何限制,2个客户端套接字线程是否不能运行?QNX中的esp。如果是这样,如何为每个客户端创建进程和进程上的线程

//客户端代码

int CNoseCapUnitECUCLient::iStartTCPIPHandler(eNCUOption选项)
{
printf(“启动%s NCU TCPIP处理程序\n”,(孟库选项==NCU空闲区)?“本地”:“远程”);
meNCUOption=eOption;
if(mbTCPHandThreadAlive)
{
istoptcipphandler();
}
其他的
{
iCreateMutex(mpMainMutex,TCPIPMutex);
}
//创建新的处理程序线程
MpandThread=CMNcreateThread();
如果(mpHandThread==nullptr)
{
返回1;
}
//线程优先级应至少等于LTA应用程序(21)
bool-bResult=mpandthread->create(“TCP\u HeartbeatThread”,0,0,0和HeartbeatThread,this);
if(bResult==false)
{
返回1;
}
mbHandling=true;
mbTCPHandThreadAlive=true;
返回EOK;
}
void CNoseCapUnitECUCLient::TCP_HeartbeatThread()
{
int-iRecvLen=0;
无符号字符buf=55;
strcpy((char*)Sendbuf,“ECU->NCU心跳\n”);
而(1)
{
cmnosleep(1);
mpMainMutex->take(200);
如果(!ConnectedToServer)
{
cmnosleep(1000);//连接后的延迟。
if(connectToNCU())
{
LastRecvTime=localClock_t::now();
ConnectedToServer=true;
}
}
else if(已连接到服务器)
{   
if(std::chrono::duration\u cast(localClock\u t::now()-LastSendTime).count()>50)//心跳
{
发送命令(msgsettimerheratbeat、MSG_NONE和buf,1);
}
其他的
{
strcpy((char*)ucRecvBuf,“”);
iRecvLen=recvResponse(ucRecvBuf);
如果(iRecvLen>0)
{
如果(strlen((char*)ucRecvBuf)>0)
{
IRECVRERROR=0;
if(commState==false)
{
iSetBrightness(液晶显示器亮度为100%);
肿瘤通讯(真);
}
ClientRxChar((char*)ucRecvBuf,iRecvLen);
}
}
else if(iRecvLen==0)
{
iRecvError++;
如果(IRECVRERROR>=5)
{
IRECVRERROR=0;
}
}
else if(iRecvLen==-1)
{
printf(“****客户端Recv函数返回false******\n”);
IRECVRERROR=0;
ConnectedToServer=false;
断开连接();
肿瘤通讯(假);
}
}
if(std::chrono::duration\u cast(localClock\u t::now()-LastRecvTime).count()>5000)//
{
printf(“****7秒钟内没有通信******\n”);
IRECVRERROR=0;
ConnectedToServer=false;
断开连接();
肿瘤通讯(假);
}
其他的
{
//printf(“接收时间%s%d%d\n”,(meNCUOption==NCU空闲区)?“本地”:“远程”,
//std::chrono::duration_cast(localClock_t::now()-LastRecvTime).count(),(localClock_t::now()-LastRecvTime.count());
}
ScanReceivedData();//扫描并调用相应的函数
}
mpMainMutex->release();
}
}
服务器端代码

void CNosecapTCPIPServer::ServerInit(int port)
{
        portno = port;
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if(listenfd < 0)
        {
            printf("ERROR opening socket \n");
        }

        optval = 1;
        setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
                 &optval , sizeof(int));

        bzero((char *) &serveraddr, sizeof(serveraddr));
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
        serveraddr.sin_port = htons((unsigned short)portno);

        if (bind(listenfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
        {
            m_errorCode = errno;
            snprintf(m_errorMsg, 128, "%s", strerror(m_errorCode));

            printf("ERROR on binding : %s \n", m_errorMsg );
        }
        printf("listen \n");
        if (listen(listenfd, 5) < 0)
        {
            m_errorCode = errno;
            snprintf(m_errorMsg, 128, "%s", strerror(m_errorCode));
            printf("ERROR on listen  %s \n", m_errorMsg);
        }
        clientlen = sizeof(clientaddr);
        //fcntl(listenfd, F_SETFL, O_NONBLOCK);
}


void CNosecapTCPIPServer::TCP_HeartbeatThread()
{
    while(1)
    {
        usleep(1000);
        //takeMutex(50);
        if(!ConnectedToClient)
        {
            //fcntl(listenfd, F_SETFL, O_NONBLOCK);   

            // accept: wait for a connection request
            connfd = accept(listenfd, (struct sockaddr *) &clientaddr, &clientlen);
            if (connfd < 0)
            {
            }
            else
            {
                lockMutex();
                               //fcntl(connfd, F_SETFL, O_NONBLOCK);      // blocking mode
                ConnectedToClient = true;
                LastRecvTime = localClock_t::now();
                printCurenttime();
                sprintf(printBuf, "Server established connection  (log) \n ");
                onLogMessage(printBuf);
                unlockMutex();
            }
        }
        else if(ConnectedToClient)
        {
            //// accept: wait for a connection request  
            if(connfd > 0)
            {
                strcpy((char*)buf,"");
                n = recv(connfd, buf, ***5000***,0 );   // used recv instead of read.
                //printf("recv connfd %d  n =  %d  errno = %d  \n", connfd, n, errno);
                if (n <= 0)
                {
                    if( n <  0  && errno != 11)   
                    {
                        //disconnect();
                        //continue;
                        sprintf(printBuf, "Read Error  n (%d) socket (%d)   error (%d)\n",n,  connfd  , errno);
                        onLogMessage(printBuf);
                    }
                    if(n == 0)
                    {
                         //LastRecvTime = localClock_t::now();
                    }
                }
                else
                {
                    lockMutex();
                    LastRecvTime = localClock_t::now();
                    ServerRxChar(buf, n);           // Update to the serialNessageQ
                    SendECUCommand(msgSetTimerHeartbeat,MSG_NONE, (unsigned char*)buf+8, 1);    
                    if(commState == false)
                    {
                        updateCommunicationStatus(true);
                    }

                    unlockMutex();
                }
            }
        }
        //unlockMutex();
    }
}


void CNosecapTCPIPServer::TCP_BufferHandleThread()
{
    while(1)
    {
        takeMutex(30);
            ScanReceivedData();  // scan and call the corresponding function

            if(ConnectedToClient)
            {
                if(std::chrono::duration_cast<std::chrono::milliseconds>(localClock_t::now() - LastRecvTime).count() > 8000) //

                {
                    sprintf(printBuf, " \t\t (1) Not Reveived more than 10 secs :  %d (%d)  \n", (localClock_t::now() - LastRecvTime).count(),
                            std::chrono::duration_cast<std::chrono::milliseconds>(localClock_t::now() - LastRecvTime).count());
                    onLogMessage(printBuf);
                    LastRecvTime = localClock_t::now();
                      if(setTimeCommand == false)
                         disconnect();
                }
                else
                {
                    //printf("\t\t   (1)last recv  %d (%d) \n",std::chrono::duration_cast<std::chrono::milliseconds>(localClock_t::now() - LastRecvTime).count(),
                    //      (localClock_t::now() - LastRecvTime).count());

                    if(setTimeCommand)
                        setTimeCommand = false;
                }
            }
        unlockMutex();
        usleep(1000);
    }
}
bool CNosecapTCPIPServer::SendCommand(void *pucCommand, int iLength)
 {
     int n;
     unsigned char tp[200];
     memcpy(tp, pucCommand, 200);

     if(connfd)
     {
        n = write(connfd, pucCommand, iLength);
        if (n < 0)
        {
            printf("    ERROR writing to socket (%d) error (%d) \n", connfd , errno);
            sprintf(printBuf , " ERROR writing to socket (%d) error (%d) \n", connfd , errno);
            onLogMessage(printBuf);
            //disconnect();
            //continue;
        }
     }
     return true;
 }
void CNosecapTCPIPServer::ServerInit(int端口)
{
端口号=端口号;
listenfd=套接字(AF_INET,SOCK_STREAM,0);
如果(列出的值小于0)
{
printf(“打开套接字时出错\n”);
}
optval=1;
setsockopt(listenfd、SOL_SOCKET、SO_REUSEADDR、,
&optval,sizeof(int));
bzero((char*)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin\u addr.s\u addr=htonl(INADDR\u ANY);
serveraddr.sin_port=htons((无符号短)端口号);
if(bind(listenfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr))<0
{
m_errorCode=errno;
snprintf(m_errorMsg,128,“%s”,strerror(m_errorCode));
printf(“绑定错误:%s\n”,m_errorMsg);
}
printf(“listen\n”);
如果(听(listenfd,5)<0)
{
m_errorCode=errno;
snprintf(m_errorMsg,128,“%s”,strerror(m_errorCode));
printf(“侦听%s\n时出错”,m_errorMsg);
}
clientlen=sizeof(clientaddr);
//fcntl(列表显示、F_设置、O_非块);
}
void CNosecapTCPIPServer::TCP_HeartbeatThread()
{
而(1)
{
usleep(1000);
//takeMutex(50);
如果(!ConnectedToClient)
{
//fcntl(列表显示、F_设置、O_非块);
//接受:等待连接请求
connfd=accept(listenfd,(struct sockaddr*)&clientaddr,&clientlen);
如果(connfd<0)
{
}
其他的
{
锁互斥体();
//fcntl(connfd,F_SETFL,O_NONBLOCK);//阻塞模式
ConnectedToClient=true;
LastRecvTime=localClock_t::now();
void CNosecapTCPIPServer::ServerInit(int port)
{
        portno = port;
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if(listenfd < 0)
        {
            printf("ERROR opening socket \n");
        }

        optval = 1;
        setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
                 &optval , sizeof(int));

        bzero((char *) &serveraddr, sizeof(serveraddr));
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
        serveraddr.sin_port = htons((unsigned short)portno);

        if (bind(listenfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
        {
            m_errorCode = errno;
            snprintf(m_errorMsg, 128, "%s", strerror(m_errorCode));

            printf("ERROR on binding : %s \n", m_errorMsg );
        }
        printf("listen \n");
        if (listen(listenfd, 5) < 0)
        {
            m_errorCode = errno;
            snprintf(m_errorMsg, 128, "%s", strerror(m_errorCode));
            printf("ERROR on listen  %s \n", m_errorMsg);
        }
        clientlen = sizeof(clientaddr);
        //fcntl(listenfd, F_SETFL, O_NONBLOCK);
}


void CNosecapTCPIPServer::TCP_HeartbeatThread()
{
    while(1)
    {
        usleep(1000);
        //takeMutex(50);
        if(!ConnectedToClient)
        {
            //fcntl(listenfd, F_SETFL, O_NONBLOCK);   

            // accept: wait for a connection request
            connfd = accept(listenfd, (struct sockaddr *) &clientaddr, &clientlen);
            if (connfd < 0)
            {
            }
            else
            {
                lockMutex();
                               //fcntl(connfd, F_SETFL, O_NONBLOCK);      // blocking mode
                ConnectedToClient = true;
                LastRecvTime = localClock_t::now();
                printCurenttime();
                sprintf(printBuf, "Server established connection  (log) \n ");
                onLogMessage(printBuf);
                unlockMutex();
            }
        }
        else if(ConnectedToClient)
        {
            //// accept: wait for a connection request  
            if(connfd > 0)
            {
                strcpy((char*)buf,"");
                n = recv(connfd, buf, ***5000***,0 );   // used recv instead of read.
                //printf("recv connfd %d  n =  %d  errno = %d  \n", connfd, n, errno);
                if (n <= 0)
                {
                    if( n <  0  && errno != 11)   
                    {
                        //disconnect();
                        //continue;
                        sprintf(printBuf, "Read Error  n (%d) socket (%d)   error (%d)\n",n,  connfd  , errno);
                        onLogMessage(printBuf);
                    }
                    if(n == 0)
                    {
                         //LastRecvTime = localClock_t::now();
                    }
                }
                else
                {
                    lockMutex();
                    LastRecvTime = localClock_t::now();
                    ServerRxChar(buf, n);           // Update to the serialNessageQ
                    SendECUCommand(msgSetTimerHeartbeat,MSG_NONE, (unsigned char*)buf+8, 1);    
                    if(commState == false)
                    {
                        updateCommunicationStatus(true);
                    }

                    unlockMutex();
                }
            }
        }
        //unlockMutex();
    }
}


void CNosecapTCPIPServer::TCP_BufferHandleThread()
{
    while(1)
    {
        takeMutex(30);
            ScanReceivedData();  // scan and call the corresponding function

            if(ConnectedToClient)
            {
                if(std::chrono::duration_cast<std::chrono::milliseconds>(localClock_t::now() - LastRecvTime).count() > 8000) //

                {
                    sprintf(printBuf, " \t\t (1) Not Reveived more than 10 secs :  %d (%d)  \n", (localClock_t::now() - LastRecvTime).count(),
                            std::chrono::duration_cast<std::chrono::milliseconds>(localClock_t::now() - LastRecvTime).count());
                    onLogMessage(printBuf);
                    LastRecvTime = localClock_t::now();
                      if(setTimeCommand == false)
                         disconnect();
                }
                else
                {
                    //printf("\t\t   (1)last recv  %d (%d) \n",std::chrono::duration_cast<std::chrono::milliseconds>(localClock_t::now() - LastRecvTime).count(),
                    //      (localClock_t::now() - LastRecvTime).count());

                    if(setTimeCommand)
                        setTimeCommand = false;
                }
            }
        unlockMutex();
        usleep(1000);
    }
}
bool CNosecapTCPIPServer::SendCommand(void *pucCommand, int iLength)
 {
     int n;
     unsigned char tp[200];
     memcpy(tp, pucCommand, 200);

     if(connfd)
     {
        n = write(connfd, pucCommand, iLength);
        if (n < 0)
        {
            printf("    ERROR writing to socket (%d) error (%d) \n", connfd , errno);
            sprintf(printBuf , " ERROR writing to socket (%d) error (%d) \n", connfd , errno);
            onLogMessage(printBuf);
            //disconnect();
            //continue;
        }
     }
     return true;
 }