多线程并发服务器端的实现

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <sys/socket.h>
  7. #include <pthread.h>
  8. #define BUF_SIZE 100
  9. #define MAX_CLNT 256
  10. void* handle_clnt(void *arg);
  11. void send_msg(char *msg, int len);
  12. void error_handling(char *msg);
  13. pthread_mutex_t mutx;
  14. int clnt_socks[MAX_CLNT];
  15. int clnt_cnt = 0;
  16. int main(int argc, char *argv[]) {
  17. // 1.检查输入
  18. if (argc != 2) {
  19. printf("Usage: %s <port>\n", argv[0]);
  20. exit(1);
  21. }
  22. // 2.初始化套接字
  23. int serv_sock;
  24. struct sockaddr_in serv_adr;
  25. pthread_mutex_init(&mutx, NULL);
  26. serv_sock = socket(PF_INET, SOCK_STREAM, 0);
  27. memset(&serv_adr, 0, sizeof(serv_adr));
  28. serv_adr.sin_family = AF_INET;
  29. serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
  30. serv_adr.sin_port = htons(atoi(argv[1]));
  31. // 3.bind() 套接字
  32. if (bind(serv_sock, (struct sockaddr*)&serv_adr, sizeof(serv_adr)) == -1)
  33. error_handling("bind() error");
  34. // 4.listen() 监听套接字
  35. if (listen(serv_sock, 5) == -1)
  36. error_handling("listen() error");
  37. struct sockaddr_in clnt_adr;
  38. int clnt_adr_sz;
  39. int clnt_sock;
  40. pthread_t t_id;
  41. while (1) {
  42. clnt_adr_sz = sizeof(clnt_adr);
  43. // 5.接入新的客户端
  44. clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz);
  45. // 6.因为主线程和其他新建的子线程 1 可能同时访问 clnt_socks,所以需要锁
  46. pthread_mutex_lock(&mutx); // lock
  47. clnt_socks[clnt_cnt++] = clnt_sock; // 主线程要访问临界区,有新的客户连接添加到 clnt_socks 中
  48. pthread_mutex_unlock(&mutx); // unlock
  49. // 7.创建线程 1 向新接入的客户端提供服务
  50. pthread_create(&t_id, NULL, handle_clnt, (void*)&clnt_sock);
  51. // 8.调用 pthread_detach 函数从内存中完全销毁已终止的线程
  52. pthread_detach(t_id);
  53. printf("Connected client IP: %s \n", inet_ntoa(clnt_adr.sin_addr));
  54. }
  55. close (serv_sock);
  56. return 0;
  57. }
  58. void* handle_clnt(void *arg) {
  59. int clnt_sock = *((int*)arg);
  60. int str_len = 0, i;
  61. char msg[BUF_SIZE];
  62. while ((str_len = read(clnt_sock, msg, sizeof(msg))) != 0)
  63. send_msg(msg, str_len);
  64. pthread_mutex_lock(&mutx); // lock
  65. for (i=0; i<clnt_cnt; i++) { // 访问临界区 clnt_cnt
  66. if (clnt_sock == clnt_socks[i]) { // 访问临界区 clnt_socks
  67. while (i++ < clnt_cnt-1)
  68. clnt_socks[i] = clnt_socks[i+1]; // 访问临界区 clnt_socks
  69. break;
  70. }
  71. }
  72. clnt_cnt--; // 访问临界区 clnt_cnt
  73. pthread_mutex_unlock(&mutx); // unlock
  74. close(clnt_sock);
  75. return NULL;
  76. }
  77. // 这里通过循环向所有的客户端发送消息
  78. void send_msg(char *msg, int len) {
  79. int i;
  80. pthread_mutex_lock(&mutx); // lock
  81. for (i=0; i<clnt_cnt; i++) // 访问临界区 clnt_cnt
  82. write(clnt_socks[i], msg, len); // 访问临界区 clnt_socks
  83. pthread_mutex_unlock(&mutx); // unlock
  84. }
  85. void error_handling(char *msg) {
  86. fputs(msg, stderr);
  87. fputc('\n', stderr);
  88. exit(1);
  89. }

§ 多线程服务器端 - 图1

所以写这些代码的时候,先不管临界区。把其他的代码写完以后,通过变量来划分临界区。

客户端

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <sys/socket.h>
  7. #include <pthread.h>
  8. #define BUF_SIZE 100
  9. #define NAME_SIZE 20
  10. void* send_msg(void *arg);
  11. void* recv_msg(void *arg);
  12. void error_handling(char *msg);
  13. char name[NAME_SIZE] = "[DEFAULT]";
  14. char msg[BUF_SIZE];
  15. int main(int argc, char *argv[]) {
  16. // 1.检查输入
  17. if (argc != 4) {
  18. printf("Usage: %s <IP> <port> <name> \n", argv[0]);
  19. exit(1);
  20. }
  21. sprintf(name, "[%s]", argv[3]);
  22. // 2.初始化套接字
  23. int sock;
  24. struct sockaddr_in serv_addr;
  25. sock = socket(PF_INET, SOCK_STREAM, 0);
  26. memset(&serv_addr, 0, sizeof(serv_addr));
  27. serv_addr.sin_family = AF_INET;
  28. serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
  29. serv_addr.sin_port = htons(atoi(argv[2]));
  30. // 3.向服务器端发起请求 connect
  31. if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1)
  32. error_handling("connect() error");
  33. // 4.创建线程,一个线程用于发送消息,另一个线程用于接收消息
  34. pthread_t snd_thread, rcv_thread;
  35. pthread_create(&snd_thread, NULL, send_msg, (void*)&sock);
  36. pthread_create(&rcv_thread, NULL, recv_msg, (void*)&sock);
  37. // 5.使用 thread_join 来监视线程的结束
  38. void *thread_return;
  39. pthread_join(snd_thread, &thread_return);
  40. pthread_join(rcv_thread, &thread_return);
  41. close(sock);
  42. return 0;
  43. }
  44. void* send_msg(void *arg) {
  45. int sock = *((int*) arg);
  46. char name_msg[NAME_SIZE+BUF_SIZE];
  47. while(1) {
  48. fgets(msg, BUF_SIZE, stdin);
  49. if (!strcmp(msg, "q\n") || !strcmp(msg, "Q\n")) {
  50. close (sock);
  51. exit(0);
  52. }
  53. sprintf(name_msg, "%s %s", name, msg);
  54. write(sock, name_msg, strlen(name_msg));
  55. }
  56. return NULL;
  57. }
  58. void* recv_msg(void *arg) {
  59. int sock = *((int*)arg);
  60. char name_msg[NAME_SIZE+BUF_SIZE];
  61. int str_len;
  62. while(1) {
  63. str_len = read(sock, name_msg, NAME_SIZE+BUF_SIZE-1);
  64. if (str_len == -1)
  65. return (void*)-1;
  66. name_msg[str_len] = 0;
  67. fputs(name_msg, stdout);
  68. }
  69. return NULL;
  70. }
  71. void error_handling(char *msg) {
  72. fputs(msg, stderr);
  73. fputc('\n', stderr);
  74. exit(1);
  75. }

运行以上程序

客户端:

§ 多线程服务器端 - 图2

服务器端:

§ 多线程服务器端 - 图3