Thread: Server in C

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Server in C

    While writing my HTTP server in C#, I've had concerns in the back of my mind about the performance issues associated with .NET, so I've decided to take a serious look at remaking the whole thing in C.

    At the W3 Consortium's web site I found an open-source project in C for all sorts of HTTP applications, but the layout of that website is REEALLY hard to navigate, so I thought it would be more efficient to fish for advice here (as a google search involving "C" yields highly irrelevant results).

    Can I get some advice regarding socket APIs in C?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Can I get some advice regarding socket APIs in C?
    I would suggest just using native raw sockets. With a few well placed preprocessor statements, it's easy to create socket code that is portable between *nix and windows platforms (assuming you want your webserver to be portable).


    To be honest, programming the networking code should be the least of your worries when creating a webserver.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Oh really? I don't suppose you'd know of a tutorial that would teach you how to go about doing that? I guess it's just working with streams, really, but I never would've thought it would be that easy...

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well the best tutorial I've ever seen on socket programming is Beej's Guide. The tutorial is for unix platforms, but converting it to windows usable code is trivial. If you would like some tips on how to make your socket code portable, I can help you with that.

    Your webserver will most likely follow this sort of setup:

    Code:
    MainThread
      Initialize sockets
      Bind to port 80, and start listening for connections
    mainloop:
      Block while waiting for connection
      Accept new connection
      Spawn a new ClientThread, passing it the socket handle
      goto mainloop:
    
    ClientThread
      read in http request from client
      parse request and validate http headers
      use HTTP RFC to determine how to handle the client's request
      Send client the requested data
      close socket connection
      return from thread
    Most of the work in the above layout is in the part that reads:
    parse request and validate http headers
    use HTTP RFC to determine how to handle the client's request
    That part of your code will be independent on networking, it's just a matter of following the http protocol. Unfortunately the protocol is rather extensive though.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Beej's Guide looks great - thanks, bud.

    As I'm reading this, I'm a little confused. It keeps making references to "sys/socket.h" and stuff like that. Where do I get these additional OS-specific libraries? It didn't talk much about them, it seemed almost as if they were part of the standard. I'm also an almost exclusively Windows user, so I'd like to get my hands on some of the Windows specifics on the subject, and then port to *nix once I've got that working.

    Another problem I have is my obsession with using the standard things... I buy generic brands ALL THE TIME. Is what I'm learning in Beej's guide pretty much the same as winsock, and is winsock like the official socket-API for Windows?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    While UNIX BSD sockets require a couple different headers, windows just requires one.
    Code:
    #include <winsock2.h>
    If you are on an older system which doesnt have the winsock2 libraries installed, then you can just include winsock.h

    The only difference really to using sockets in windows, is that windows requires you to make a call to WSAStartup() before you call any other winsock function.

    A list of all winsock functions can be found here. As you can see, there is a winsock equivilant to every function you see in Beej's guide.

    One last thing to note is that you need to link to the winsock or winsock2 library before you can use the winsock functions. This is done just as you would link to any other library. I believe the library names are wsock32.lib if you are using winsock 1.1, or ws2_32.lib if you are using winsock2.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Oh, I forgot the one other major difference between windows and unix sockets, and that is the error codes. For instance:

    If a call to accept() fails on windows, GetLastError() returns the error code. Winsock error codes can be found here.

    The unix version of accept() on the other hand has different return errors.

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by sean_mackrory
    Oh really? I don't suppose you'd know of a tutorial that would teach you how to go about doing that? I guess it's just working with streams, really, but I never would've thought it would be that easy...
    Well when you think about it, all a server does is sit there and waits for you to send it requests via HTTP. it then gets your requests and sends it back to the caller alonga with a few environment variables. if i'm correct the request are sent back as file streams so you can manipulate them like thus. In the case of a browser- since this is the client one normally uses to connect to a server - all it really cares about is the MIME-TYPE, if its text/html, the HTML engine takes care of the markup, if is not, it just passes the stream to a plug in.


    BTW are you talking about libwww?

    [edit] as bithub said beej's tutorial is unix specific and even though there are minor differences i've included the following links for winsock

    http://tangentsoft.net/wskfaq/
    http://www.hal-pc.org/~johnnie2/winsock.html
    http://fiddle.visc.vt.edu/courses/ec...tures.html#T16

    a google search will probably turn up more info and if you have additional questions that bithub can't help you with :d - you can check out here

    BTW bith very succint pseudocode
    [/edit]
    Last edited by caroundw5h; 11-07-2004 at 07:30 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    1) Yes, I know how a server works, I've already written one, it's getting network access in C is what I've been asking about. And there is, by the way, a LOT more to the server then that. You still have to do a lot of processing on the files to get it even close to HTTP compliant.

    2) Yes, I am talking about libwww

  10. #10
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Sean, are you going to use pthreads for your server? just curious...
    :wq

  11. #11
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    1) Yes, I know how a server works, I've already written one,
    glad to hear.

    it's getting network access in C is what I've been asking about.
    And now you have a plethora of info.

    And there is, by the way, a LOT more to the server then that. You still have to do a lot of processing on the files to get it even close to HTTP compliant.
    you are of course right sean. I was merely given you an outline using text/html and a browser as the client. You can get as complex as you like.

    goog luck in your endeavour.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Actually I haven't given threads much thought yet. Any advice on THAT?

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    windows doesn't come with a POSIX thread implementation, so to use pthreads, you're going to have to install a third party library (cygwin might come with POSIX threads, I'm not sure).

    I suggest just using the windows API for threading. The function to use is CreateThread().

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I suggest just using the windows API for threading.
    Ah - I see you're catching on to the whole generic-brand thing I've got going... Thanks, bud.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  3. SMTP Server Not Working
    By (TNT) in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-15-2003, 05:33 AM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM