codeplea/Hands-On-Network-Programming-with-C

Using perror for better error messages

Closed this issue · 2 comments

Really, really enjoying your book. Big thanks!

However, doing this would have helped me as port 8080 is in use on my system:

fprintf(stderr, "bind() failed. (%d)\n", GETSOCKETERRNO());

making it or adding another line:

git diff
diff --git a/chap02/time_server.c b/chap02/time_server.c
index b3456cb..50c13f5 100644
--- a/chap02/time_server.c
+++ b/chap02/time_server.c
@@ -97,6 +97,7 @@ int main() {
     if (bind(socket_listen,
                 bind_address->ai_addr, bind_address->ai_addrlen)) {
         fprintf(stderr, "bind() failed. (%d)\n", GETSOCKETERRNO());
+        perror("bind() failed");
         return 1;
     }
     freeaddrinfo(bind_address);

which then helps me with:

./time_server 
Configuring local address...
Creating socket...
Binding socket to local address...
bind() failed. (98)
bind() failed: Address already in use

Thanks.

I'm glad you like the book!

There is some info in the book about error handling in chapter 13, including how to get a text description. For one-off example programs, I figured it was easier to keep the program shorter and any error codes could be searched for online.

I don't use the perror() function directly because it is not available on Windows. If I do a second edition, I think I'll talk about error handling much sooner in the book and define some cross-platform perror()-like equivalents. I understand it can be very frustrating when something doesn't work when there isn't a very good indication as to why. In your case, I'm glad you were able to figure out that 8080 was already in use!

Thanks for the feedback.

Thanks Lewis. A 2nd edition would be amazing! Closing for now.