1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| int clnt_socks[100] = { 0 }; int clnt_cnt = 0; pthread_mutex_t mutex1;
void send_msg(const char* msg, ssize_t str_len) { pthread_mutex_lock(&mutex1); for (int i = 0; i < clnt_cnt; i++) { if (clnt_socks[i] >= 0) write(clnt_socks[i], msg, str_len); } pthread_mutex_unlock(&mutex1); }
void* handle_clnt(void* arg) { pthread_detach(pthread_self()); int clnt_sock = *(int*)arg; char msg[1024] = "";
ssize_t str_len = 0; while ((str_len = read(clnt_sock, msg, sizeof(msg))) > 0) { send_msg(msg, str_len); } pthread_mutex_lock(&mutex1); *(int*)arg = -1; pthread_mutex_unlock(&mutex1); close(clnt_sock); pthread_exit(NULL); }
void server98() { int serv_sock, clnt_sock; struct sockaddr_in serv_adr, clnt_adr; socklen_t clnt_adr_sz = sizeof(clnt_adr); serv_sock = socket(PF_INET, SOCK_STREAM, 0); memset(&serv_adr, 0, sizeof(serv_adr)); serv_adr.sin_family = AF_INET; serv_adr.sin_addr.s_addr = htonl(INADDR_ANY); serv_adr.sin_port = htons(9527); pthread_mutex_init(&mutex1, NULL); if (bind(serv_sock, (struct sockaddr*)&serv_adr, sizeof(serv_adr)) == -1) { printf("bind error %d %s", errno, strerror(errno)); return; } if (listen(serv_sock, 5) == -1) { printf("listen error %d %s", errno, strerror(errno)); return; }
for (;;) { clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz); if (clnt_sock == -1) { printf("accept error %d %s", errno, strerror(errno)); break; } pthread_mutex_lock(&mutex1); clnt_socks[clnt_cnt++] = clnt_sock; pthread_mutex_unlock(&mutex1); pthread_t thread;
pthread_create(&thread, NULL, handle_clnt, &clnt_socks[clnt_cnt - 1]); } close(serv_sock); pthread_mutex_destroy(&mutex1); } sem_t semid; char name[64] = "[DEFAULT]"; void* client_send_msg(void* arg) { pthread_detach(pthread_self()); int sock = *(int*)arg; char msg[256] = ""; char buffer[1024]; while (true) { memset(buffer, 0, sizeof(buffer)); fgets(msg, sizeof(msg), stdin); if ((strcmp(msg, "q\n") == 0) || (strcmp(msg, "Q\n") == 0)) { break; } snprintf(buffer, sizeof(msg), "%s %s", name, msg); write(sock, buffer, strlen(buffer)); } sem_post(&semid); pthread_exit(NULL); }
void* client_recv_msg(void* arg) { pthread_detach(pthread_self()); int sock = *(int*)arg; char msg[256] = ""; while (true) { ssize_t str_len = read(sock, msg, sizeof(msg)); if (str_len <= 0)break; fputs(msg, stdout); memset(msg, 0, str_len); } sem_post(&semid); pthread_exit(NULL); }
void client98() { int sock = socket(PF_INET, SOCK_STREAM, 0); struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); serv_addr.sin_port = htons(9527); fputs("input your name:", stdout); scanf("%s", name); if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1) { printf("connect error %d %s", errno, strerror(errno)); return; } pthread_t thsend, threcv; sem_init(&semid, 0, -1); pthread_create(&thsend, NULL, client_send_msg, (void*)&sock); pthread_create(&threcv, NULL, client_recv_msg, (void*)&sock); sem_wait(&semid); close(sock); }
void lession98(const char* arg) { if (strcmp(arg, "s") == 0) { server98(); } else { client98(); } }
|