Skip to content

Instantly share code, notes, and snippets.

@skayred
Created October 18, 2011 14:11
Show Gist options
  • Save skayred/1295511 to your computer and use it in GitHub Desktop.
Save skayred/1295511 to your computer and use it in GitHub Desktop.
Client code
package com.skalar;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public Client(String host, int port) {
this.port = port;
hostname = host;
}
public void connect() {
try {
fromServer = new Socket(hostname,3000);
in = new
BufferedReader(new
InputStreamReader(fromServer.getInputStream()));
out = new
PrintWriter(fromServer.getOutputStream(),true);
inu = new
BufferedReader(new InputStreamReader(System.in));
} catch (IOException exc) {
exc.printStackTrace();
}
}
public void startCalculation() {
String fuser,fserver;
try {
while ((fuser = inu.readLine())!=null) {
out.println(fuser);
fserver = in.readLine();
System.out.println(fserver);
if (fuser.equalsIgnoreCase("close")) {
break;
}
if (fuser.equalsIgnoreCase("exit")) {
break;
}
}
out.close();
in.close();
inu.close();
fromServer.close();
} catch (IOException exc) {
exc.printStackTrace();
}
}
public static void main(String[] args) {
Client client = new Client("localhost", 3000);
client.connect();
client.startCalculation();
}
private Socket fromServer;
private String hostname;
private int port;
private BufferedReader in;
private PrintWriter out;
private BufferedReader inu;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment