Commit a8803b9e authored by Jonathan Sudibya's avatar Jonathan Sudibya
Browse files

JSON-connect final

parent eac30866
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/> <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2"> <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group> <group>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/TCPClient/src/TCPClient.java</file> <file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/client/TCPClient/src/TCPClient.java</file>
</group> </group>
</open-files> </open-files>
</project-private> </project-private>
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/> <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2"> <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group> <group>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/TCPServer/src/TCPServer.java</file> <file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/server/TCPServer/src/TCPServer.java</file>
</group> </group>
</open-files> </open-files>
</project-private> </project-private>
...@@ -6,6 +6,10 @@ import java.io.InputStreamReader; ...@@ -6,6 +6,10 @@ import java.io.InputStreamReader;
import java.net.Socket; import java.net.Socket;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/* /*
* To change this license header, choose License Headers in Project Properties. * To change this license header, choose License Headers in Project Properties.
...@@ -37,27 +41,35 @@ class Client implements Runnable { ...@@ -37,27 +41,35 @@ class Client implements Runnable {
@Override @Override
public void run() { public void run() {
JSONParser parser = new JSONParser();
System.out.println("Running thread " + threadName); System.out.println("Running thread " + threadName);
while(active) while(active)
{ {
DataOutputStream outToClient = null; DataOutputStream outToClient = null;
BufferedReader inFromClient = null;
try { try {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
clientSentence = inFromClient.readLine(); clientSentence = inFromClient.readLine();
if (clientSentence != null){ if (clientSentence != null){
outToClient = new DataOutputStream(connectionSocket.getOutputStream()); outToClient = new DataOutputStream(connectionSocket.getOutputStream());
System.out.println("Received: " + clientSentence); System.out.println("Received: " + clientSentence);
Object obj = parser.parse(clientSentence);
if (obj != null){
JSONObject array = (JSONObject) obj;
}
capitalizedSentence = clientSentence.toUpperCase() + '\n'; capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence); outToClient.writeBytes(capitalizedSentence);
connectionSocket.close();
} }
else{ else{
active = false; active = false;
} }
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} finally { } finally {
try { try {
inFromClient.close();
outToClient.close(); outToClient.close();
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
......
...@@ -22,10 +22,12 @@ public class ConnectionHandler implements Runnable { ...@@ -22,10 +22,12 @@ public class ConnectionHandler implements Runnable {
private ServerSocket welcomeSocket; private ServerSocket welcomeSocket;
private Vector<Client> clients; private Vector<Client> clients;
private boolean active; private boolean active;
private int globalID;
public ConnectionHandler(){ public ConnectionHandler(){
threadName = "Connection-Handler"; threadName = "Connection-Handler";
active = true; active = true;
globalID = 0;
try { try {
welcomeSocket = new ServerSocket(6789); welcomeSocket = new ServerSocket(6789);
clients = new Vector<>(); clients = new Vector<>();
...@@ -40,8 +42,9 @@ public class ConnectionHandler implements Runnable { ...@@ -40,8 +42,9 @@ public class ConnectionHandler implements Runnable {
System.out.println("Running thread " + threadName); System.out.println("Running thread " + threadName);
while(active){ while(active){
try { try {
clients.add(new Client(clients.size(),welcomeSocket.accept())); clients.add(new Client(globalID,welcomeSocket.accept()));
clients.lastElement().start(); clients.lastElement().start();
globalID++;
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
} }
......
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