Skip to content
Snippets Groups Projects
Commit 10d50813 authored by Hendro Triokta Brianto's avatar Hendro Triokta Brianto
Browse files

bisa menangani lost connection

parent d0e40b07
Branches master
No related merge requests found
No preview for this file type
......@@ -19,6 +19,8 @@ import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.json.JSONArray;
......@@ -36,8 +38,8 @@ public class Connection {
private DataOutputStream toClient;
String response;
String request;
private static String URL = "167.205.32.46";
private static int defPort = 8025;
private static String URL = "localhost";
private static int defPort = 6789;
// private static String URL;
// private static int defPort;
......@@ -46,29 +48,37 @@ public class Connection {
// connectionSocket = c;
// }
public String sendRequest(String JSON) throws IOException, JSONException{
miniSocket = new Socket(URL,defPort);
char[] buffer = new char[1024];
JSONObject obj = new JSONObject(JSON);
sendToClient(obj.toString());
// PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(miniSocket.getOutputStream())));
public String sendRequest(String JSON){
try {
miniSocket = new Socket(URL,defPort);
char[] buffer = new char[1024];
JSONObject obj = new JSONObject(JSON);
sendToClient(obj.toString());
// PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(miniSocket.getOutputStream())));
// out.print(obj);
// //out.print();
// out.flush();
fromClient = new BufferedReader(new InputStreamReader(miniSocket.getInputStream()));
fromClient.read(buffer);
clientSentence = new String(buffer);
fromClient = new BufferedReader(new InputStreamReader(miniSocket.getInputStream()));
fromClient.read(buffer);
clientSentence = new String(buffer);
// InputStream dataInputStream = new DataInputStream(miniSocket.getInputStream());
// InputStreamReader inputStreamReader = new InputStreamReader(dataInputStream);
// BufferedReader in = new BufferedReader(inputStreamReader);
// in.read(buffer);
// response = new String(buffer);
// in.close();
return clientSentence;
return clientSentence;
} catch (IOException ex) {
//Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
return "{\"status\": \"error\"}";
} catch (JSONException ex) {
//Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
return "{\"status\": \"error\"}";
}
}
public void setToken(String JSONString){
......
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