From 5806c893b67e20366d8e0b51164878e44f23d7f7 Mon Sep 17 00:00:00 2001 From: Thea Olivia <theaolivia.mail@gmail.com> Date: Tue, 26 Apr 2016 18:47:15 +0700 Subject: [PATCH] Server.java and Paxos.java update --- src/server/Paxos.java | 13 ++++++++++++- src/server/Server.java | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/server/Paxos.java b/src/server/Paxos.java index 3aca61f..27248cc 100644 --- a/src/server/Paxos.java +++ b/src/server/Paxos.java @@ -9,5 +9,16 @@ import org.json.simple.parser.JSONParser; public class Paxos { - + DatagramSocket clientSocket; + + public void sendRequest(String sentence, String host, int port) + { + clientSocket = new DatagramSocket(); + InetAddress IPAddress = InetAddress(); + byte[] sendData = new byte[1024]; + System.out.println(sentence); + sendData = sentence.getBytes(); + DatagramPacket sendPacket = new DatagramPacket(); + clientSocket.send(sendPacket); + } } \ No newline at end of file diff --git a/src/server/Server.java b/src/server/Server.java index 89486b1..f28bbe7 100644 --- a/src/server/Server.java +++ b/src/server/Server.java @@ -6,15 +6,22 @@ import java.net.ConnectException; import java.net.NoRouteToHostException; import java.net.UnknownHostException; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.parser.JSONParser; public class Server { + private String host; + private int port; + private Socket socket; + private final String DEFAULT_HOST = "localhost"; + public Server() { - + this.host = host; + this.port = port; + this.socket = socket; } public boolean isWerewolf() @@ -23,11 +30,9 @@ public class Server return yes; } - - public void connect(String host, int port) throws IOException { - this.host = hostl + this.host = host; this.port = port; socket = new Socket(host, port); System.out.println("Game sudah dimulai... V^__^V"); @@ -36,7 +41,27 @@ public class Server public JSONObject receiveJSON() throws IOException { + InputStream in = socket.getInputStream(); + ObjectInputStream i = new ObjectInputStream(); + JSONObject line = null; + try { + line = (JSONObject) i.readObject(); + } catch (ClassNotFoundException e){ + e.printStackTrace(); + } + + return line; + } + + public void sendJSON(JSONObject jsonObj) throws IOException + { + JSONObject jsonObject2 = new JSONObject(); + jsonObject2.put("key", new Paper(250,333)); + OutputStream out = socket.getOutputStream(); + ObjectOutputStream o = new ObjectOutputStream(out); + o.writeObject(jsonObject2); + out.flush(); } public static void main(String[] args) throws Exception -- GitLab