Moving client to another repo.
This commit is contained in:
parent
51f3d88a8e
commit
f6f9511537
@ -1,7 +1,6 @@
|
||||
ConsoleChat
|
||||
===========
|
||||
|
||||
This program was made as a personal project to learn Java network programming. The concept of it is rather simple, all it does is send messages to a server which, in turn, sends the message to everyone else (aside from some other commands added). Although the license is MIT, please consider also disclosing the source-code under an open-source license. It helps to give back to the community and help others just like this may have helped you.
|
||||
ConsoleChat-Server
|
||||
==================
|
||||
This is the _server-side_ to the __ConsoleChat__ project. This code takes care of handling commands and sending the information to all the other clients connected.
|
||||
|
||||
### Compiling
|
||||
To compile the source code, make sure you have JDK and Apache Ant installed. Then run the following command:
|
||||
|
14
build.xml
14
build.xml
@ -1,10 +1,9 @@
|
||||
<project name="ConsoleChat" basedir="." default="main" >
|
||||
<project name="ConsoleChat-Server" basedir="." default="main" >
|
||||
<property name="src.dir" value="src" />
|
||||
<property name="bin.dir" value="bin" />
|
||||
<property name="classes.dir" value="${bin.dir}/classes" />
|
||||
<property name="jar.dir" value="${bin.dir}/jar" />
|
||||
<property name="main-class-client" value="consolechat.client.Client" />
|
||||
<property name="main-class-server" value="consolechat.server.Server" />
|
||||
<property name="main-class" value="consolechat.server.Server" />
|
||||
|
||||
<target name="clean" >
|
||||
<delete dir="bin" />
|
||||
@ -17,14 +16,9 @@
|
||||
|
||||
<target name="jar" depends="compile" >
|
||||
<mkdir dir="${jar.dir}" />
|
||||
<jar destfile="${jar.dir}/${ant.project.name}-client.jar" basedir="${classes.dir}" >
|
||||
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" >
|
||||
<manifest>
|
||||
<attribute name="Main-Class" value="${main-class-client}" />
|
||||
</manifest>
|
||||
</jar>
|
||||
<jar destfile="${jar.dir}/${ant.project.name}-server.jar" basedir="${classes.dir}" >
|
||||
<manifest>
|
||||
<attribute name="Main-Class" value="${main-class-server}" />
|
||||
<attribute name="Main-Class" value="${main-class}" />
|
||||
</manifest>
|
||||
</jar>
|
||||
</target>
|
||||
|
@ -1,116 +0,0 @@
|
||||
package consolechat.client;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.lang.*;
|
||||
|
||||
/**
|
||||
* @author Nicolás A. Ortega
|
||||
* @copyright Nicolás A. Ortega
|
||||
* @license MIT
|
||||
* @year 2014
|
||||
*
|
||||
*/
|
||||
public class Client implements Runnable {
|
||||
private String version = "v1.0.1";
|
||||
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: consolechat-client [server] [port]");
|
||||
} else {
|
||||
new Client(args[0], Integer.parseInt(args[1]));
|
||||
}
|
||||
}
|
||||
|
||||
// Constructor method
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
// The run method containing the main loop
|
||||
public void run() {
|
||||
String uinput;
|
||||
while(thread != null) {
|
||||
try {
|
||||
uinput = console.readLine();
|
||||
if(uinput.equals("/clientVersion")) {
|
||||
System.out.println(version);
|
||||
} else {
|
||||
streamOut.writeUTF(uinput);
|
||||
streamOut.flush();
|
||||
}
|
||||
} catch(IOException e) {
|
||||
System.out.println("Sending error: " + e.getMessage());
|
||||
stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle messages
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Open and start all necessary threads
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// Stop and close all necessary threads
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
package consolechat.client;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
/**
|
||||
* @author Nicolás A. Ortega
|
||||
* @copyright Nicolás A. Ortega
|
||||
* @license MIT
|
||||
* @year 2014
|
||||
*
|
||||
* For details on the copyright, look at the COPYRIGHT file that came with
|
||||
* this program.
|
||||
*
|
||||
*/
|
||||
public class ClientThread extends Thread {
|
||||
private Socket socket = null;
|
||||
private Client client = null;
|
||||
private DataInputStream streamIn = null;
|
||||
private boolean run = false;
|
||||
|
||||
// Constructor
|
||||
public ClientThread(Client _client, Socket _socket) {
|
||||
client = _client;
|
||||
socket = _socket;
|
||||
open();
|
||||
start();
|
||||
}
|
||||
|
||||
// Open all necessary streams/threads
|
||||
public void open() {
|
||||
try {
|
||||
streamIn = new DataInputStream(socket.getInputStream());
|
||||
} catch(IOException e) {
|
||||
System.out.println("Error getting input stream: " + e);
|
||||
client.stop();
|
||||
}
|
||||
run = true;
|
||||
}
|
||||
|
||||
// Close the streams
|
||||
public void close() throws IOException {
|
||||
if(streamIn != null) { streamIn.close(); }
|
||||
run = false;
|
||||
}
|
||||
|
||||
// The run method which will be called every frame
|
||||
public void run() {
|
||||
while(run) {
|
||||
try {
|
||||
client.handle(streamIn.readUTF());
|
||||
} catch(IOException e) {
|
||||
System.out.println("Listening error: " + e.getMessage());
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user