Commit d44f8bd1 authored by adwisatya's avatar adwisatya
Browse files

IP untuk GEtServer bener

parent d2fb77df
......@@ -17,6 +17,7 @@ import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
......@@ -41,7 +42,8 @@ public class Client extends Thread{
public String tmpStr;
public String time;
public ArrayList<String> location = new ArrayList<String>(Arrays.asList("0", "0"));
//public int item;
NetworkInterface networkinterface = NetworkInterface.getByIndex(13);
//public int item;
// public int heigth;
// public int width;
// public String name;
......@@ -56,6 +58,7 @@ public class Client extends Thread{
}
public void run(){
getIP();
/* Testing database */
clientSentence = "";
while(!clientSentence.equals("dead")){
......@@ -172,13 +175,12 @@ public class Client extends Thread{
System.out.println("request");
}
public void cmdJoin() throws IOException, JSONException{
String response = "";
Socket miniSocket = new Socket(URL,defPort);
ArrayList<String> tmpServer = new ArrayList<String>();
JSONObject obj = new JSONObject();
obj.put("method", "join");
obj.put("ip", InetAddress.getLocalHost().getHostAddress());
obj.put("ip", getIP());
obj.put("port", new Integer(myPort));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(miniSocket.getOutputStream())));
......@@ -588,4 +590,30 @@ public class Client extends Thread{
}
return pass;
}
public String getIP(){
String myIP = "";
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
if(ip.contains("167.205.")){
myIP = ip;
}
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
return myIP;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment