Commit 1ce989a2 authored by Stephen's avatar Stephen
Browse files

Edited protocol, fixed sendfind

parent 5c393c17
......@@ -267,7 +267,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
}
catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
......@@ -275,8 +275,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -292,15 +294,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver register exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -316,15 +320,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver login exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -340,15 +346,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver inventory exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -365,7 +373,7 @@ public class TCPClient {
try {
System.out.println("mix mantap");
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
System.out.println("harusnya udah nambah");
} catch (IOException ex) {
System.out.println("error woi mix nya");
......@@ -374,8 +382,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -391,15 +401,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver map exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -415,15 +427,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver offer exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -439,15 +453,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver tradebox exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -463,15 +479,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver move exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -487,7 +505,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver field exception");
......@@ -495,8 +513,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -512,15 +532,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver tradebox exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -536,15 +558,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver send accept exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -560,7 +584,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver accept exception");
......@@ -568,8 +592,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -585,15 +611,17 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver find offer exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -609,7 +637,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver fetch item exception");
......@@ -617,8 +645,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -634,7 +664,7 @@ public class TCPClient {
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
outToServer.write(toSend.getBytes("UTF-8"));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver canceloffer exception");
......@@ -642,8 +672,10 @@ public class TCPClient {
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
byte b[] = new byte[4096];
inFromClient.read(b);
String response = new String(b, "UTF-8");
JSONObject responseJSON = new JSONObject(response);
closeConnection();
return responseJSON;
......@@ -665,7 +697,7 @@ public class TCPClient {
// System.out.print("Password : "); password = inFromUser.readLine();
// String toSend = registerJSON(username, password).toString();
//
// outToServer.writeBytes(toSend + '\n');
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
......@@ -686,7 +718,7 @@ public class TCPClient {
// System.out.print("Password : "); password = inFromUser.readLine();
// String toSend = loginJSON(username, password).toString();
//
// outToServer.writeBytes(toSend + '\n');
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
......@@ -711,7 +743,7 @@ public class TCPClient {
// if(command.equalsIgnoreCase("inventory")) {
// String toSend = inventoryJSON(token).toString();
//
// outToServer.writeBytes(toSend + '\n');
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
......@@ -738,7 +770,7 @@ public class TCPClient {
// System.out.print("Item 2 ID : "); int id2 = Integer.parseInt(inFromUser.readLine());
// String toSend = mixitemJSON(token, id1, id2).toString();
//
// outToServer.writeBytes(toSend + '\n');
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
......
......@@ -330,7 +330,7 @@ public class MySQLAccess {
int difference = resultSet.getInt(resultSet2.getString("item_name").toLowerCase().replace(' ', '_')) - n1;
if(difference >= 0) {
preparedStatement = connect.prepareStatement("INSERT INTO offer "
+ "VALUES(NULL, ?, ?, ?, ?, ?, ?, ?)");
+ "VALUES(NULL, ?, ?, ?, ?, ?, ?, '', 0, ?)");
preparedStatement.setInt(1, userID);
preparedStatement.setInt(2, offered_item);
preparedStatement.setInt(3, n1);
......@@ -657,4 +657,24 @@ public class MySQLAccess {
close();
return id;
}
public Host getHostByOfferToken(String offer_token)
{
open();
Host host = null;
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
host.ipAddress = resultSet.getString("ip");
host.port = resultSet.getInt("port");
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return host;
}
}
This diff is collapsed.
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