Hi,
You need proper error handling. There are functions such as perror() and strerror() which can help in diagnosis. More information using the man pages:
Or get the man pages online, if you're not on Linux.
I learnt network programming from scratch using
https://beej.us/guide/bgnet/. There are good examples in there.
All the best.