From 147c0951054b7431f96f873a2fe36c160ddc86bd Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Thu, 29 Sep 2016 13:49:21 +0200 Subject: [PATCH] Report errors in the netcat example PUBLISHED_FROM=f43dc48512ff13028a57571b537c41d9658034b5 --- examples/netcat/nc.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/netcat/nc.c b/examples/netcat/nc.c index 64800c4d..5040becc 100644 --- a/examples/netcat/nc.c +++ b/examples/netcat/nc.c @@ -105,7 +105,7 @@ int main(int argc, char *argv[]) { int i, is_listening = 0; const char *address = NULL; struct mg_connection *c; - // struct mg_bind_opts = {}; + const char *err = NULL; mg_mgr_init(&mgr, NULL); @@ -133,13 +133,21 @@ int main(int argc, char *argv[]) { signal(SIGPIPE, SIG_IGN); if (is_listening) { - if ((c = mg_bind(&mgr, address, ev_handler)) == NULL) { - fprintf(stderr, "mg_bind(%s) failed\n", address); + struct mg_bind_opts opts; + memset(&opts, 0, sizeof(opts)); + opts.error_string = &err; + if ((c = mg_bind_opt(&mgr, address, ev_handler, opts)) == NULL) { + fprintf(stderr, "mg_bind(%s) failed: %s\n", address, err); + exit(EXIT_FAILURE); + } + } else { + struct mg_connect_opts opts; + memset(&opts, 0, sizeof(opts)); + opts.error_string = &err; + if ((c = mg_connect_opt(&mgr, address, ev_handler, opts)) == NULL) { + fprintf(stderr, "mg_connect(%s) failed: %s\n", address, err); exit(EXIT_FAILURE); } - } else if ((c = mg_connect(&mgr, address, ev_handler)) == NULL) { - fprintf(stderr, "mg_connect(%s) failed\n", address); - exit(EXIT_FAILURE); } if (s_is_websocket) { mg_set_protocol_http_websocket(c);