Commit 8f547042 authored by Kevin's avatar Kevin
Browse files

aneh dah

parent ca3cabd6
......@@ -21,7 +21,6 @@ import org.json.simple.parser.ParseException;
class TCPServer {
public ServerSocket welcomeSocket;
public Vector<Client> clients;
public boolean active;
public int globalID;
public Socket conSocket;
......@@ -32,6 +31,7 @@ class TCPServer {
globalID = 0;
try {
welcomeSocket = new ServerSocket(2000);
// welcomeSocket.setSoTimeout(100000);
} catch (IOException ex) {
Logger.getLogger(TCPServer.class.getName()).log(Level.SEVERE, null, ex);
}
......@@ -43,15 +43,20 @@ class TCPServer {
DataOutputStream outToTracker = null;
BufferedReader inFromTracker = null;
System.out.println("Nunggu koneksi");
System.out.println("Why???");
conSocket = welcomeSocket.accept();
System.out.println("Koneksi sudah ada");
inFromTracker = new BufferedReader(new InputStreamReader(conSocket.getInputStream()));
outToTracker = new DataOutputStream(conSocket.getOutputStream());
int i = 0;
while (active) {
try {
trackerSentence = inFromTracker.readLine();
while ((trackerSentence = inFromTracker.readLine()) == null) {
System.out.println(i);
inFromTracker = new BufferedReader(new InputStreamReader(conSocket.getInputStream()));
}
System.out.println("trackerSentence telah terbaca");
System.out.println("Received: " + trackerSentence);
System.out.println("TrackerSentence: " + trackerSentence);
if (trackerSentence != null) {
System.out.println("TrackerSentence: " + trackerSentence);
Object obj = parser.parse(trackerSentence);
......@@ -61,14 +66,15 @@ class TCPServer {
JSONArray server_list_active = new JSONArray();
server_list_active = (JSONArray) inJson.get("server");
System.out.println(server_list_active);
resultJson.put("status", "ok");
outToTracker.write(resultJson.toString().getBytes());
return resultJson.toString();
}
}
} else {
} else if (trackerSentence == null) {
active = false;
System.out.println("TrackerSentence NULL");
}
resultJson.put("status", "ok");
outToTracker.write(resultJson.toString().getBytes());
return resultJson.toString();
} catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
......@@ -97,29 +103,29 @@ class TCPServer {
newServer.close();
}
// public static String serverStatus(Object inObj) {
// JSONObject resultJson = new JSONObject();
// if (inObj != null) {
// JSONObject inJson = (JSONObject) inObj;
// if (inJson.containsKey("method")) {
// if (inJson.get("method").toString().equalsIgnoreCase("serverStatus")) {
// JSONArray server_list_active = new JSONArray();
// server_list_active = (JSONArray) inJson.get("server");
// System.out.println(server_list_active);
// resultJson.put("status", "ok");
// }
// }
// }
// resultJson.put("status", "error");
// return resultJson.toString();
// }
public static void message() throws IOException {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket newServer = new Socket("167.205.32.46", 8025);
DataOutputStream outToTracker = new DataOutputStream(newServer.getOutputStream());
BufferedReader inFromTracker = new BufferedReader(new InputStreamReader(newServer.getInputStream()));
sentence = inFromUser.readLine();
modifiedSentence = inFromTracker.readLine();
System.out.println("FROM TRACKER: " + modifiedSentence);
outToTracker.write(sentence.getBytes());
newServer.close();
}
public static void main(String argv[]) throws Exception {
// ConnectionHandler CH = new ConnectionHandler();
// CH.start();
String hasil;
join();
TCPServer tes = new TCPServer();
hasil = tes.serverStatus();
System.out.println(hasil);
// message();
}
}
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