c线程创建中的编程错误?

时间:2014-02-26 13:04:47

标签: c linux multithreading sockets timer

void *newThread(void *arg)
{
  makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
  makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
  makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms
  return 0;
}


int CreateSocket()
{
  pthread_t tid;
  socklen_t len = sizeof(client);
  // Socket creation for UDP

  acceptSocket=socket(AF_INET,SOCK_DGRAM,0);

  if (acceptSocket==-1)
  {
    printf("Failure: socket creation is failed, failure code\n");
    return 1;
  }
  else
  {
    printf("Socket started!\n");
  }

  //non blocking mode
  /* rc = ioctl(acceptSocket, FIONBIO, (char *)&flag);
      if (rc < 0)
      {
                 printf("\n ioctl() failed \n");
         return 0;
      }*/

  //Bind the socket
  memset(&addr, 0, sizeof(addr));
  addr.sin_family=AF_INET;
  addr.sin_port=htons(port);
  addr.sin_addr.s_addr=htonl(INADDR_ANY);
  rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));
  if (rc== -1)
  {
    printf("Failure: listen, failure code:\n");
    return 1;
  }
  else
  {
    printf("Socket an port %d \n",port);
  }

  if (acceptSocket == -1)
  {
    printf("Fehler: accept, fehler code:\n");
    return 1;
  }
  else
  {
    pthread_create(&tid, NULL, &newThread, NULL);

    while (rc!=-1)
    {
      rc=recvfrom(acceptSocket,buf, 256, 0, (struct sockaddr*) &client, &len);

      if (rc==0)
      {
        printf("Server has no connection..\n");
        break;
      }

      if (rc==-1)
      {
        printf("something went wrong with data %s", strerror(errno));
        break;
      }

      XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
    }
  }

  close(acceptSocket);
  return 0;
}


int main()
{
  Xcp_Initialize();
  CreateSocket();
  return 0;
}

我正在研究客户端和服务器架构。服务器代码如上所示,我创建了一个套接字,通过ip地址和端口号从客户端接收请求。服务器正在等待来自客户端的请求并将响应发送回客户端。我还创建了定时器,每2ms,10ms和100ms调用te任务。我创建了一个用于调用计时器任务的独立线程。在运行之后调用线程,之后它不会从客户端收到任何数据。什么是错误?

0 个答案:

没有答案