From c3df8f97e7e8164b95c4ba20d890ed7c0cbdcff5 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Fri, 27 Oct 2023 18:19:22 +0800 Subject: [PATCH] =?UTF-8?q?bugfix=20=E4=BF=AE=E5=A4=8D=E6=97=A0=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E7=9A=84=E9=97=AE=E9=A2=98,=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86sock=5Fbind=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ssocket.h | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/include/ssocket.h b/include/ssocket.h index 8bd1654..8b69ce0 100644 --- a/include/ssocket.h +++ b/include/ssocket.h @@ -76,21 +76,27 @@ enum { ERR_SOCK_EAI_SOCKTYPE, }; -static inline int make_sock(SOCKET* sock); -static inline int sock_connect(SOCKET sock, const char* connect_ip, unsigned short port); -static inline int sock_bindlisten(SOCKET sock, const char* server_ip, unsigned short port); -static inline int sock_accpet(SOCKET sock,SOCKET* client, char** accept_ip, unsigned short* port); + static inline int make_server_sock(SOCKET* sock, const char* server_ip, unsigned short port); static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigned short port); 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, struct addrinfo** resaddr); static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsigned short port, struct addrinfo** resaddr); +static inline int make_sock(SOCKET* sock); +static inline int sock_connect(SOCKET sock, const char* connect_ip, unsigned short port); +static inline int sock_bind(SOCKET sock, const char* server_ip, unsigned short port); +static inline int sock_bindlisten(SOCKET sock, const char* server_ip, unsigned short port); +static inline int sock_accpet(SOCKET sock, SOCKET* client, char** accept_ip, unsigned short* port); +static inline int make_sock_tcp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_STREAM); } +static inline int make_sock_tcp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_STREAM); } +static inline int make_sock_udp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_DGRAM); } +static inline int make_sock_udp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_DGRAM); } + static inline int _getaddrinfo(struct addrinfo* inf, struct addrinfo** resaddr, const char* ip, const char* port); static inline int _socket(SOCKET* sock, struct addrinfo* resaddr); @@ -98,12 +104,8 @@ static inline int _connect(SOCKET sock, struct addrinfo* resaddr); static inline int _bind(SOCKET sock, struct addrinfo* resaddr); static inline int _bindlisten(SOCKET sock, struct addrinfo* resaddr); static inline int _accept(SOCKET sock, SOCKET* client, struct addrinfo* inf); - static inline int _sock(SOCKET* sock, int af, int type); -static inline int make_sock_tcp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_STREAM); } -static inline int make_sock_tcp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_STREAM); } -static inline int make_sock_udp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_DGRAM); } -static inline int make_sock_udp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_DGRAM); } + //not recommand and it will be remove static inline int make_sock(SOCKET* sock) { return make_sock_tcp4(sock); }; @@ -220,6 +222,21 @@ RES: return res; } +static inline int sock_bind(SOCKET sock, const char* server_ip, unsigned short port) { + int res = ERR_SOCK_SUCCESS; + char buf[8] = { 0 }; + sprintf(buf, "%u", port); + struct addrinfo inf = { 0 }; + struct addrinfo* resaddr = NULL; + res = _getaddrinfo(&inf, &resaddr, server_ip, buf); + if (res != ERR_SOCK_SUCCESS) goto RES; + res = _bind(sock, resaddr); + if (res != ERR_SOCK_SUCCESS) goto RES; +RES: + if (resaddr) freeaddrinfo(resaddr); + return res; +} + static inline int sock_bindlisten(SOCKET sock, const char* server_ip, unsigned short port) { int res = ERR_SOCK_SUCCESS; char buf[8] = { 0 }; @@ -307,6 +324,7 @@ static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, unsig res = _bindlisten(*sock, *resaddr); if (res != ERR_SOCK_SUCCESS) return res; + return res; } static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsigned short port, @@ -336,7 +354,9 @@ static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsi res = _connect(*sock, *resaddr); if (res != ERR_SOCK_SUCCESS) return res; } + return res; } + static inline void close_sock(SOCKET sock) { if(sock == -1) return; #if WIN_PART