r/C_Programming • u/wfcl • Aug 10 '24
Question Any good learning resources for C sockets?
I have an idea of a simple ascii-driven multiplayer card game to make as my first ever C project (my first ever programming project in general). I want to use POSIX sockets for multiplayer to understand at least a bit how they work. Can you guys recommend any learning resources? Preferably something to read, thx in advance :)
29
Upvotes
15
u/JamesTKerman Aug 10 '24
I'd never touched Unix sockets before this week. Using Beej's guide to IPC I was able to successfully incorporate them into a daemon project I'm working on in less than an hour between reading the guide and coding in my project. The basic usage is pretty simple and Beej does a great job of explaining it, including potential pitfalls.
59
u/[deleted] Aug 10 '24
Beej's Networking Guide
24
u/raevnos Aug 10 '24
W. Richard Steven's classic book Unix Network Programming.
3
u/veghead Aug 10 '24
I learned from man pages; don't do that.
2
u/TPIRocks Aug 10 '24
I hate to say this, but I just now asked ChatGPT to write a standard C program, using network sockets, to open a connection to an ftp server and give me a list of files in the root directory, then close the connection. It did a clean job and then explained some things without being prodded. I assume I could ask it to elaborate, on any aspect of the generated code, and get good information.
I didn't test the code, but it looked correct. It seems to be especially good at expressing itself through code generation. I think it's kinda neat, programming languages all have an underlying grammar, therefore ChatGPT is really good at generating code without syntax errors.
I'm not a fan of blindly going along with whatever it spits out, turning language into logic is sometimes a struggle for it, or at least it was when I really played with it makes months ago. I decided to see how it could do, and this looks good enough to learn the basic protocols of using sockets.
1
u/Still_Competition_24 Aug 10 '24 edited Aug 10 '24
Just look up pretty much any tcp/udp client/server example (depending on your usecase), look up pretty much any select/poll example. Check man pages when unsure about function usage. There really isn't anything magical about them.
Only real catch is to remember TCP is stream and there is no guarantee on how many chunks your message will arrive in.
1
u/Cylian91460 Aug 10 '24
man IPv6 and man IP has pretty much everything
1
u/CaitaXD Aug 11 '24
Any portable wrappers?
ifdef WIN32 is painful
0
u/wfcl Aug 10 '24
Thx for pointing out, my bad, not UNIX sockets, but POSIX sockets (at least that's how the implementation of Berkeley sockets found in linux is called on wikipedia). Got these two mixed up somehow
-1
u/penny_stacker Aug 10 '24
Look up some open source networking libraries and check out the code. StackOverflow had a list of the top ones.