Skip to content

Instantly share code, notes, and snippets.

@ahmedengu
Last active March 19, 2017 10:10
Show Gist options
  • Save ahmedengu/d473d734a7b57ef1381abd0ed64a605b to your computer and use it in GitHub Desktop.
Save ahmedengu/d473d734a7b57ef1381abd0ed64a605b to your computer and use it in GitHub Desktop.
Multithreaded client-server with oxford dictionary api, JSON parsing using json-simple: https://code.google.com/archive/p/json-simple/downloads
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
/**
* Created by ahmedengu.
*/
public class Client {
public static void main(String[] args) {
try {
Socket client = new Socket("localhost", 1234);
System.out.println("Just connected to " + client.getRemoteSocketAddress());
Scanner scanner = new Scanner(System.in);
DataOutputStream out = new DataOutputStream(client.getOutputStream());
DataInputStream in = new DataInputStream(client.getInputStream());
System.out.println("Write a word[-1 to exit]:");
String read = scanner.nextLine();
out.writeUTF(read);
while (!read.contains("-1")) {
System.out.println(in.readUTF());
System.out.println("Write a word[-1 to exit]:");
read = scanner.nextLine();
out.writeUTF(read);
}
client.close();
System.out.println("Disconnected");
} catch (Exception e) {
e.printStackTrace();
}
}
}
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
/**
* Created by ahmedengu.
*/
public class Oxford {
public static Oxford instance;
private Oxford() {
}
public static Oxford getInstance() {
if (instance == null) {
synchronized (Oxford.class) {
if (instance == null)
instance = new Oxford();
}
}
return instance;
}
public String dictionary(String word) {
JSONParser parser = new JSONParser();
String ret = "404";
try {
final String result = getRequest(buildURL(word));
final Object parse = parser.parse(result);
ret = (String) ((JSONArray) ((JSONObject) ((JSONArray) ((JSONObject) ((JSONArray) ((JSONObject) ((JSONArray) ((JSONObject) ((JSONArray) ((JSONObject) parse).get("results")).get(0)).get("lexicalEntries")).get(0)).get("entries")).get(0)).get("senses")).get(0)).get("definitions")).get(0);
} catch (Exception e) {
System.out.println(e);
}
return ret;
}
private String buildURL(final String word) {
final String language = "en";
final String word_id = word.toLowerCase(); //word id is case sensitive and lowercase is required
return "https://od-api.oxforddictionaries.com:443/api/v1/entries/" + language + "/" + word_id;
}
private String getRequest(String link) {
final String app_id = "2f0d7a3a";
final String app_key = "5215f3131a55b7cdabb65c4193c63d49";
try {
URL url = new URL(link);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestProperty("app_id", app_id);
urlConnection.setRequestProperty("app_key", app_key);
// read the output from the server
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
return e.toString();
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread {
Socket cSocket;
static int counter;
Server(Socket cSocket) {
this.cSocket = cSocket;
}
public static void main(String args[]) throws Exception {
ServerSocket sSock = new ServerSocket(1234);
System.out.println("Listening");
while (true) {
Socket sock = sSock.accept();
new Thread(new Server(sock)).start();
}
}
public void run() {
try {
System.out.println("Connected " + cSocket.getRemoteSocketAddress() + ", Counter: " + ++counter);
DataInputStream in = new DataInputStream(cSocket.getInputStream());
DataOutputStream out = new DataOutputStream(cSocket.getOutputStream());
String read = in.readUTF();
while (!read.contains("-1")) {
out.writeUTF(Oxford.getInstance().dictionary(read));
read = in.readUTF();
}
System.out.println("Disconnected " + cSocket.getRemoteSocketAddress() + ", Counter: " + --counter);
cSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment