r/learnprogramming • • May 27 '11

Help with getting into network programming? [C]

I recently started gaining an interest in network programming while using C. I was given an assignment by a mentor to write a program that would use an HTTP GET request on HTTP 1.0.

Let me make this clear, I am NOT asking for direct code to use. In fact, if you gave working code, I would down vote you. What I do want is articles, tutorials, general information on the subject.

I don't know much about socket programming, but if someone could tell me the concept of making an HTTP GET request I would be thankful.

I am working on Ubuntu compiling with gcc, and I was told to use the Berkeley headers.

Which functions should I look into using for performing the HTTP GET request?

3 Upvotes

6 comments sorted by

8

u/defrost May 27 '11

2

u/GuanoTail May 27 '11

Thank you very much. I will look at these as soon as possible.

1

u/sausagefeet May 27 '11 edited May 27 '11

Haha. I read level exercises and wondered if you Just always suggest a healthy lifestyle. And ##c misses you defrost.

EDIT: kegel not level, stupid phone.

0

u/[deleted] May 27 '11

[deleted] — view deleted comment

1

u/defrost May 27 '11

oh. my. well, I never.

3

u/dmazzoni May 27 '11

You basically need to learn two things:

  1. How to open a low-level socket from one computer to another and send data back and forth - I'd do a Google search for Unix network programming. There are a lot of steps you need to do just to set up the connection, and then you need to learn how to properly send or receive only as many bytes as the socket can handle at a time. The end result will be only 50 lines of code, but it's tricky at first!
  2. How to use the HTTP 1.0 protocol to request a webpage and download the result. This is extremely simple compared to the first part. HTTP has many complexities if you're writing a full web browser or implementing your own web server or cache, but if you just want to retrieve a simple webpage given its URL, you can learn HTTP GET in like 5 minutes...assuming you've finished part 1.

1

u/GuanoTail May 27 '11

Do you have any recommendations on tutorials/links?

2

u/miyakohouou May 27 '11

defrost's comment has you covered for socket related code. For the HTTP part, if you want in-depth documentation, check out the RFCs for HTTP/1.0 and HTTP/1.1. They are good to read, and will give you an overview of the protocol. More practically, you might also want to write a simple html page that submits a form via a get request, then use wireshark or tcpdump to watch the traffic so you can see how the requests are actually implemented.