views:

2673

answers:

9

I am relatively new to the C language, but I'm a very fast learner. I'm looking to do some basic network programming in C but I'm not sure how to start. Could anyone point me to a good guide or tutorial for C Network Programming?

+5  A: 

This was what I used.

C Hogg
+2  A: 

Do you want to program in *nix or windows? AFAIK there are some subtle differences between the *nix and windows implementations of sockets (which you'll probably need to use).

Anyway, for winsock programming I found this FAQ VERY useful. It's more of a manual than a FAQ imo.

Erik van Brakel
+17  A: 

Berkeley Sockets

This is a reasonable guide to Berkeley sockets, which is pretty much what every OS uses for the network API.

I learned using "Unix Network Programming, Volume 1: The Sockets Networking API" which is an excellent book that not only shows you how to use it, but what happens when you use it, and why you might choose one usage over another.

Look up "C TCP/IP example" for some example programs to whet your whistle. Also pay attention to your particular compiler's libraries - there are slight differences in how a the Berkeley API is implemented, which might bite you.

Adam Davis
Very good article. I went searching for online guides for C a couple of months ago, but never came across it. It is definitely one for my bookmarks!
kaybenleroll
or it might byte him...
micmoo
+1  A: 

Googling for "C sockets" yields a few tutorials that seem OK, such as these:

http://www.linuxhowtos.org/C_C++/socket.htm
http://beej.us/guide/bgnet/

Only a couple of functions are really necessary for your first networking program. Once you know what they are (WSAStartup first on Win32, socket to create a socket, bind/listen/accept or connect and close, etc.; see the tutorials) you can look them up on your platform reference (i.e. MSDN or man pages)

And download a copy of Netcat, "The Swiss Army Knife of TCP/IP," a telnet-like program that will help you a lot with your testing and debugging.

aib
+1  A: 

I found Linux Socket Programming by Example a very helpful read when I was learning. Depending on the OS you're using you may also find it a good read.

OJ
+1  A: 

If you're doing Windows sockets programming, make sure you pay careful attention to The Windows Sockets Lame List. It kept me from making stupid mistakes many times.

Ferruccio
A: 

You could look at a project like libcurl and first use it and then look at how they implemented their stuff.

/Johan

Johan
A: 

for Unix/Linux, I would recommend unix network programming by Richard Stevens.

neesh
A: 

I've worked in networking programming for the last 6 years.

The best start is with Beginning Linux Programming-WROX-Neil Mathew and Richard Stones,

chapter 15 I think,...and Richard Stevens's books will be bibles for you. A set of 7 books:

  • Advanced programming in Unix environment,
  • Unix net work programming volume 1/2/3 (vol 1 is enough for you),
  • TCP/IP illustrated volume 1/2/3, and
  • Socket programing by example.

These are the best books according to my experience.

Narendra Allam