sockets.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "sockets.h"
  2. bool socket_init() {
  3. #ifdef _WIN32 /* initialize the socket API */
  4. WSADATA info;
  5. if (WSAStartup(MAKEWORD(1, 1), &info) != 0) {
  6. fprintf(stderr, "Cannot initialize Winsock.\n");
  7. return false;
  8. }
  9. #endif
  10. return true;
  11. }
  12. bool socket_set_non_blocking(int fd) {
  13. if (fd < 0)
  14. return false;
  15. #ifdef _WIN32
  16. unsigned long mode = 1;
  17. return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? true : false;
  18. #else
  19. int flags = fcntl(fd, F_GETFL, 0) | O_NONBLOCK;
  20. return (fcntl(fd, F_SETFL, flags) == 0) ? true : false;
  21. #endif
  22. }
  23. int socket_accept(int server_fd) {
  24. int cfd;
  25. struct sockaddr_in client;
  26. struct hostent *client_info;
  27. #ifndef _WIN32
  28. socklen_t asize;
  29. #else
  30. int asize;
  31. #endif
  32. asize = sizeof(struct sockaddr_in);
  33. cfd = accept(server_fd, (struct sockaddr *)&client, &asize);
  34. if (cfd == -1) {
  35. #ifdef _WIN32
  36. int e = WSAGetLastError();
  37. if (e == WSAEWOULDBLOCK)
  38. return 0;
  39. fprintf(stderr, "Accept error: %d.\n", e);
  40. #else
  41. if (errno == EWOULDBLOCK)
  42. return 0;
  43. fprintf(stderr, "Accept error: %d.\n", errno);
  44. #endif
  45. return -1;
  46. }
  47. client_info = gethostbyname((char *)inet_ntoa(client.sin_addr));
  48. printf("Accepted connection from: %s.\n", client_info->h_name);
  49. return cfd;
  50. }
  51. bool socket_close(int fd) {
  52. #ifdef _WIN32
  53. return (closesocket(fd) == 0) ? true : false;
  54. #else
  55. return (close(fd) == 0) ? true : false;
  56. #endif
  57. }
  58. bool socket_cleanup() {
  59. #ifdef _WIN32
  60. return (WSACleanup() == 0) ? true : false;
  61. #else
  62. return true;
  63. #endif
  64. }
  65. /*
  66. Creates a socket and bind it to port.
  67. */
  68. int create_socket_server(int port) {
  69. int sfd, rc;
  70. struct sockaddr_in address;
  71. if (!socket_init())
  72. {
  73. fprintf (stderr, "socket_init failed");
  74. return -1;
  75. }
  76. sfd = socket(AF_INET, SOCK_STREAM, 0);
  77. if (sfd == -1) {
  78. fprintf(stderr, "Cannot create socket.\n");
  79. return -1;
  80. }
  81. int one = 1;
  82. setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  83. memset(&address, 0, sizeof(struct sockaddr_in));
  84. address.sin_family = AF_INET;
  85. address.sin_port = htons((unsigned short)port);
  86. address.sin_addr.s_addr = INADDR_ANY;
  87. rc = bind(sfd, (struct sockaddr *)&address, sizeof(struct sockaddr));
  88. if (rc == -1) {
  89. fprintf(stderr, "Cannot bind port %d.\n", port);
  90. socket_close(sfd);
  91. return -1;
  92. }
  93. if (listen(sfd, 1) == -1) {
  94. fprintf(stderr, "Cannot listen for connections.\n");
  95. socket_close(sfd);
  96. return -1;
  97. }
  98. printf ("socket initialized at port %d.\n", port);
  99. return sfd;
  100. }