Runtime_win32.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <vector>
  5. #include <string>
  6. #include <winsock2.h>
  7. #include <ws2tcpip.h>
  8. #pragma comment(lib,"ws2_32.lib")
  9. #include "cocos2d.h"
  10. #include "runtime/ConfigParser.h"
  11. using namespace std;
  12. string getIPAddress()
  13. {
  14. WSADATA wsaData;
  15. char name[155]={0};
  16. char ip[16];
  17. struct addrinfo hints;
  18. struct addrinfo *res = nullptr;
  19. int ret;
  20. struct sockaddr_in *addr;
  21. // customized by user
  22. auto &bindAddress = ConfigParser::getInstance()->getBindAddress();
  23. if (!bindAddress.empty())
  24. {
  25. return bindAddress;
  26. }
  27. memset(ip, 0, sizeof(ip));
  28. if ( WSAStartup( MAKEWORD(2,0), &wsaData ) == 0 )
  29. {
  30. memset(&hints, 0, sizeof(struct addrinfo));
  31. hints.ai_family = AF_INET; /* Allow IPv4 */
  32. hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
  33. hints.ai_protocol = 0; /* Any protocol */
  34. hints.ai_socktype = SOCK_STREAM;
  35. ret = getaddrinfo(name, NULL, &hints, &res);
  36. if (ret == 0 && res)
  37. {
  38. addr = (struct sockaddr_in *)res->ai_addr;
  39. inet_ntop(AF_INET, &addr->sin_addr, ip, 16);
  40. }
  41. WSACleanup( );
  42. }
  43. return ip;
  44. }