Commit ee2d88ff authored by Rakhmatullah Yoga Sutrisna's avatar Rakhmatullah Yoga Sutrisna
Browse files

clientHandler

parent 2de5496f
/*
* 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 grandquestserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Rakhmatullah Yoga S
*/
public class ClientHandler extends Thread {
private Socket socketListener;
public ClientHandler(ServerSocket sock) throws IOException {
System.out.println("Waiting for client on port " + sock.getLocalPort() + "...");
socketListener = sock.accept();
System.out.println("Just connected to " + socketListener.getRemoteSocketAddress());
}
@Override
public void run() {
String response;
JSONParse parse = new JSONParse();
while(true) {
try {
DataInputStream in = new DataInputStream(socketListener.getInputStream());
response = in.readUTF();
System.out.println(response);
DataOutputStream out = new DataOutputStream(socketListener.getOutputStream());
out.writeUTF("Thank you for connecting to " + socketListener.getLocalSocketAddress() + "\nGoodbye!");
} catch (IOException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
try {
socketListener.close();
} catch (IOException ex1) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex1);
}
}
}
}
}
......@@ -58,4 +58,7 @@ public class DBHandler {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void removefromInventory() {
}
}
......@@ -45,8 +45,8 @@ public class Server extends Thread {
@Override
public void run()
{
String response;
JSONParse parse = new JSONParse();
String response;
JSONParse parse = new JSONParse();
while(true)
{
try
......@@ -59,22 +59,22 @@ public class Server extends Thread {
DataInputStream in =
new DataInputStream(server.getInputStream());
response = in.readUTF();
System.out.println(response);
response = in.readUTF();
System.out.println(response);
DataOutputStream out =
new DataOutputStream(server.getOutputStream());
if(parse.parseJSON(response, "method").equals("login")) {
out.writeUTF(Login(response));
}
}
else {
out.writeUTF("Thank you for connecting to "
+ server.getLocalSocketAddress() + "\nGoodbye!");
}
server.close();
} catch (ParseException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
......@@ -82,7 +82,7 @@ public class Server extends Thread {
}catch(IOException e)
{
break;
}
}
}
}
}
}
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