send_head.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "stdafx.h"
  2. #include <winsock2.h>
  3. #include "constdef.h"
  4. void sendHead(SOCKET s, int state, char* fileExtension, char* version)
  5. {
  6. char buf[MAXSIZE] = {'\0'}, *content = "text/plain", *stateStr = NULL;
  7. switch(state)
  8. {
  9. case 200:
  10. stateStr = "OK";
  11. break;
  12. case 400:
  13. stateStr = "Bad Request";
  14. break;
  15. case 404:
  16. stateStr = "Not Found";
  17. break;
  18. default:
  19. stateStr = "Unknown";
  20. break;
  21. }
  22. if(strcmp(fileExtension, "html") == 0 || strcmp(fileExtension, "htm") == 0)
  23. {
  24. content = "text/html";
  25. }
  26. else if(strcmp(fileExtension, "css") == 0)
  27. {
  28. content = "text/css";
  29. }
  30. else if(strcmp(fileExtension, "gif") == 0)
  31. {
  32. content = "image/gif";
  33. }
  34. else if(strcmp(fileExtension, "jpeg") == 0)
  35. {
  36. content ="image/jpeg";
  37. }
  38. else if(strcmp(fileExtension, "jpg") == 0)
  39. {
  40. content = "image/jpg";
  41. }
  42. else if(strcmp(fileExtension,"png") == 0)
  43. {
  44. content = "image/png";
  45. }
  46. sprintf_s(buf, "%s %d %s \r\nContent-Type: %s \r\n\r\n", version, state, stateStr, content);
  47. int sendCount = strlen(buf);
  48. //for(;buf[sendCount]!='\0';sendCount++);
  49. send(s, buf, sendCount, 0);
  50. return ;
  51. }