clib/test/project/client_proj/client_proj.c
2023-11-03 13:05:57 +08:00

126 lines
3.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#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)
{
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 == -1 || res > sizeof(Buf)) {
log_head_error_ex(buf, BUFFER_SIZE, "receive", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "recv error %d\n", res);
break;
}
if (res == 0) {
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "server close...\n");
break;
}
else {
log_head_info_ex(buf, BUFFER_SIZE, "recv", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "[Recv]:%d,\"%s\"\n", res, Buf);
}
}
log_head_info_ex(buf, BUFFER_SIZE, "recv", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "press enter to exit\n");
if (getchar());
exit(-1);
// return NULL;
}
void end(void) {
shutdown(cfd, SHUT_WD);
close_sock(cfd);
}
int main(int argc, char** argv)
{
int res;
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);
struct addrinfo* addr_info = NULL;
res = make_client_sock_ex(&cfd, param[0], param[1], &addr_info);
atexit(end);
if (res != 0) {
log_head_fatal_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "client start error... press enter to continue\n");
if (getchar());
return res;
}
CONNECT:
res = _connect(cfd, addr_info);
if (res != 0) {
log_head_error_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "connect server error, press enter to exit\n");
if(getchar());
goto CONNECT;
}
freeaddrinfo(addr_info);
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(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");
shutdown(cfd, SHUT_WD);
if(getchar());
exit(-1);
}
szlocal_buf = strlen(local_buf);
if (local_buf[szlocal_buf-1] == '\n') {
local_buf[szlocal_buf-1] = '\0';
}
else {
szlocal_buf += 1;
}
res = send(cfd, local_buf, szlocal_buf, 0);
if (res == -1) {
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);
}
log_head_info_ex(buf, BUFFER_SIZE, "send", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "[send]=%d,\"%s\"\n", szlocal_buf, local_buf);
}
return 0;
}