@@ -131,6 +131,18 @@ int main(int argc, char *argv[]) {
131
131
X509_free (cert );
132
132
133
133
134
+ //Set to non-blocking. This is needed to for the SSL functions.
135
+ //Without this, SSL_read() might block.
136
+ #if defined(_WIN32 )
137
+ unsigned long nonblock = 1 ;
138
+ ioctlsocket (socket_peer , FIONBIO , & nonblock );
139
+ #else
140
+ int flags ;
141
+ flags = fcntl (socket_peer , F_GETFL , 0 );
142
+ fcntl (socket_peer , F_SETFL , flags | O_NONBLOCK );
143
+ #endif
144
+
145
+
134
146
135
147
printf ("Connected.\n" );
136
148
printf ("To send data, enter text followed by enter.\n" );
@@ -157,11 +169,18 @@ int main(int argc, char *argv[]) {
157
169
char read [4096 ];
158
170
int bytes_received = SSL_read (ssl , read , 4096 );
159
171
if (bytes_received < 1 ) {
160
- printf ("Connection closed by peer.\n" );
161
- break ;
172
+ int err ;
173
+ if ((err = SSL_get_error (ssl , bytes_received )) &&
174
+ (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE )) {
175
+ //Just waiting on SSL, nothing to do.
176
+ } else {
177
+ printf ("Connection closed by peer.\n" );
178
+ break ;
179
+ }
180
+ } else {
181
+ printf ("Received (%d bytes): %.*s" ,
182
+ bytes_received , bytes_received , read );
162
183
}
163
- printf ("Received (%d bytes): %.*s" ,
164
- bytes_received , bytes_received , read );
165
184
}
166
185
167
186
#if defined(_WIN32 )
0 commit comments