Commit 044d1698 authored by adwisatya's avatar adwisatya
Browse files

Exception remoed

parent 55fa3759
......@@ -12,7 +12,9 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.*;
import java.security.MessageDigest;
......@@ -61,7 +63,7 @@ public class Client extends Thread{
/* Testing database */
clientSentence = "";
char[] buffer = new char[MAX_BUFFER];
while(!clientSentence.equals("dead")){
while(true){
try{
fromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
toClient = new DataOutputStream(connectionSocket.getOutputStream());
......@@ -69,22 +71,41 @@ public class Client extends Thread{
clientSentence = new String(buffer);
checkCommand(clientSentence);
}catch(IOException e){
try {
connectionSocket.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(e);
} catch (NoSuchAlgorithmException ex) {
try {
connectionSocket.close();
} catch (IOException ex1) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex1);
}
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (JSONException ex) {
try {
connectionSocket.close();
} catch (IOException ex1) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex1);
}
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void sendToClient(String tmpStr) throws IOException{
new DataOutputStream(connectionSocket.getOutputStream()).writeBytes(tmpStr);
// new DataOutputStream(connectionSocket.getOutputStream()).writeBytes(tmpStr);
// new DataOutputStream(connectionSocket.getOutputStream());
// OutputStream output = connectionSocket.getOutputStream();
PrintStream out = new PrintStream(connectionSocket.getOutputStream());
out.print(tmpStr);
}
public void checkCommand(String cmd) throws IOException, NoSuchAlgorithmException, JSONException {
cmdJoin();
System.out.println("Request: " + cmd);
//if(isJSONValid(cmd)){
if(isJSONValid(cmd)){
switch (JSONtoString("method",cmd)){
case "getServer":
System.out.println(database.getServers().toString());
......@@ -151,7 +172,7 @@ public class Client extends Thread{
cmdCancelOffer(JSONtoString("token",cmd), JSONtoString("offer_token",cmd));
break;
}
//}
}
}
public String JSONtoString(String param, String JSON) throws JSONException{
......@@ -216,7 +237,6 @@ public class Client extends Thread{
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
sendToClient(obj.toString());
System.out.println(obj.toString());
}
public void cmdLogin(String username, String password) throws IOException, NoSuchAlgorithmException, JSONException{
......
......@@ -42,7 +42,6 @@ public class Database {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
rs = statement.executeUpdate();
System.out.println(rs);
statement.close();
conn.close();
}catch(SQLException e){
......
......@@ -30,7 +30,7 @@ public class TCPServer {
mainSocket = new ServerSocket(portNumber);
System.out.println("Start listening...");
cmdJoin();
while(true){
while(!mainSocket.isClosed()){
Socket connectionSocket = mainSocket.accept();
if(connectionSocket != null){
Client client = new Client(connectionSocket);
......
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