consolechat-server/src-client/Client.java

116 lines
2.9 KiB
Java
Raw Normal View History

2014-05-04 18:55:37 +00:00
import java.net.*;
import java.io.*;
import java.lang.*;
/**
* @author Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
* For details on the copyright, look at the COPYRIGHT file that came with
* this program.
*
*/
public class Client implements Runnable {
private String version = "v1.0";
private Socket socket = null;
private ClientThread cThread = null;
private DataOutputStream streamOut = null;
private BufferedReader console = null;
private Thread thread = null;
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("Usage: java Client [server] [port]");
} else {
2014-05-07 15:34:45 +00:00
new Client(args[0], Integer.parseInt(args[1]));
2014-05-04 18:55:37 +00:00
}
}
2014-05-07 15:34:45 +00:00
// Constructor method
2014-05-04 18:55:37 +00:00
public Client(String server, int port) {
System.out.println("ConsoleChat client " + version + " Copyright (C) 2014 Nicolás A. Ortega\n" +
"This program comes with ABSOLUTELY NO WARRANTY; details in WARRANTY file.\n" +
"This is free software, and you are welcome to redistribute it\n" +
"under certain conditions; details in LICENSE file.\n");
try {
// Create a new socket connection
System.out.println("Connecting to server...");
socket = new Socket(server, port);
System.out.println("Connected!");
start();
} catch(UnknownHostException uhe) {
System.out.println("Host unknown: " + uhe.getMessage());
} catch(IOException e) {
System.out.println("Unknown exception: " + e.getMessage());
}
}
2014-05-07 15:34:45 +00:00
// The run method containing the main loop
2014-05-04 18:55:37 +00:00
public void run() {
String uinput;
2014-05-04 18:55:37 +00:00
while(thread != null) {
try {
uinput = console.readLine();
if(uinput.equals("/clientVersion")) {
System.out.println(version);
} else {
streamOut.writeUTF(uinput);
streamOut.flush();
}
2014-05-04 18:55:37 +00:00
} catch(IOException e) {
System.out.println("Sending error: " + e.getMessage());
stop();
}
}
}
2014-05-07 15:34:45 +00:00
// Handle messages
2014-05-04 18:55:37 +00:00
public void handle(String msg) {
if(msg.equals("/quit")) {
System.out.println("Goodbye bye. Press RETURN to exit...");
stop();
} else {
System.out.println(msg);
if(msg.length() > 6 && msg.substring(0, 5).equals("Kick:")) {
stop();
}
}
}
2014-05-07 15:34:45 +00:00
// Open and start all necessary threads
2014-05-04 18:55:37 +00:00
private void start() throws IOException {
console = new BufferedReader(new InputStreamReader(System.in));
streamOut = new DataOutputStream(socket.getOutputStream());
if(thread == null) {
cThread = new ClientThread(this, socket);
thread = new Thread(this);
thread.start();
}
}
2014-05-07 15:34:45 +00:00
// Stop and close all necessary threads
2014-05-04 18:55:37 +00:00
public void stop() {
if(thread != null) {
thread.interrupt();
thread = null;
}
try {
if(console != null) { console.close(); }
if(streamOut != null) { streamOut.close(); }
if(socket != null) { socket.close(); }
} catch(IOException e) {
System.out.println("Error closing...");
}
try {
cThread.close();
} catch(IOException e) {
System.out.println("Error closing the thread: " + e);
}
cThread.interrupt();
}
}