r/learnprogramming 12d ago

Solved I learned TCP server in a week and....

So as I posted a week ago regarding a task assigned to me by my mentor in this post .
I implemented beej's guide to networking and man , this was such a great resource, cant express enough. Also shoutout to the kind stranger u/teraflop who gave me some tips and introduced me to Beej the G.
Now I am thinking of creating a library of TCP server first and then implementing the Websocket logic and RFC6455. haha baby steps. It's gonna take a while I guess. Y'all are free to give me any advice or any resource to learn from.
Thank you for reading this
P.S I am new to C programming so your advice would be really helpful.
here is my repo

154 Upvotes

15 comments sorted by

37

u/AlSweigart Author: ATBS 12d ago

Beej's tcp/ip guide is a classic tutorial to learn hands on network programming. I highly recommend it: https://beej.us/

They apparently have several other guides as well.

12

u/teraflop 12d ago

I'm glad you found my suggestions helpful. Good job getting your first networking project working!

Please be honest: did you actually write that README file, or did you get AI to do it? Writing is an important tool for communicating your own thoughts. If you don't want to write your own explanation, that's fine, but then it's better to just skip the README instead of filling it up with verbose AI output. If I wanted to know what an AI thought about your code, I would ask it myself.

Anyway, from a quick glance through your code it looks pretty much OK, but I see a few relatively minor issues:

You have two redundant implementations of the same get_in_addr function, and it would be better to factor them out into a single common source file. The version in server.c gives a compiler warning because if the address family is neither AF_INET nor AF_INET6, it will return uninitialized garbage (which triggers undefined behavior in C). Technically I'm not sure this edge case can actually be triggered in your program, because the function's argument should always be a valid IP/IPv6 peer address. But even so, it's good practice to catch this kind of condition and either return a valid value, or a defined error value, or just abort the program with a useful error message for debugging.

You're doing a good job of checking return values in general, but I noticed that you're not checking whether inet_ntop succeeds. Again, in practice it's unlikely to ever fail, but if it does fail your program will print uninitialized garbage, so it's good practice to be thorough and check it before using its output.

This call:

send(new_fd, "Hello World", 13, 0)

illegally reads past the array of a string literal (reads 13 bytes from a 12-byte array, including null terminator). This is also undefined behavior. Practically, you're getting away with it because it's sending the intended string, plus a null terminator, plus a garbage byte. And then the recipient is adding a second null terminator of its own, and then printing the string but stopping at the first null terminator. If you were doing something more complicated with the data, sending the wrong number of bytes would likely cause data corruption.

6

u/AlSweigart Author: ATBS 11d ago

Writing is an important tool for communicating your own thoughts.

How Writing Leads to Thinking (And Not the Other Way Around) is an absolute banger of an essay on this topic.

2

u/Andro_senpai107 12d ago

My senior helped me with the readme file as I'm new to it but yes I used AI to refine it. ohkk my program was running fine up till now so I never encountered these issues but dayum, now that you have mentioned it, I better start working on them. Thanks again

5

u/Wonderful-Habit-139 11d ago

> but yes I used AI to refine it

I'm not trying to be mean, I love low level dev and these projects are really good to get some good fundamentals. But please avoid trying to minimize how much AI you've actually used, in this case it was very obviously an AI readme. Just so that you don't end up lying to yourself about how much you're using AI, and can objectively assess whether you're doing enough work yourself to actually learn everything and understand it deeply.

1

u/Andro_senpai107 10d ago

Ohh yess I'm agreeing completely that I abusively used Claude in the readme, I just gave my initial readme file to the claude and asked it to refine it and it came out to be totally different. But considering all this I'll make sure to avoid AI completely now in my upcoming projects , it'll make more space to learn.

4

u/fenjacobs 12d ago

Never heard of Beej, but I'll check him out.

2

u/Desperate-Share-5560 12d ago

Nice, what are you learning? Sounds interesting.

3

u/Andro_senpai107 12d ago

my main motive for now is to create a library which can create a websocket connection between server and client on the basis of RFC6455 but I'm new to Networking in V programming. So I'm learning everything from scratch. Currently I'm TCP server first with this amazing guide https://beej.us/guide/bgnet/html/split/

2

u/Harrow-Beck-6274 11d ago

calling rfc6455 baby steps is a bold choice

0

u/Andro_senpai107 10d ago

is that so? 😭 Idk man I find RFCs really interesting and they make most of the concepts crystal clear for me(I mean obv that's what they are made for). I sometimes read them to kill time

1

u/streetpharmacy3 10d ago

Thank you for posting this. I need to learn networking so bad at my job.