TCP / IP套接字阻塞串行连接

时间:2012-11-28 03:33:01

标签: c sockets serial-port posix nonblocking

我有一个程序应该同时读取串口和tcp / ip socket。

但是,当我通过套接字收到消息时,我才能看到串行信息。我认为它与阻塞/非阻塞accept()方法有关。但是,我没有成功找出如何添加标志。

有人有什么想法吗?

我是否必须重写整个内容,还是只需添加一个标志?

int main(int argc, char *argv[]) {
  int       list_s;                /*  listening socket          */
  int       conn_s;                /*  connection socket         */
  short int port;                  /*  port number               */
  struct    sockaddr_in servaddr;  /*  socket address structure  */
  char      buffer[MAX_LINE];      /*  character buffer          */
  char     *endptr;                /*  for strtol()              */


  /*  Get port number from the command line, and
   *        set to default port if no arguments were supplied  */

  if ( argc == 2 ) {
    port = strtol(argv[1], &endptr, 0);
    if ( *endptr ) {
      fprintf(stderr, "ECHOSERV: Invalid port number.\n");
      exit(EXIT_FAILURE);
    }
  }
  else if ( argc < 2 ) {
    port = ECHO_PORT;
  }
  else {
    fprintf(stderr, "ECHOSERV: Invalid arguments.\n");
    exit(EXIT_FAILURE);
  }


  /*  Create the listening socket  */

  if ( (list_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
    fprintf(stderr, "ECHOSERV: Error creating listening socket.\n");
    exit(EXIT_FAILURE);
  }


  /*  Set all bytes in socket address structure to
   *        zero, and fill in the relevant data members   */

  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family      = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port        = htons(port);


  /*  Bind our socket addresss to the 
   *    listening socket, and call listen()  */

  if ( bind(list_s, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) {
    fprintf(stderr, "ECHOSERV: Error calling bind()\n");
    exit(EXIT_FAILURE);
  }

  if ( listen(list_s, LISTENQ) < 0 ) {
    fprintf(stderr, "ECHOSERV: Error calling listen()\n");
    exit(EXIT_FAILURE);
  }


  /*  Enter an infinite loop to respond
   *        to client requests and echo input  */
   int result = 0;
  int portID = -1;
  char *device = "/dev/ttyUSB1";
   int rate = convertRate("115200");
   char parity = convertParity("N");
   int databits = convertDatabits("8");
   int stopbits = convertStopbits("1");
  portID = posixComOpen(device,rate,parity,databits,stopbits);



  while ( 1 ) {

      char input = 0;

      while(posixComDataReady(portID) && posixComRead(portID, &input)) {
    printf("%c", input);
      } //while

      /* Write character to Vex */
     if(posixComWrite(portID, 'x') < 0)
       printf("POSIX: Error Writing char %c",'x');


    /*  Wait for a connection, then accept() it  */

    if ( (conn_s = accept(list_s, NULL, NULL) ) < 0 ) {
      fprintf(stderr, "ECHOSERV: Error calling accept()\n");
      exit(EXIT_FAILURE);
    }



    /*  Retrieve an input line from the connected socket
     *      then simply write it back to the same socket.     */
    Readline(conn_s, buffer, MAX_LINE-1);
    printf("Netbook got a TCP/IP message: %s\n", buffer);
    char* sendMessage = "Thanks for the message!";
    Writeline(conn_s,sendMessage , strlen(sendMessage));




    /*  Close the connected socket  */
    if ( close(conn_s) < 0 ) {
      fprintf(stderr, "ECHOSERV: Error calling close()\n");
      exit(EXIT_FAILURE);
    }



  }//while

  return 0;
}

1 个答案:

答案 0 :(得分:0)

你是对的,问题是accept阻塞,直到有新连接。

我不知道串行连接是如何工作的,但您可能希望查看select函数,该函数允许您等待几个不同的文件描述符,并在另一个上有数据时对它们做出响应端。

以下是如何使用select for socket的示例:Server Example