From faff7724d61b9e5ec97d81e01d01dda1ca83c8e2 Mon Sep 17 00:00:00 2001 From: "Xawotihs@gmail.com" Date: Mon, 28 Jan 2013 22:03:26 +0000 Subject: [PATCH] Added a function to get the current IP address --- JGE/src/JNetwork.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/JGE/src/JNetwork.cpp b/JGE/src/JNetwork.cpp index 7ee54f4d1..934501e86 100644 --- a/JGE/src/JNetwork.cpp +++ b/JGE/src/JNetwork.cpp @@ -6,6 +6,7 @@ #include "../include/DebugRoutines.h" #include "../include/JNetwork.h" +#include #if defined (WIN32) || defined (LINUX) #else @@ -40,6 +41,32 @@ bool JNetwork::isConnected(){ return socket->isConnected(); } +void JNetwork::getServerIp(string& aString) +{ +#ifdef WIN32 + char ac[80]; + if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) { + DebugTrace("Error " << WSAGetLastError() << + " when getting local host name.\n"); + return; + } + DebugTrace( "Host name is " << ac << ".\n"); + + struct hostent *phe = gethostbyname(ac); + if (phe == 0) { + DebugTrace("Yow! Bad host lookup.\n"); + return; + } + + for (int i = 0; phe->h_addr_list[i] != 0; ++i) { + struct in_addr addr; + memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr)); + aString = inet_ntoa(addr); + } +#endif +} + + JNetwork::JNetwork() : mpWorkerThread(NULL), socket(0) { @@ -145,9 +172,9 @@ void JNetwork::ThreadProc(void* param) while(pSocket && pSocket->isConnected()) { char buff[4096]; { - boost::mutex::scoped_lock l(pThis->receiveMutex); int len = pSocket->Read(buff, sizeof(buff)); if(len > 0) { + boost::mutex::scoped_lock l(pThis->receiveMutex); DebugTrace("receiving " << len << " bytes : " << buff); pThis->received.str(pThis->received.str() + buff); DebugTrace("received " << pThis->received.str().size() << " bytes : " << pThis->received.str()); @@ -167,6 +194,8 @@ void JNetwork::ThreadProc(void* param) #ifndef ANDROID boost::this_thread::sleep(1); #endif +#elif WIN32 +// boost::this_thread::sleep(boost::posix_time::microseconds(10)); #endif } @@ -174,7 +203,7 @@ void JNetwork::ThreadProc(void* param) } #if defined (WIN32) || defined (LINUX) -int JNetwork::connect(string ip) +int JNetwork::connect(const string& ip) { if (mpWorkerThread) return 0; serverIP = ip;