Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get requested address in socket programming with C

I am using something like this to create a server using C. When I go to 127.0.0.1:5000 from my browser I can see "Hello Worlds" as I am sending it as buffer. But I want 127.0.0.1:5000/filename.html to work. But I don't know how to get filename that comes after 127.0.0.1:5000 in C.

I am using this to make connection:

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(5000);

  bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));

  connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
like image 557
sadaf2605 Avatar asked Jul 25 '13 06:07

sadaf2605


People also ask

Can socket programming be done in C?

It extracts the first connection request on the queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. At this point, the connection is established between client and server, and they are ready to transfer data.

How do I print a socket address?

STEP 1: Start the program. STEP 2: Declare the variables and structure for the socket. STEP 3: The socket is binded at the specified port. STEP 4: Using the object the port and address are declared.

What does socket () do in C?

The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The socket() function takes the following arguments: domain. Specifies the communications domain in which a socket is to be created.

Can sockets send and receive?

A socket is the interface between your application and the outside world: through a socket, you can send and receive data. Therefore, any network program will most likely have to deal with sockets, they are the central element of network communication.


1 Answers

The browser will be sending your server an HTTP request that contains the URL it is after. The request could look like this:

GET /filename.html HTTP/1.1
Host: 127.0.0.1:5000

Your C program must read this request from the socket and parse it to find the URL. Note that the request will likely contain more information than the above, but it should always end with a blank line (so you know where to stop parsing). Lines in HTTP requests should end with both a carriage return and line feed ("\r\n").

You receive data through the same socket that you use to send data. The steps to read an HTTP request could be something like this:

  1. Declare a buffer of a sufficient size, perhaps 4096 bytes or more.

  2. Read data into this buffer using read and your connfd until:

    1. You have received 4095 bytes (in which case your server should respond with error 413)

    2. You have encountered the characters "\r\n\r\n" (this indicates a blank line)

    3. Some amount of time has passed and neither of the above has occurred. In order to implement a timeout you will need to use select() or poll().

  3. Once you have received the HTTP request into your buffer, parse it:

    1. The first line is the request line which dictates the method of the request, the URI, and the protocol version number. A possible way to parse this line is to split it by space.

    2. Subsequent lines represent HTTP header fields, and can generally be parsed as Key: Value\r\n. These header fields contain cookies, information about the client making the request, etc.

  4. You need to form your HTTP response as well. A response for when the URI specifies a valid resource (such as filename.html) might be:

    HTTP/1.1 200 OK
    Date: Thu, 25 Jul 2013 03:55:00 GMT
    Server: sadaf2605-server/1.0
    Content-Type: text/html
    Content-Length: 40595
    
    < contents of filename.html follows here >
    

    In the above, Content-Length refers to the number of bytes in the filename.html file. Just like the request, a response is separated from data using a blank line.

like image 187
dreamlax Avatar answered Sep 29 '22 07:09

dreamlax