canl_get_error_message(canl_ctx cc)
{
glb_ctx *ctx = (glb_ctx*) cc;
+ int ret;
+ char *msg = NULL;
- if (ctx == NULL || ctx->err_msg == NULL)
- return "No error available";
+ if (ctx == NULL)
+ return "Context is not initialized";
+
+ ret = canl_get_error(ctx, &msg);
+ if (ret)
+ return "No human-error available";
+ ctx->err_msg = msg;
return ctx->err_msg;
}
case 'h':
fprintf(stderr, "Usage: %s [-p port]"
"[-s server] [-h] \n", argv[0]);
- break;
+ exit(0);
case 'p':
port = atoi(optarg);
break;
err = canl_io_connect(my_ctx, my_io_h, p_server, port, 0, NULL, &timeout);
if (err) {
- printf("[CLIENT] connection cannot be established\n");
+ printf("[CLIENT] connection to %s cannot be established: %s\n",
+ p_server, canl_get_error_message(my_ctx));
goto end;
}
else {
my_io_h = NULL;
end:
- canl_get_error(my_ctx, &err_msg);
- if (err_msg != NULL)
- printf("%s\n", err_msg);
-
canl_free_ctx(my_ctx);
return err;
case 'h':
fprintf(stderr, "Usage: %s [-p port] [-c certificate]"
" [-k private key] [-h] \n", argv[0]);
- break;
+ exit(0);
case 'p':
port = atoi(optarg);
break;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
err = errno;
- freeaddrinfo(servinfo); // all done with this structure
- return -1;
+ continue;
}
if ((err = bind(sockfd, p->ai_addr, p->ai_addrlen))) {
- close(sockfd);
err = errno;
+ close(sockfd);
continue;
}
if ((err = listen(sockfd, BACKLOG))) {
continue;
}
-
break;
}
freeaddrinfo(servinfo); // all done with this structure
if (p == NULL) {
- printf("Failed to acquire a server socket");
+ /* Beware that only the last error is displayed here ... */
+ printf("Failed to acquire a server socket: %s\n",
+ strerror(err));
return 1;
}
}
err = canl_io_read (my_ctx, my_io_h, buf, sizeof(buf)-1, NULL);
- if (err > 0) {
- buf[err] = '\0';
- printf ("[SERVER] received: %s\n", buf);
- }
- else
- printf("[SERVER] Failed to receive reply from client: %s\n",
+ if (err <= 0) {
+ printf("[SERVER] Failed to receive reply from client: %s\n",
canl_get_error_message(my_ctx));
-
-end:
- print_error_from_canl(my_ctx);
-
- if (my_io_h) {
- err = canl_io_close(my_ctx, my_io_h);
- if (err){
- printf("[SERVER] Cannot close connection\n");
- print_error_from_canl(my_ctx);
- }
+ goto end;
}
- if (my_io_h) {
+ buf[err] = '\0';
+ printf ("[SERVER] received: %s\n", buf);
+ err = 0;
+
+end:
+ if (my_io_h)
err = canl_io_destroy(my_ctx, my_io_h);
- if (err){
- printf("[SERVER] Cannot destroy connection\n");
- print_error_from_canl(my_ctx);
- }
- my_io_h = NULL;
- }
canl_free_ctx(my_ctx);