diff --git a/.gitignore b/.gitignore index d75b649..3d6e315 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ !doc/* !src/* !test/*/* +!test/project/client_proj/* +!test/project/server_proj/* \ No newline at end of file diff --git a/include/iini.h b/include/iini.h index ebed14e..f4bf92b 100644 --- a/include/iini.h +++ b/include/iini.h @@ -155,7 +155,7 @@ static int _int_get_all_string(FILE* fp, const INICHAR* section, const INICHAR* static int ini_get_str(const INICHAR* filename, const INICHAR* section, const INICHAR* key, const INICHAR* def_vaule, INICHAR* buf, size_t szbuf) { FILE* fp = NULL; - int res = 0; + int res = -1; if (fp = fopen(filename, "rb")) { res = _int_get_all_string(fp, section, key, -1, -1, buf, szbuf); diff --git a/test/client/client.c b/test/client/client.c index 4f1f8d6..4c8f138 100644 --- a/test/client/client.c +++ b/test/client/client.c @@ -30,10 +30,10 @@ int main(int argc, char** argv) int res; char Buf[1024] = { 0 }; printf("connect server...\n"); - res = make_client_sock(&cfd, _SOCKET_TEST_IP6, _SOCKET_TEST_PORT); + res = make_client_sock(&cfd, _SOCKET_TEST_IP4, _SOCKET_TEST_PORT); if (res != 0) { printf("error client sock\nerror code:%d\npress enter to continue\n", res); - getchar(); + if(getchar()); exit(-1); } printf("conncet server success\n"); @@ -44,14 +44,14 @@ int main(int argc, char** argv) fgets(Buf, sizeof(Buf), stdin); if (strncasecmp(Buf, "exit", strlen("exit")) == 0) { printf("press enter to continue\n"); - getchar(); + if(getchar()); exit(-1); } res = send(cfd, Buf, strlen(Buf) + 1, 0); if (res == -1) { printf("send error %s", Buf); printf("press enter to continue\n"); - getchar(); + if(getchar()); exit(-1); } printf("[Buf]=%d,%s", res, Buf); diff --git a/test/project/CMakeLists.txt b/test/project/CMakeLists.txt index 5eb1611..1c8eb28 100644 --- a/test/project/CMakeLists.txt +++ b/test/project/CMakeLists.txt @@ -1,16 +1,10 @@ cmake_minimum_required(VERSION 3.22.1) -project(server_proj) +project(proj) set(EXECUTABLE_OUTPUT_PATH ../) -set(CMAKE_C_STANDARD 99) -set(SRC_FILE server_proj.c) +add_subdirectory(server_proj server_proj) +add_subdirectory(client_proj client_proj) -include_directories(../../include) -add_executable(${PROJECT_NAME} ${SRC_FILE}) - -if(WIN32) - target_link_libraries(${PROJECT_NAME} wsock32 ws2_32) -endif() # cmake .. # cmake --build . \ No newline at end of file diff --git a/test/project/client_proj/CMakeLists.txt b/test/project/client_proj/CMakeLists.txt new file mode 100644 index 0000000..0869f90 --- /dev/null +++ b/test/project/client_proj/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.22.1) + +project(client_proj) + +set(EXECUTABLE_OUTPUT_PATH ../../) +set(CMAKE_C_STANDARD 99) + +include_directories(../../../include) +add_executable(client_proj client_proj.c) + +if(WIN32) + target_link_libraries(${PROJECT_NAME} wsock32 ws2_32) +endif() +# cmake .. +# cmake --build . \ No newline at end of file diff --git a/test/project/client_proj/client_proj.c b/test/project/client_proj/client_proj.c new file mode 100644 index 0000000..4c8f138 --- /dev/null +++ b/test/project/client_proj/client_proj.c @@ -0,0 +1,60 @@ +#include +#include + +#include +#include +SOCKET cfd; + +void receive_message(void* param) +{ + printf("recv\n"); + int res = 0; + char Buf[1024] = { 0 }; + while (1) { + res = recv(cfd, Buf, sizeof(Buf), 0); + if (res > 0 && res <= 1024) { + printf("[Recv]:%d,%s", res, Buf); + } + else { + break; + } + } + printf("server close connect, Close in three seconds\n"); + sleeps(3); + exit(-1); + // return NULL; +} + +int main(int argc, char** argv) +{ + int res; + char Buf[1024] = { 0 }; + printf("connect server...\n"); + res = make_client_sock(&cfd, _SOCKET_TEST_IP4, _SOCKET_TEST_PORT); + if (res != 0) { + printf("error client sock\nerror code:%d\npress enter to continue\n", res); + if(getchar()); + exit(-1); + } + printf("conncet server success\n"); + + thread_create(NULL, receive_message, NULL); + + while (1) { + fgets(Buf, sizeof(Buf), stdin); + if (strncasecmp(Buf, "exit", strlen("exit")) == 0) { + printf("press enter to continue\n"); + if(getchar()); + exit(-1); + } + res = send(cfd, Buf, strlen(Buf) + 1, 0); + if (res == -1) { + printf("send error %s", Buf); + printf("press enter to continue\n"); + if(getchar()); + exit(-1); + } + printf("[Buf]=%d,%s", res, Buf); + } + return 0; +} \ No newline at end of file diff --git a/test/project/server_proj.c b/test/project/server_proj.c deleted file mode 100644 index 06b094b..0000000 --- a/test/project/server_proj.c +++ /dev/null @@ -1,120 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - -#define BUFFER_SIZE 128 -#define CLIENT_SIZE 32 -SOCKET sfd; -SOCKET cfds[CLIENT_SIZE]; - -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); - printf("recv start %d\n", sock); - while(1) { - res = recv(sock, Buf, sizeof(Buf), 0); - if(res == -1 || res > sizeof(Buf) || res == 0) { -#if WIN_PART - printf("\n", GetLastError()); -#elif LINUX_PART - perror("recv error"); -#endif - break; - } - printf("recv buf:%d,%s",res, Buf); - send_the_message(sock, Buf, strlen(Buf)+1); - } - printf(" exit\n", sock); - for(int i = 0; i < CLIENT_SIZE; i++) { - if(cfds[i] == sock) { - cfds[i] = 0; - } - } -} - -void acceptfunc(void* param) { - char *ip; - unsigned short port; - SOCKET* psock; - SOCKET sock; - while(1) { - psock = (SOCKET*)malloc(sizeof(SOCKET)); - if(psock == NULL) { - exit(-1); - } - if (sock_accpet(sfd, &sock, &ip, &port) != 0) { - continue; - } - *psock = sock; - for(int i = 0; i < CLIENT_SIZE; i++) { - if(cfds[i] == 0) { - cfds[i] = sock; - printf("\n", ip, port, sock); - free(ip); - thread_create(NULL, receive_message, (void*)psock); - goto CONTINUE; - } - } - printf("client full"); - exit(-2); - CONTINUE: - psock = NULL; - } -} - -#define _STR(str) #str -#define STR(str) _STR(str) - -int main() -{ - char param[2][BUFFER_SIZE] = { 0 }; - char Buf[BUFFER_SIZE]; - int szbuf = BUFFER_SIZE; - szbuf -= 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); - - void* ptr = NULL; - int res = make_server_sock_ex(&sfd, param[0], param[1], &ptr); - freeaddrinfo(ptr); - - if(res != 0) { - printf("server start error\npress enter to continue"); - getchar(); - return 0; - } - printf("server start...\n"); - thread_create(NULL, acceptfunc, NULL); - while(1) { - fgets(Buf, sizeof(Buf), stdin); - printf("%s", Buf); - if(strncasecmp(Buf, "exit", strlen("exit")) == 0) { - printf("exit success\npress enter to continue"); - getchar(); - exit(-1); - } - } - return 0; -} \ No newline at end of file diff --git a/test/project/server_proj/CMakeLists.txt b/test/project/server_proj/CMakeLists.txt new file mode 100644 index 0000000..80e9d22 --- /dev/null +++ b/test/project/server_proj/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.22.1) + +project(server_proj) + +set(EXECUTABLE_OUTPUT_PATH ../../) +set(CMAKE_C_STANDARD 99) + +include_directories(../../../include) +add_executable(server_proj server_proj.c) + +if(WIN32) + target_link_libraries(${PROJECT_NAME} wsock32 ws2_32) +endif() +# cmake .. +# cmake --build . \ No newline at end of file diff --git a/test/project/server_proj/server_proj.c b/test/project/server_proj/server_proj.c new file mode 100644 index 0000000..41aa393 --- /dev/null +++ b/test/project/server_proj/server_proj.c @@ -0,0 +1,138 @@ +#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; + +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 start %d\n", sock); + while(1) { + res = recv(sock, Buf, sizeof(Buf), 0); + if(res == -1 || res > sizeof(Buf) || res == 0) { + log_head_error_ex(buf, BUFFER_SIZE, "receive", g_mask); + tprintf(&std_mutex, buf); + tprintf(&std_mutex, "recv error %d\n", sock); + break; + } + log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask); + tprintf(&std_mutex, buf); + tprintf(&std_mutex, "recv buf:%d,%s", res, Buf); + send_the_message(sock, Buf, strlen(Buf)+1); + } + log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask); + tprintf(&std_mutex, buf); + tprintf(&std_mutex, "socket <%d> exit\n", sock); + for(int i = 0; i < CLIENT_SIZE; i++) { + if(cfds[i] == sock) { + cfds[i] = 0; + } + } +} + +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 = 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; + } +} + +#define _STR(str) #str +#define STR(str) _STR(str) + +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); + + void* ptr = NULL; + int res = make_server_sock_ex(&sfd, param[0], param[1], &ptr); + freeaddrinfo(ptr); + + 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; +} \ No newline at end of file