当前位置:   article > 正文

c语言实现简单的tcp服务端_c语言创建server端主进程

c语言创建server端主进程

功能:监听本地8888端口,接收到客户端连接请求后创建线程单独处理与客户端的交互,支持同时与多个客户端交互。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <pthread.h>
  9. #define PORT 8888
  10. void *deal_client(void *args)
  11. {
  12. int fd;
  13. int i;
  14. unsigned char recv_buff[1024] = {0};
  15. int read_len = 0;
  16. int send_len = 0;
  17. unsigned char *send_data = "hello,this is a tcp server\n";
  18. fd = (int) *(int *) args;
  19. printf("fd %d\n", fd);
  20. if (fd < 0)
  21. {
  22. printf("socket err\n");
  23. return NULL;
  24. }
  25. while (1)
  26. {
  27. memset(recv_buff, 0x00, sizeof (recv_buff));
  28. read_len = recv(fd, recv_buff, sizeof (recv_buff), 0);
  29. if (read_len < 0)
  30. {
  31. printf("recv err\n");
  32. close(fd);
  33. fd = -1;
  34. return NULL;
  35. } else if (read_len == 0)
  36. {
  37. printf("close \n");
  38. close(fd);
  39. fd = -1;
  40. return NULL;
  41. }
  42. printf("recv data: %s\n", recv_buff);
  43. // for (i = 0; i < read_len; i++)
  44. // {
  45. // printf("%02x ", recv_buff[i]);
  46. // }
  47. // printf("\n");
  48. send_len = send(fd, send_data, strlen(send_data), 0);
  49. if (send_len <= 0)
  50. {
  51. printf("send err\n");
  52. close(fd);
  53. fd = -1;
  54. return NULL;
  55. }
  56. }
  57. }
  58. int main()
  59. {
  60. //创建套接字
  61. int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  62. if (socket_fd < 0)
  63. {
  64. printf("socket err\n");
  65. return -1;
  66. }
  67. printf("socket create success sfd=%d\n", socket_fd);
  68. //设置端口重用
  69. int resue = 1;
  70. if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof (resue)) < 0)
  71. {
  72. printf("setsockopt SO_REUSEADDR err\n");
  73. return -1;
  74. }
  75. //填充服务器的地址信息结构体
  76. struct sockaddr_in sin;
  77. sin.sin_family = AF_INET;
  78. sin.sin_port = htons(PORT);
  79. sin.sin_addr.s_addr = inet_addr("0.0.0.0");
  80. //绑定端口号
  81. if (bind(socket_fd, (struct sockaddr*) &sin, sizeof (sin)) < 0)
  82. {
  83. printf("bind err\n");
  84. return -1;
  85. }
  86. printf("bind success\n");
  87. //监听端口号
  88. if (listen(socket_fd, 5) == -1)
  89. {
  90. printf("listen err\n");
  91. return -1;
  92. }
  93. printf("listen success\n");
  94. while (1)
  95. {
  96. struct sockaddr_in cin; //存储连接成功的客户端地址
  97. socklen_t addrlen = sizeof (cin);
  98. int socket_cli = -1;
  99. socket_cli = accept(socket_fd, (struct sockaddr*) &cin, &addrlen);
  100. if (socket_cli < 0)
  101. {
  102. printf("accept err\n");
  103. continue;
  104. }
  105. printf("socket_cli %d\n", socket_cli);
  106. pthread_t tid = -1;
  107. pthread_create(&tid, NULL, deal_client, &socket_cli);
  108. usleep(1000); //注意这里必须sleep一下,不然参数传递不过去
  109. }
  110. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/886196
推荐阅读
相关标签
  

闽ICP备14008679号

        
cppcmd=keepalive&