#include #include #include #include #include #include #include #define BUFFER_SIZE 128 #define CLIENT_SIZE 32 SOCKET sfd; SOCKET cfds[CLIENT_SIZE]; MUTEX std_mutex; char buf[BUFFER_SIZE]; const log_mask_t g_mask = LOG_MASK_BASE_DEFAULT; #define _STR(str) #str #define STR(str) _STR(str) void send_the_message(int sock, const char *Buff, int szBuf) { char Buf[2048] = {0}; int tempsock; sprintf(Buf, ":", sock); strcat(Buf, Buff); for(int i = 0; i < CLIENT_SIZE; i++) { if(cfds[i] != 0 && cfds[i] != sock) { tempsock = cfds[i]; send(tempsock, Buf, strlen(Buf)+1, 0); } } } void receive_message(void* param) { int res; char Buf[1024] = {0}; int sock = *((SOCKET*)param); free(param); log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "recv <%d> start...\n", sock); while(1) { res = recv(sock, Buf, sizeof(Buf), 0); if (res == -1 || res > sizeof(Buf)) { log_head_error_ex(buf, BUFFER_SIZE, "receive", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "recv error <%d>:%d\n", sock, res); break; } if (res == 0) { log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "client <%d> close...\n", sock); break; } log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "recv from <%d> buf[%d]:\"%s\"\n", sock, res, Buf); send_the_message(sock, Buf, strlen(Buf)+1); } log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask); tprintf(&std_mutex, buf); shutdown(sock, SHUT_WD); tprintf(&std_mutex, "socket <%d> exit\n", sock); for(int i = 0; i < CLIENT_SIZE; i++) { if(cfds[i] == sock) { cfds[i] = 0; } } close_sock(sock); } void acceptfunc(void* param) { char *ip; unsigned short port; SOCKET* psock; SOCKET sock; psock = (SOCKET*)malloc(sizeof(SOCKET)); if(psock == NULL) { log_head_fatal_ex(buf, BUFFER_SIZE, "accept", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "server start error... press enter to continue\n"); exit(-1); } while(1) { if (sock_accpet(sfd, &sock, &ip, &port) != 0) { continue; } psock = (SOCKET*)malloc(sizeof(SOCKET)); if (psock == NULL) { log_head_fatal_ex(buf, BUFFER_SIZE, "accept", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "malloc error... press enter to continue\n"); if (getchar()); exit(-1); } *psock = sock; for(int i = 0; i < CLIENT_SIZE; i++) { if(cfds[i] == 0) { cfds[i] = sock; log_head_info_ex(buf, BUFFER_SIZE, "accept", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "\n", ip, port, sock); free(ip); thread_create(NULL, receive_message, (void*)psock); goto CONTINUE; } } log_head_warn_ex(buf, BUFFER_SIZE, "accept", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "client full"); CONTINUE: psock = NULL; } } void end(void) { shutdown(sfd, SHUT_WD); close_sock(sfd); for (int i = 0; i < CLIENT_SIZE; i++) { if (cfds[i] != 0) { close_sock(cfds[i]); } } } int main() { char param[2][BUFFER_SIZE] = { 0 }; ini_get_str("server.ini", "base", "server_ip", _SOCKET_TEST_IP4, param[0], BUFFER_SIZE); ini_get_str("server.ini", "base", "server_port", STR(_SOCKET_TEST_PORT), param[1], BUFFER_SIZE); thread_mutex_init(&std_mutex); int res = make_server_sock_ex(&sfd, param[0], param[1], NULL); atexit(end); if(res != 0) { log_head_fatal_ex(buf, BUFFER_SIZE, "ssocket", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "server start error... press enter to continue\n"); if(getchar()); return 0; } log_head_info_ex(buf, BUFFER_SIZE, "ssocket", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "server start... [%s:%s]\n", param[0], param[1]); thread_create(NULL, acceptfunc, NULL); while(1) { fgets(buf, sizeof(buf), stdin); if(strncasecmp(buf, "exit", strlen("exit")) == 0) { log_head_info_ex(buf, BUFFER_SIZE, "command", g_mask); tprintf(&std_mutex, buf); tprintf(&std_mutex, "server exit... press enter to continue\n"); if(getchar()); exit(-1); } } return 0; }