Commit 2a9c729e authored by Jonathan Sudibya's avatar Jonathan Sudibya
Browse files

Mark 1 : Client Backend clear

parent 8b59bd62
/*
* 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.
*/
package Connection;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Jonathan
*/
public class ConnectionHandler {
private Socket connection;
public ConnectionHandler(String address,int port)
{
try {
connection = new Socket(address,port);
} catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void start(){
}
}
/*
* 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.
*/
package Connection;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
*
* @author Jonathan
*/
public class Listener implements Runnable {
private Socket connection;
private Vector<JSONObject> data;
private Thread t;
private boolean active;
public Listener(Socket _connection)
{
connection = _connection;
data = new Vector<>();
active = true;
}
@Override
public void run() {
JSONParser parser = new JSONParser();
while (active){
try {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
data.add((JSONObject) parser.parse(inFromServer.readLine()));
} catch (IOException ex) {
Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void start(){
if (t != null){
t = new Thread(this,"Client Listener");
t.start();
}
}
public void setActive(boolean _active)
{
active = _active;
}
}
/*
* 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.
*/
package Connection;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
/**
*
* @author Jonathan
*/
public class Sender implements Runnable {
private JSONObject data;
private Socket connectionSocket;
private boolean active;
private Thread t;
public Sender(Socket _connection)
{
connectionSocket = _connection;
active = true;
}
@Override
public void run()
{
while(active)
{
if (!data.isEmpty()){
try {
DataOutputStream outToServer = new DataOutputStream(connectionSocket.getOutputStream());
outToServer.write(data.toJSONString().getBytes(Charset.forName("UTF-8")));
data.clear();
} catch (IOException ex) {
Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void setActive(boolean a)
{
active = a;
}
public void setData(JSONObject _data)
{
data = _data;
}
public boolean isDataEmpty()
{
return data.isEmpty();
}
public void start()
{
if (t != null)
{
t = new Thread(this,"Client Sender");
t.start();
}
}
}
......@@ -18,16 +18,17 @@ import static org.json.simple.JSONValue.parse;
class TCPClient {
public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
System.out.println(InetAddress.getLocalHost().getAddress());
// String sentence;
// String modifiedSentence;
// BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
// Socket clientSocket = new Socket("localhost", 6789);
// DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
// BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// sentence = inFromUser.readLine();
// outToServer.writeBytes(sentence + '\n');
// modifiedSentence = inFromServer.readLine();
// System.out.println("FROM SERVER: " + modifiedSentence);
// clientSocket.close();
}
}
......@@ -57,7 +57,7 @@ public class Client implements Runnable {
System.out.println("Received: " + clientSentence);
Object obj = parser.parse(clientSentence);
if (obj != null){
JSONObject array = (JSONObject) obj;
JSONObject data = (JSONObject) obj;
}
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
......
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