This commit is contained in:
zzy 2023-11-02 22:53:52 +08:00
parent 8b2f07489c
commit 98794eb508
3 changed files with 97 additions and 40 deletions

View File

@ -290,20 +290,16 @@ 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;
char buf[8] = { 0 };
sprintf(buf, "%u", port);
int res = make_server_sock_ex(sock, server_ip, buf, &resaddr);
if (resaddr) freeaddrinfo(resaddr);
int res = make_server_sock_ex(sock, server_ip, buf, NULL);
return res;
}
static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigned short port) {
struct addrinfo* resaddr = NULL;
char buf[8] = { 0 };
sprintf(buf, "%u", port);
int res = make_client_sock_ex(sock, connect_ip, buf, &resaddr);
if (resaddr) freeaddrinfo(resaddr);
int res = make_client_sock_ex(sock, connect_ip, buf, NULL);
return res;
}
@ -322,14 +318,23 @@ static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, const
#endif
int res = ERR_SOCK_SUCCESS;
struct addrinfo inf = { 0 };
res = _getaddrinfo(&inf, resaddr, server_ip, port);
if (res != ERR_SOCK_SUCCESS) return res;
struct addrinfo* addr = NULL;
res = _getaddrinfo(&inf, &addr, server_ip, port);
if (res != ERR_SOCK_SUCCESS) goto END;
res = _socket(sock, *resaddr);
if (res != ERR_SOCK_SUCCESS) return res;
res = _socket(sock, addr);
if (res != ERR_SOCK_SUCCESS) goto END;
res = _bindlisten(*sock, *resaddr);
if (res != ERR_SOCK_SUCCESS) return res;
res = _bindlisten(*sock, addr);
if (res != ERR_SOCK_SUCCESS) goto END;
END:
if (resaddr == NULL) {
freeaddrinfo(addr);
}
else {
*resaddr = addr;
}
return res;
}
@ -348,15 +353,24 @@ static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, cons
#endif
int res = ERR_SOCK_SUCCESS;
struct addrinfo inf = { 0 };
res = _getaddrinfo(&inf, resaddr, connect_ip, port);
if (res != ERR_SOCK_SUCCESS) return res;
struct addrinfo* addr = NULL;
res = _getaddrinfo(&inf, &addr, connect_ip, port);
if (res != ERR_SOCK_SUCCESS) goto END;
res = _socket(sock, *resaddr);
if (res != ERR_SOCK_SUCCESS) return res;
res = _socket(sock, addr);
if (res != ERR_SOCK_SUCCESS) goto END;
if (connect_ip) {
res = _connect(*sock, *resaddr);
if (res != ERR_SOCK_SUCCESS) return res;
res = _connect(*sock, addr);
if (res != ERR_SOCK_SUCCESS) goto END;
}
END:
if (resaddr == NULL) {
freeaddrinfo(addr);
}
else {
*resaddr = addr;
}
return res;
}

View File

@ -3,23 +3,39 @@
#include <ssocket.h>
#include <tthread.h>
#include <iini.h>
#include <llog.h>
#define BUFFER_SIZE 128
SOCKET cfd;
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 receive_message(void* param)
{
printf("recv\n");
log_head_info_ex(buf, BUFFER_SIZE, "recv", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "recv start...\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);
log_head_info_ex(buf, BUFFER_SIZE, "recv", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "[Recv]:%d,%s", res, Buf);
}
else {
break;
}
}
printf("server close connect, Close in three seconds\n");
log_head_info_ex(buf, BUFFER_SIZE, "recv", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "server close connect, Close in three seconds\n");
sleeps(3);
exit(-1);
// return NULL;
@ -28,33 +44,54 @@ void receive_message(void* param)
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);
char param[2][BUFFER_SIZE] = { 0 };
char local_buf[BUFFER_SIZE] = { 0 };
int szlocal_buf = 0;
ini_get_str("client.ini", "base", "server_ip", _SOCKET_TEST_IP4, param[0], BUFFER_SIZE);
ini_get_str("client.ini", "base", "server_port", STR(_SOCKET_TEST_PORT), param[1], BUFFER_SIZE);
thread_mutex_init(&std_mutex);
res = make_client_sock_ex(&cfd, param[0], param[1], NULL);
if (res != 0) {
printf("error client sock\nerror code:%d\npress enter to continue\n", res);
log_head_fatal_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "error client sock, error code:%d, press enter to continue\n", res);
if(getchar());
exit(-1);
}
printf("conncet server success\n");
log_head_info_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "conncet server success... [%s:%s]\n", param[0], param[1]);
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");
fgets(local_buf, BUFFER_SIZE, stdin);
if (strncasecmp(local_buf, "exit", strlen("exit")) == 0) {
log_head_info_ex(buf, BUFFER_SIZE, "command", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "client exit... press enter to continue\n");
if(getchar());
exit(-1);
}
res = send(cfd, Buf, strlen(Buf) + 1, 0);
szlocal_buf = strlen(local_buf);
if (local_buf[szlocal_buf] == '\n') {
local_buf[szlocal_buf] = '\0';
}
else {
szlocal_buf += 1;
}
res = send(cfd, local_buf, szlocal_buf, 0);
if (res == -1) {
printf("send error %s", Buf);
printf("press enter to continue\n");
log_head_info_ex(buf, BUFFER_SIZE, "send", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "send error %s, press enter to continue\n", local_buf);
if(getchar());
exit(-1);
}
printf("[Buf]=%d,%s", res, Buf);
log_head_info_ex(buf, BUFFER_SIZE, "send", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "[Buf]=%d,%s", szlocal_buf, local_buf);
}
return 0;
}

View File

@ -15,6 +15,9 @@ 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};
@ -78,6 +81,14 @@ void acceptfunc(void* param) {
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) {
@ -98,9 +109,6 @@ void acceptfunc(void* param) {
}
}
#define _STR(str) #str
#define STR(str) _STR(str)
int main()
{
char param[2][BUFFER_SIZE] = { 0 };
@ -108,9 +116,7 @@ int main()
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);
int res = make_server_sock_ex(&sfd, param[0], param[1], NULL);
if(res != 0) {
log_head_fatal_ex(buf, BUFFER_SIZE, "ssocket", g_mask);