Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. #include "Net.h"
  2.  
  3. void * SERVER::get_in_addr(sockaddr* sa)
  4. {
  5. if(sa->sa_family == AF_INET)
  6. {
  7. return &(((sockaddr_in*)sa)->sin_addr);
  8. }else if(sa->sa_family == AF_INET6)
  9. {
  10. return &(((sockaddr_in6*)sa)->sin6_addr);
  11. }
  12. }
  13.  
  14. SERVER::SERVER()
  15. {
  16. int listener = SERVER::SetUpServer();
  17.  
  18. sockaddr_storage remoteAddr;
  19. socklen_t socketlength = sizeof(remoteAddr);
  20.  
  21. char buffer[4096];
  22. memset(&buffer,0,sizeof(buffer));
  23.  
  24. int fdmax,nByte;
  25. fd_set master,read_fd;
  26.  
  27. FD_ZERO(&master);
  28. FD_ZERO(&read_fd);
  29.  
  30. char remoteIP[INET6_ADDRSTRLEN];
  31.  
  32.  
  33. if(listen(listener,10) == SOCKET_ERROR)
  34. {
  35. std::cout << "listen() failed with error number " << WSAGetLastError() <<std::endl;
  36. exit(-1);
  37. }
  38.  
  39. fdmax = listener;
  40.  
  41. FD_SET(listener,&master);
  42.  
  43. for(;;)
  44. {
  45. read_fd = master;
  46.  
  47. if(select(fdmax +1,&read_fd,NULL,NULL,NULL) == SOCKET_ERROR)
  48. {
  49. std::cout << "select() failed with error number " << WSAGetLastError() <<std::endl;
  50. exit(-1);
  51. }
  52.  
  53. for(int i = 0; i <= fdmax; i++)
  54. {
  55. if(FD_ISSET(i,&read_fd))
  56. {
  57. if(i == listener)
  58. {
  59. int newSock = accept(listener,(sockaddr *)&remoteAddr,&socketlength);
  60.  
  61. if(newSock == SOCKET_ERROR)
  62. {
  63. std::cout << "accept() failed with error number " << WSAGetLastError() << std::endl;
  64. }
  65.  
  66. std::cout << "New connection from: " << inet_ntop(remoteAddr.ss_family,get_in_addr((sockaddr*)&remoteAddr),remoteIP,INET6_ADDRSTRLEN) <<std::endl;
  67.  
  68. FD_SET(newSock,&master);
  69.  
  70. if(newSock > fdmax)
  71. fdmax = newSock;
  72. }else
  73. {
  74.  
  75. if ((nByte = recv(i,buffer,sizeof(buffer),0)) == ERROR)
  76. {
  77. std::cout << "recv() failed with error number "<<WSAGetLastError() <<std::endl;
  78. exit(-1);
  79. }else if(nByte == 0)
  80. {
  81. std::cout << "Connection closed by host: " << i << std::endl;
  82. closesocket(i);
  83. FD_CLR(i,&master);
  84. }else
  85. {
  86. std::cout << buffer << std::endl;
  87. memset(&buffer,0,sizeof(buffer));
  88. }
  89. }
  90. }
  91. }
  92. }
  93.  
  94. }
  95.  
  96. int SERVER::SetUpServer()
  97. {
  98. addrinfo *result = NULL;
  99. SOCKET listener = 0;
  100. const char yes = 1;
  101. WSAData data;
  102.  
  103. WSAStartup(MAKEWORD(2,2),&data);
  104.  
  105. SERVER::gatherInformation(&result);
  106.  
  107. for(;result != NULL; result = result->ai_next)
  108. {
  109. if((listener = socket(result->ai_family,result->ai_socktype,result->ai_protocol)) == INVALID_SOCKET)
  110. {
  111. std::cout << "Socket () failed with error number: " << WSAGetLastError() << std::endl;
  112. }
  113.  
  114. if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(int)) == ERROR)
  115. {
  116. std::cout << "Setsockopt() failed with error number:" << WSAGetLastError() <<std::endl;
  117. freeaddrinfo(result);
  118. exit(-1);
  119. }
  120. if(bind(listener,result->ai_addr,result->ai_addrlen) < 0)
  121. {
  122. continue;
  123. }
  124. return listener;
  125. }
  126. }
  127.  
  128. void SERVER::gatherInformation(addrinfo ** result)
  129. {
  130. addrinfo hints;
  131. ZeroMemory(&hints,sizeof(hints));
  132. hints.ai_flags = AI_PASSIVE;
  133. hints.ai_family = AF_UNSPEC;
  134.  
  135. retry:
  136. try
  137. {
  138. if(getaddrinfo(NULL,INIT_PORT_NUMBER,&hints,result) < 0)
  139. {
  140. DWORD error = GetLastError();
  141. throw new ERR(error);
  142. }
  143. freeaddrinfo(&hints);
  144.  
  145. }catch(ERR& e)
  146. {
  147. if(e.GetError() == WSATRY_AGAIN)
  148. {
  149. /* Handling this here because
  150. * it's just a temporany error */
  151. Sleep(10000);
  152. goto retry;
  153. }else
  154. {
  155. /*Using another function
  156. * to parse non-handled error*/
  157. std::cout << "getaddrinfo() failed with error number: " <<std::endl;
  158. freeaddrinfo(*result);
  159. }
  160. }
  161.  
  162. }
  163.  
  164. #ifndef H_GUARD_NET
  165. #define H_GUARD_NET
  166.  
  167. #define WIN32_LEAN_AND_MEAN
  168.  
  169. #include <WinSock2.h>
  170. #include <Windows.h>
  171. #include <WS2tcpip.h>
  172. #include <iostream>
  173. #include <WinBase.h>
  174.  
  175. #pragma comment(lib,"kernel32.lib");
  176. #pragma comment(lib,"Ws2_32.lib");
  177.  
  178.  
  179. #define INIT_PORT_NUMBER "64390"
  180.  
  181. class SERVER
  182. {
  183. public:
  184. SERVER();
  185. private:
  186. int SetUpServer();
  187. void gatherInformation(addrinfo** hints);
  188. void * get_in_addr(sockaddr* sa);
  189.  
  190. ~SERVER(){}
  191.  
  192. };
  193.  
  194. class ERR
  195. {
  196. public:
  197.  
  198. ERR(DWORD& val) : ERR_NO(val) {}
  199.  
  200. int GetError() {return ERR_NO;}
  201.  
  202.  
  203.  
  204. ~ERR() {}
  205.  
  206. private:
  207. const int ERR_NO;
  208. };
  209.  
  210. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement