歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux下服務端編程(二)

 if(listen(server_sockfd, LEN_LISTEN_QUEUE) < 0)

  {

  printf("call listen failure!\n");

  exit(1);

  }

  while(1)

  {

  char buf[BUFFER_SIZE];

  long timestamp;

  socklen_t length = sizeof(client_addr);

  client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_addr, &length);

  if(client_sockfd < 0)

  {

  printf("error comes when call accept!\n");

  break;

  }

  strcpy(buf, WELCOME_MESSAGE);

  printf("from client,IP:%s,Port:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

  timestamp = time(NULL);

  strcat(buf, "timestamp in server:");

  strcat(buf, ctime(& timestamp));

  send(client_sockfd, buf, BUFFER_SIZE, 0);

  close(client_sockfd);

  }

  close(server_sockfd);

  return 0;

  }

Copyright © Linux教程網 All Rights Reserved