Commit 4572a1f7 authored by Kevin's avatar Kevin
Browse files

perbaiki conflict private.xml

parents 9677b203 650deeda
......@@ -3,7 +3,11 @@
<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>
<<<<<<< HEAD
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/client/TCPClient/src/TCPClient.java</file>
=======
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/client/TCPClient/src/TCPClient.java</file>
>>>>>>> origin/JSON-connect
</group>
</open-files>
</project-private>
......@@ -4,7 +4,6 @@
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/crudDB/crudDB.java</file>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/TCPServer.java</file>
</group>
</open-files>
</project-private>
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
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.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jonathan
*/
class Client implements Runnable {
private int ID;
private String threadName;
private Socket connectionSocket;
private Thread t;
private boolean active;
private String clientSentence;
private String capitalizedSentence;
public Client(int _id,Socket _client)
{
ID = _id;
connectionSocket = _client;
threadName = "Client-Listener-"+ID;
active = true;
System.out.println("Created thread " + threadName);
}
@Override
public void run() {
JSONParser parser = new JSONParser();
System.out.println("Running thread " + threadName);
while(active)
{
DataOutputStream outToClient = null;
BufferedReader inFromClient = null;
try {
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);
}
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);
}
}
}
System.out.println("Thread " + threadName + " exiting...");
}
public void start(){
if (t == null)
{
t = new Thread(this, threadName);
t.start();
}
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jonathan
*/
public class ConnectionHandler implements Runnable {
private Thread t;
private String threadName;
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<>();
} catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Created thread " + threadName);
}
@Override
public void run() {
System.out.println("Running thread " + threadName);
while(active){
try {
clients.add(new Client(globalID,welcomeSocket.accept()));
clients.lastElement().start();
globalID++;
} catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Thread " + threadName + " exiting...");
}
public void start(){
if (t == null){
t = new Thread(this, threadName);
t.start();
}
}
public void setActive(boolean _active)
{
active = _active;
}
}
......@@ -13,21 +13,11 @@ import java.net.*;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
class TCPServer {
public static void main(String argv[]) throws Exception {
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
for (;;) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient
= new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
class TCPServer
{
public static void main(String argv[]) throws Exception
{
ConnectionHandler CH = new ConnectionHandler();
CH.start();
}
}
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