dev bugfix socket close
This commit is contained in:
parent
ec69cfa8b2
commit
a12cee77f1
@ -24,6 +24,17 @@
|
|||||||
#ifndef strncasecmp
|
#ifndef strncasecmp
|
||||||
#define strncasecmp _strnicmp
|
#define strncasecmp _strnicmp
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef SHUT_RD
|
||||||
|
#define SHUT_RD SD_RECEIVE
|
||||||
|
#endif
|
||||||
|
#ifndef SHUT_WD
|
||||||
|
#define SHUT_WD SD_SEND
|
||||||
|
#endif
|
||||||
|
#ifndef SHUT_RDWD
|
||||||
|
#define SHUT_RDWD SD_BOTH
|
||||||
|
#endif
|
||||||
|
|
||||||
//#define socklen_t int
|
//#define socklen_t int
|
||||||
#elif LINUX_PART
|
#elif LINUX_PART
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -148,7 +159,7 @@ static inline int _sock_eai_res_map(int res) {
|
|||||||
// need to using freeaddrinfo to free <resaddr>
|
// need to using freeaddrinfo to free <resaddr>
|
||||||
static inline int _getaddrinfo(struct addrinfo* inf, struct addrinfo** resaddr,
|
static inline int _getaddrinfo(struct addrinfo* inf, struct addrinfo** resaddr,
|
||||||
const char* ip, const char* port) {
|
const char* ip, const char* port) {
|
||||||
if (resaddr == NULL) { return ERR_SOCK_NULLPTR; }
|
if (resaddr == NULL || ip == NULL || port == NULL) { return ERR_SOCK_NULLPTR; }
|
||||||
int res = getaddrinfo(ip, port, inf, resaddr);
|
int res = getaddrinfo(ip, port, inf, resaddr);
|
||||||
if (res != 0) {
|
if (res != 0) {
|
||||||
return -(_sock_eai_res_map(res));
|
return -(_sock_eai_res_map(res));
|
||||||
@ -300,6 +311,10 @@ static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigne
|
|||||||
char buf[8] = { 0 };
|
char buf[8] = { 0 };
|
||||||
sprintf(buf, "%u", port);
|
sprintf(buf, "%u", port);
|
||||||
int res = make_client_sock_ex(sock, connect_ip, buf, NULL);
|
int res = make_client_sock_ex(sock, connect_ip, buf, NULL);
|
||||||
|
if (res != ERR_SOCK_SUCCESS) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
res = sock_connect(*sock, connect_ip, port);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,11 +375,6 @@ static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, cons
|
|||||||
res = _socket(sock, addr);
|
res = _socket(sock, addr);
|
||||||
if (res != ERR_SOCK_SUCCESS) goto END;
|
if (res != ERR_SOCK_SUCCESS) goto END;
|
||||||
|
|
||||||
if (connect_ip) {
|
|
||||||
res = _connect(*sock, addr);
|
|
||||||
if (res != ERR_SOCK_SUCCESS) goto END;
|
|
||||||
}
|
|
||||||
|
|
||||||
END:
|
END:
|
||||||
if (resaddr == NULL) {
|
if (resaddr == NULL) {
|
||||||
freeaddrinfo(addr);
|
freeaddrinfo(addr);
|
||||||
|
@ -24,23 +24,38 @@ void receive_message(void* param)
|
|||||||
char Buf[1024] = { 0 };
|
char Buf[1024] = { 0 };
|
||||||
while (1) {
|
while (1) {
|
||||||
res = recv(cfd, Buf, sizeof(Buf), 0);
|
res = recv(cfd, Buf, sizeof(Buf), 0);
|
||||||
if (res > 0 && res <= 1024) {
|
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);
|
log_head_info_ex(buf, BUFFER_SIZE, "recv", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
tprintf(&std_mutex, "[Recv]:%d,\"%s\"\n", res, Buf);
|
tprintf(&std_mutex, "[Recv]:%d,\"%s\"\n", res, Buf);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
log_head_info_ex(buf, BUFFER_SIZE, "recv", g_mask);
|
log_head_info_ex(buf, BUFFER_SIZE, "recv", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
tprintf(&std_mutex, "server close connect, Close in three seconds\n");
|
tprintf(&std_mutex, "press enter to exit\n");
|
||||||
sleeps(3);
|
if (getchar());
|
||||||
exit(-1);
|
exit(-1);
|
||||||
// return NULL;
|
// return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void end(void) {
|
||||||
|
shutdown(cfd, SHUT_WD);
|
||||||
|
close_sock(cfd);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
@ -51,15 +66,28 @@ int main(int argc, char** argv)
|
|||||||
ini_get_str("client.ini", "base", "server_port", STR(_SOCKET_TEST_PORT), param[1], BUFFER_SIZE);
|
ini_get_str("client.ini", "base", "server_port", STR(_SOCKET_TEST_PORT), param[1], BUFFER_SIZE);
|
||||||
thread_mutex_init(&std_mutex);
|
thread_mutex_init(&std_mutex);
|
||||||
|
|
||||||
res = make_client_sock_ex(&cfd, param[0], param[1], NULL);
|
struct addrinfo* addr_info = NULL;
|
||||||
|
res = make_client_sock_ex(&cfd, param[0], param[1], &addr_info);
|
||||||
|
atexit(end);
|
||||||
if (res != 0) {
|
if (res != 0) {
|
||||||
log_head_fatal_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
|
log_head_fatal_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
tprintf(&std_mutex, "error client sock, error code:%d, press enter to continue\n", res);
|
tprintf(&std_mutex, "client start error... press enter to continue\n");
|
||||||
if (getchar());
|
if (getchar());
|
||||||
exit(-1);
|
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);
|
log_head_info_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
tprintf(&std_mutex, "conncet server success... [%s:%s]\n", param[0], param[1]);
|
tprintf(&std_mutex, "conncet server success... [%s:%s]\n", param[0], param[1]);
|
||||||
@ -71,6 +99,7 @@ int main(int argc, char** argv)
|
|||||||
log_head_info_ex(buf, BUFFER_SIZE, "command", g_mask);
|
log_head_info_ex(buf, BUFFER_SIZE, "command", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
tprintf(&std_mutex, "client exit... press enter to continue\n");
|
tprintf(&std_mutex, "client exit... press enter to continue\n");
|
||||||
|
shutdown(cfd, SHUT_WD);
|
||||||
if(getchar());
|
if(getchar());
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
@ -41,28 +41,36 @@ void receive_message(void* param)
|
|||||||
free(param);
|
free(param);
|
||||||
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
|
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
tprintf(&std_mutex, "recv start %d\n", sock);
|
tprintf(&std_mutex, "recv <%d> start...\n", sock);
|
||||||
while(1) {
|
while(1) {
|
||||||
res = recv(sock, Buf, sizeof(Buf), 0);
|
res = recv(sock, Buf, sizeof(Buf), 0);
|
||||||
if(res == -1 || res > sizeof(Buf) || res == 0) {
|
if (res == -1 || res > sizeof(Buf)) {
|
||||||
log_head_error_ex(buf, BUFFER_SIZE, "receive", g_mask);
|
log_head_error_ex(buf, BUFFER_SIZE, "receive", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
tprintf(&std_mutex, "recv error %d\n", sock);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
|
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
tprintf(&std_mutex, "recv buf:%d,\"%s\"\n", res, Buf);
|
tprintf(&std_mutex, "recv from <%d> buf[%d]:\"%s\"\n", sock, res, Buf);
|
||||||
send_the_message(sock, Buf, strlen(Buf)+1);
|
send_the_message(sock, Buf, strlen(Buf)+1);
|
||||||
}
|
}
|
||||||
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
|
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
|
shutdown(sock, SHUT_WD);
|
||||||
tprintf(&std_mutex, "socket <%d> exit\n", sock);
|
tprintf(&std_mutex, "socket <%d> exit\n", sock);
|
||||||
for(int i = 0; i < CLIENT_SIZE; i++) {
|
for(int i = 0; i < CLIENT_SIZE; i++) {
|
||||||
if(cfds[i] == sock) {
|
if(cfds[i] == sock) {
|
||||||
cfds[i] = 0;
|
cfds[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
close_sock(sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void acceptfunc(void* param) {
|
void acceptfunc(void* param) {
|
||||||
@ -109,6 +117,16 @@ void acceptfunc(void* param) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
char param[2][BUFFER_SIZE] = { 0 };
|
char param[2][BUFFER_SIZE] = { 0 };
|
||||||
@ -117,7 +135,7 @@ int main()
|
|||||||
thread_mutex_init(&std_mutex);
|
thread_mutex_init(&std_mutex);
|
||||||
|
|
||||||
int res = make_server_sock_ex(&sfd, param[0], param[1], NULL);
|
int res = make_server_sock_ex(&sfd, param[0], param[1], NULL);
|
||||||
|
atexit(end);
|
||||||
if(res != 0) {
|
if(res != 0) {
|
||||||
log_head_fatal_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
|
log_head_fatal_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
|
||||||
tprintf(&std_mutex, buf);
|
tprintf(&std_mutex, buf);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user