Skip to content

Instantly share code, notes, and snippets.

@MojamojaK
Last active April 16, 2018 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MojamojaK/b6685ed5bf665bf3558da92b5c41aa5b to your computer and use it in GitHub Desktop.
Save MojamojaK/b6685ed5bf665bf3558da92b5c41aa5b to your computer and use it in GitHub Desktop.
// ClientCommunication without thread
import java.io.*;
import java.net.*;
import java.util.LinkedList;
public class ClientCommunication{
private InetAddress addr = InetAddress.getByName("localhost");
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private LinkedList<Character> inbox = new LinkedList<>();
private boolean stopped = true;
ClientCommunication (String args[]) throws IOException{
if (args.length > 0) addr = InetAddress.getByName(args[0]);
try {
System.out.println("addr = " + addr);
socket = new Socket(addr, 8080);
System.out.println("socket = " + socket);
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
stopped = false;
} catch (IOException e) {
e.printStackTrace();
}
}
public void update() {
try {
while (in.ready()) {
appendInbox((char)in.read());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
try {
stopped = true;
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private synchronized void appendInbox(char message) {
inbox.offer(message);
}
public int inboxSize() {
return inbox.size();
}
public char getMessage() {
return inbox.poll();
}
public synchronized void sendMessage(char message) {
out.print(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment