#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    41
    Rep Power
    12

    network programming with C, help!!!


    Hi there,

    I have just started doing network programming with C, thus, am still very shallow in that topic.

    However, for a TCP connection, I do know that we have (should) do the following procedures:
    1. socket();
    2. bind();
    3. listen();
    4. accept();
    5. send(), recv(), write() or read(); etc...

    My problem is, I would like my server to do what telnet does when the client types the following message from the client machine:

    GET /my_web_server.html HTTP/1.0

    The server will respond with the html codes of the page.

    I dont see how it works. Currently I only know how to send a message to the server from client, and the server will send back an identical message back to the client. Thus, not much dynamic interactions.

    By any chance, could it be by default if we use port 80 (web-server port) as the server, with IP address "my_web_server", the server will AUTOMATICALLY send the html codes back to the client. But how????

    I hope you understand what I mean.

    Any help will be appriciated.

    GeekoidXp.
    p/s: I am running Linux btw.
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    no, you cannot just use port 80 and expect magic to happen :)
    if you want the server to send u back webpages, then you will have to code the server to do that. we did exactly this in my CS class about a month ago. basically, there are a few steps to take:
    -you must parse the clients HTTP request and extract the file that they want to view
    -you must then open this file in your code, read the HTML into a buffer, and send that code back to the client
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    41
    Rep Power
    12
    hhaha....I thought I had to do something more.

    I don't quite get what you mean.
    Are you saying that the server has to read the message sent by the client --> msg="get /my_web_server.html http:/1.0"

    Then the server see the get command in the message, thus determine which webpage to get, thus, since it says my_web_server.html, thus, the server passes the client the my_web_server.html codes......

    If you could help me out a bit it would be great!

    Thanks!

    GeekoidXP
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    yes you got it precisely. the client will send a HTTP request, on the server side you will recieve that request, and then lets say for example your server will use '/' as a delimiter and extract the text after '/' to use for the filename. in your above example, the msg "get /my_web_server.html http/1.0" would have the server get the file named my_web_server.html. you would have to open this file and read the text into a local buffer. after filling up the buffer, you will send this back to the client. does that help? i am often not too great at explaining stuff and if that doesnt clarify at all then there will be much smarter peeps than me on tomorrow :)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    41
    Rep Power
    12
    Yup!!! got it, thanks...!!!

    Hm, that leaves me wondering if I could also use socket programming in C to access database just like how perl and PHP can.

    Geekoid
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    Originally posted by Geekoidxp
    Yup!!! got it, thanks...!!!

    Hm, that leaves me wondering if I could also use socket programming in C to access database just like how perl and PHP can.

    Geekoid
    Yes, you can ... to SUPPORT remote database access.

    In your reading, you should have encountered the OSI networking model, a stack with seven layers, each of which performs specific functions in a network connection. TCP/IP uses a similar layered model that is a simplified form the the OSI.

    Sockets programming works in the middle, in the Transport and IP layers. With sockets programming, you handle addressing, create the connection, and read and write data back and forth -- and that's about all that you do with sockets. Data is data, bytes are bytes, and sockets doesn't care one bit what is in the data nor what it will be used for; sockets just sends and receives the data. It's up to the Application Layer to know what that data is for and how to use it.

    Think of it like file I/O. You write your data to a buffer and then write that buffer to disk. The file system doesn't know nor does it care what's in that buffer; all it cares about is how many bytes and where you want to write it to. Same thing when the file system reads a block of data from a file; all it cares about is how many bytes, where in which file it's to start reading the data from, and where the buffer is to write it to. It's your application that knows what that data is for and how to use it.

    So, sockets will do the vital work of making the connection and reading and writing the data (which can include application commands), but it's up to the application to know how to be a telnet client, an HTTP client (eg, a browser), a database client, etc, and to handle everything the way that that kind of client needs to.

    Does that help to get the right perspective?
    Last edited by dwise1_aol; May 29th, 2003 at 09:45 AM.

IMN logo majestic logo threadwatch logo seochat tools logo