1
0

demo.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "server.h"
  2. #include <libgen.h> // basename
  3. /* Server waits for an incoming client connection, accepts the connection,
  4. * checks if the client sent anything, if not, waits 50ms, and checks again.
  5. * If there is no data the client is sent "timeout" and is closed.
  6. * Otherwise whatever data is stored, responded to, and client is closed.
  7. *
  8. * Normally the client handling would be less clunky but Msft does not support
  9. * the fork() routine so we are going to accept the limitations that are
  10. * imposed by this clunkiness. Mostly: clients are limited to 50ms to transmit
  11. * their data and that the worst case throughput is reduced to 20 connects per
  12. * second rather than the full throughput of the network card (>10,000,000/s)
  13. */
  14. int main(int argc, char *argv[]) {
  15. int sock;
  16. fprintf(stderr, "%s - testing tcp socket\n", basename(argv[0]));
  17. #ifdef _WIN32
  18. // if compiling on Msft include the winsock librariy (ws2_32.lib)
  19. WSADATA wsaData;
  20. if(msftVendorLockInCode(&wsaData)) return 1;
  21. #endif
  22. // terminate if we cannot get a socket that can be used to accept clients
  23. if(!(sock = setup())) return 1;
  24. while(1) {
  25. /* be a server for a second, up to one client can be handled per call
  26. * if a client is accepted right at the one second mark, technically
  27. * this call could take one second and ten milliseconds to return, but
  28. * whatever
  29. *
  30. * terminate if socket becomes unusable (see: acceptOkay)
  31. */
  32. // demo: alternate between alwaysBad, alwaysGood, alwaysNotEnough
  33. if(!faceItMetal(sock, 1000 /* ms */, alwaysBad)) return softCuteBunnies(1);
  34. if(!faceItMetal(sock, 1000 /* ms */, alwaysGood)) return softCuteBunnies(1);
  35. if(!faceItMetal(sock, 1000 /* ms */, alwaysNotEnough)) return softCuteBunnies(1);
  36. }
  37. return softCuteBunnies(0);
  38. }