diff --git a/include/iini.h b/include/iini.h index 12a86b7..ebed14e 100644 --- a/include/iini.h +++ b/include/iini.h @@ -30,6 +30,9 @@ extern "C" { #endif +static int ini_get_str(const INICHAR* filename, const INICHAR* section, const INICHAR* key, + const INICHAR* def_vaule, INICHAR* buf, size_t szbuf); + static inline INICHAR* _ini_skip_leading(const INICHAR* str) { assert(str != NULL); diff --git a/include/ssocket.h b/include/ssocket.h index eb1e146..1a9892c 100644 --- a/include/ssocket.h +++ b/include/ssocket.h @@ -86,9 +86,9 @@ static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigne static inline void close_sock(SOCKET sock); static inline void out_sock_err(FILE* output, int errcode); static inline void get_sock_err(char* buff_128, size_t buff_len, int errcode); -static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, unsigned short port, +static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, const char* port, struct addrinfo** resaddr); -static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsigned short port, +static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, const char* port, struct addrinfo** resaddr); static inline int make_sock(SOCKET* sock); @@ -291,19 +291,23 @@ static inline int sock_accpet(SOCKET sock,SOCKET* client, char** accept_ip, unsi static inline int make_server_sock(SOCKET* sock, const char* server_ip, unsigned short port) { struct addrinfo* resaddr = NULL; - int res = make_server_sock_ex(sock, server_ip, port, &resaddr); + char buf[8] = { 0 }; + sprintf(buf, "%u", port); + int res = make_server_sock_ex(sock, server_ip, buf, &resaddr); if (resaddr) freeaddrinfo(resaddr); return res; } static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigned short port) { struct addrinfo* resaddr = NULL; - int res = make_client_sock_ex(sock, connect_ip, port, &resaddr); + char buf[8] = { 0 }; + sprintf(buf, "%u", port); + int res = make_client_sock_ex(sock, connect_ip, buf, &resaddr); if (resaddr) freeaddrinfo(resaddr); return res; } -static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, unsigned short port, +static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, const char* port, struct addrinfo** resaddr) { #ifdef WIN_PART WSADATA wsaData; @@ -317,10 +321,8 @@ static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, unsig } #endif int res = ERR_SOCK_SUCCESS; - char buf[8] = { 0 }; - sprintf(buf, "%u", port); struct addrinfo inf = { 0 }; - res = _getaddrinfo(&inf, resaddr, server_ip, buf); + res = _getaddrinfo(&inf, resaddr, server_ip, port); if (res != ERR_SOCK_SUCCESS) return res; res = _socket(sock, *resaddr); @@ -331,7 +333,7 @@ static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, unsig return res; } -static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsigned short port, +static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, const char* port, struct addrinfo** resaddr) { #ifdef WIN_PART WSADATA wsaData; @@ -345,10 +347,8 @@ static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsi } #endif int res = ERR_SOCK_SUCCESS; - char buf[8] = { 0 }; - sprintf(buf, "%u", port); struct addrinfo inf = { 0 }; - res = _getaddrinfo(&inf, resaddr, connect_ip, buf); + res = _getaddrinfo(&inf, resaddr, connect_ip, port); if (res != ERR_SOCK_SUCCESS) return res; res = _socket(sock, *resaddr); diff --git a/test/project/CMakeLists.txt b/test/project/CMakeLists.txt new file mode 100644 index 0000000..5eb1611 --- /dev/null +++ b/test/project/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.22.1) + +project(server_proj) + +set(EXECUTABLE_OUTPUT_PATH ../) +set(CMAKE_C_STANDARD 99) +set(SRC_FILE server_proj.c) + +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/server_proj.c b/test/project/server_proj.c new file mode 100644 index 0000000..06b094b --- /dev/null +++ b/test/project/server_proj.c @@ -0,0 +1,120 @@ +#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