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

JSON-connect final

parent eac30866
......@@ -3,7 +3,7 @@
<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">
<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>
</open-files>
</project-private>
......@@ -3,7 +3,7 @@
<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">
<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>
</open-files>
</project-private>
......@@ -6,6 +6,10 @@ import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
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.
......@@ -37,27 +41,35 @@ class Client implements Runnable {
@Override
public void run() {
JSONParser parser = new JSONParser();
System.out.println("Running thread " + threadName);
while(active)
{
DataOutputStream outToClient = null;
BufferedReader inFromClient = null;
try {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
clientSentence = inFromClient.readLine();
if (clientSentence != null){
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
System.out.println("Received: " + clientSentence);
Object obj = parser.parse(clientSentence);
if (obj != null){
JSONObject array = (JSONObject) obj;
}
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
connectionSocket.close();
}
else{
active = false;
}
} catch (IOException 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 {
try {
inFromClient.close();
outToClient.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
......
......@@ -22,10 +22,12 @@ public class ConnectionHandler implements Runnable {
private ServerSocket welcomeSocket;
private Vector<Client> clients;
private boolean active;
private int globalID;
public ConnectionHandler(){
threadName = "Connection-Handler";
active = true;
globalID = 0;
try {
welcomeSocket = new ServerSocket(6789);
clients = new Vector<>();
......@@ -40,8 +42,9 @@ public class ConnectionHandler implements Runnable {
System.out.println("Running thread " + threadName);
while(active){
try {
clients.add(new Client(clients.size(),welcomeSocket.accept()));
clients.add(new Client(globalID,welcomeSocket.accept()));
clients.lastElement().start();
globalID++;
} catch (IOException 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