@@ -46,14 +46,16 @@ run_as_server(void *arg)
4646 pthread_mutex_lock (& lock );
4747 sock = socket (AF_INET , SOCK_STREAM , 0 );
4848 if (sock < 0 ) {
49+ pthread_mutex_unlock (& lock );
4950 perror ("Create a socket failed" );
50- goto RETURN ;
51+ return NULL ;
5152 }
5253
5354#ifndef __wasi__
5455 if (setsockopt (sock , SOL_SOCKET , SO_REUSEADDR , (char * )& on , sizeof (on ))) {
56+ pthread_mutex_unlock (& lock );
5557 perror ("Setsockopt failed" );
56- goto RETURN ;
58+ goto fail1 ;
5759 }
5860#endif
5961
@@ -64,13 +66,15 @@ run_as_server(void *arg)
6466
6567 addrlen = sizeof (addr );
6668 if (bind (sock , (struct sockaddr * )& addr , addrlen ) < 0 ) {
69+ pthread_mutex_unlock (& lock );
6770 perror ("Bind failed" );
68- goto UNLOCK_SHUTDOWN ;
71+ goto fail1 ;
6972 }
7073
7174 if (listen (sock , 0 ) < 0 ) {
75+ pthread_mutex_unlock (& lock );
7276 perror ("Listen failed" );
73- goto UNLOCK_SHUTDOWN ;
77+ goto fail1 ;
7478 }
7579
7680 server_is_ready = true;
@@ -82,25 +86,22 @@ run_as_server(void *arg)
8286 new_sock = accept (sock , (struct sockaddr * )& addr , (socklen_t * )& addrlen );
8387 if (new_sock < 0 ) {
8488 perror ("Accept failed" );
85- goto SHUTDOWN ;
89+ goto fail1 ;
8690 }
8791
8892 printf ("Start sending. \n" );
8993 send_len = sendmsg (new_sock , & msg , 0 );
9094 if (send_len < 0 ) {
9195 perror ("Sendmsg failed" );
92- goto SHUTDOWN ;
96+ goto fail2 ;
9397 }
9498 printf ("Send %ld bytes successfully!\n" , send_len );
9599
96- SHUTDOWN :
100+ fail2 :
101+ close (new_sock );
102+ fail1 :
97103 shutdown (sock , SHUT_RD );
98- return NULL ;
99-
100- UNLOCK_SHUTDOWN :
101- shutdown (sock , SHUT_RD );
102- RETURN :
103- pthread_mutex_unlock (& lock );
104+ close (sock );
104105 return NULL ;
105106}
106107
@@ -125,7 +126,7 @@ run_as_client(void *arg)
125126 sock = socket (AF_INET , SOCK_STREAM , 0 );
126127 if (sock < 0 ) {
127128 perror ("Create a socket failed" );
128- goto RETURN ;
129+ return NULL ;
129130 }
130131
131132 /* 127.0.0.1:1234 */
@@ -135,14 +136,14 @@ run_as_client(void *arg)
135136
136137 if (connect (sock , (struct sockaddr * )& addr , sizeof (addr )) < 0 ) {
137138 perror ("Connect failed" );
138- goto UNLOCK_SHUTDOWN ;
139+ goto fail ;
139140 }
140141
141142 printf ("Start receiving. \n" );
142143 recv_len = recvmsg (sock , & msg , 0 );
143144 if (recv_len < 0 ) {
144145 perror ("Recvmsg failed" );
145- goto SHUTDOWN ;
146+ goto fail ;
146147 }
147148
148149 printf ("Receive %ld bytes successlly!\n" , recv_len );
@@ -155,14 +156,9 @@ run_as_client(void *arg)
155156 s += strlen (s ) + 1 ;
156157 }
157158
158- SHUTDOWN :
159- shutdown (sock , SHUT_RD );
160- return NULL ;
161-
162- UNLOCK_SHUTDOWN :
159+ fail :
163160 shutdown (sock , SHUT_RD );
164- RETURN :
165- pthread_mutex_unlock (& lock );
161+ close (sock );
166162 return NULL ;
167163}
168164
0 commit comments