From f46bd2855104c1b97873971a637a57b213aa8683 Mon Sep 17 00:00:00 2001 From: microcai Date: Tue, 17 Dec 2024 09:45:57 +0800 Subject: [PATCH] =?UTF-8?q?C=20=E4=BB=A3=E7=A0=81=E5=B0=B1=E6=98=AF?= =?UTF-8?q?=E8=BF=99=E6=A0=B7=E4=B9=B1=E7=94=A8=E5=85=A8=E5=B1=80=E5=8F=98?= =?UTF-8?q?=E9=87=8F=EF=BC=8C=E9=A3=8E=E6=A0=BC=E5=BE=88=E5=B7=AE=E5=BE=88?= =?UTF-8?q?=E5=B7=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/echo_server/echo_server_stackfull.c | 26 ++++++++++----------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/example/echo_server/echo_server_stackfull.c b/example/echo_server/echo_server_stackfull.c index a930547..02e5a31 100644 --- a/example/echo_server/echo_server_stackfull.c +++ b/example/echo_server/echo_server_stackfull.c @@ -21,23 +21,19 @@ enum // configuration // the completion port static HANDLE cpl_port; -// the listening socket -static SOCKET listener; - -// ----------------------------------------------------------------------------- // prototypes - main functions static void init(void); // prototypes - helper functions -static void bind_listening_socket(void); +static void bind_listening_socket(SOCKET); static SOCKET create_accepting_socket(void); static void create_io_completion_port(void); -static void create_listening_socket(void); +static SOCKET create_listening_socket(void); static BOOL get_completion_status(DWORD*, ULONG_PTR*, OVERLAPPED**); static void init_winsock(void); static void prepare_endpoint(struct sockaddr_in*, u_long, u_short); -static void start_listening(void); +static void start_listening(SOCKET); // ----------------------------------------------------------------------------- @@ -50,7 +46,7 @@ int main(void) // ----------------------------------------------------------------------------- -static void bind_listening_socket(void) +static void bind_listening_socket(SOCKET listener) { struct sockaddr_in sin; @@ -92,9 +88,9 @@ static void create_io_completion_port(void) // ----------------------------------------------------------------------------- -static void create_listening_socket(void) +static SOCKET create_listening_socket(void) { - listener = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0 , 0, WSA_FLAG_OVERLAPPED); + SOCKET listener = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0 , 0, WSA_FLAG_OVERLAPPED); if (listener == INVALID_SOCKET) { printf("* error creating listening socket!\n"); @@ -107,6 +103,7 @@ static void create_listening_socket(void) printf("* error %d in listener\n", err); exit(1); } + return listener; } static void echo_sever_client_session(void* param) @@ -172,6 +169,7 @@ static void echo_sever_client_session(void* param) static void accept_coroutine(void* param) { char addr_buf[1024]; + SOCKET listener = (SOCKET)param; for (;;) { @@ -206,9 +204,9 @@ static void init(void) { init_winsock(); create_io_completion_port(); - create_listening_socket(); - bind_listening_socket(); - start_listening(); + SOCKET listener = create_listening_socket(); + bind_listening_socket(listener); + start_listening(listener); #ifdef _WIN32 ConvertThreadToFiber(0); @@ -243,7 +241,7 @@ static void prepare_endpoint(struct sockaddr_in* sin, u_long address, sin->sin_port = htons(port); } -static void start_listening(void) +static void start_listening(SOCKET listener) { if (listen(SOCKET_get_fd(listener), 100) == SOCKET_ERROR) {