1
0

web-demo.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* example web server
  2. */
  3. #include "server.h"
  4. #include "http-server/http-server.h"
  5. int main(int argc, char *argv[]) {
  6. int sock;
  7. #ifdef _WIN32
  8. // if compiling on Msft include the winsock librariy (ws2_32.lib)
  9. WSADATA wsaData;
  10. if(msftVendorLockInCode(&wsaData)) return 1;
  11. #endif
  12. // terminate if we cannot get a socket that can be used to accept clients
  13. if(!(sock = setup())) return 1;
  14. #ifdef USE_CLIENT_POOL
  15. struct client pool[POOL_SIZE];
  16. // initialize client pool
  17. for(uint32_t i = 0; i < POOL_SIZE; i++) pool[i].fd = -1;
  18. // server listen port is first socket file descriptor
  19. memset(&pool[0], 0, sizeof(pool[0])); pool[0].fd = sock;
  20. #endif
  21. while(1) {
  22. /* be a server for a second, up to one client can be handled per call
  23. * if a client is accepted right at the one second mark, technically
  24. * this call could take one second and ten milliseconds to return, but
  25. * whatever
  26. *
  27. * terminate if socket becomes unusable (see: acceptOkay)
  28. */
  29. #ifdef USE_CLIENT_POOL
  30. if(!faceItMetal(1000 /* ms */, httpEcho, pool)) return softCuteBunnies(1);
  31. #else
  32. if(!faceItMetal(sock, 1000 /* ms */, httpEcho)) return softCuteBunnies(1);
  33. #endif
  34. }
  35. return softCuteBunnies(0);
  36. }