commit c028bd1eea561d0b6058d4ce33ca0e41fb0617f2
parent 1c569e1fa432adc0131e6f6c1934ef856fbb8879
Author: Cody Lewis <luxdotsugi@gmail.com>
Date: Wed, 15 Aug 2018 21:03:03 +1000
Step 1: SCP works
Diffstat:
M | ChatClient.java | | | 70 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- |
M | ChatServer.java | | | 82 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- |
2 files changed, 122 insertions(+), 30 deletions(-)
diff --git a/ChatClient.java b/ChatClient.java
@@ -1,3 +1,11 @@
+import java.util.Scanner;
+import java.time.Instant;
+import java.io.BufferedReader;
+import java.io.PrintWriter;
+import java.io.InputStreamReader;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.io.IOException;
/**
* ChatClient.java - SENG3400A1
* A socket based half duplex chat client
@@ -5,31 +13,65 @@
* @author Cody Lewis
* @since 2018-08-10
*/
-import java.net.Socket;
-import java.net.UnknownHostException;
-import java.io.IOException;
public class ChatClient {
private Socket socket;
+ private PrintWriter out;
+ private BufferedReader in;
public static void main(String args[]) {
ChatClient cc = new ChatClient();
cc.run(args);
System.exit(0);
}
+ /**
+ * Main flow of the program
+ * @param args arguments sent in with the program
+ */
public void run(String args[]) {
- String hostName = args[0];
- int port = Integer.parseInt(args[1]);
- System.out.println(String.format("Connecting to %s:%d", hostName, port));
- connectToServer(hostName, port);
- System.out.println("Connected to server");
- }
- private boolean connectToServer(String hostName, int port) {
try {
- socket = new Socket(hostName, port);
- return true;
+ String hostName = args[0];
+ int port = Integer.parseInt(args[1]);
+ System.out.println(String.format("Connecting to %s:%d", hostName, port));
+ if(connectToServer(hostName, port)) {
+ System.out.println("Connected to server");
+ Scanner console = new Scanner(System.in);
+ System.out.print("Input a username: ");
+ String username = console.next();
+ scpConnect(hostName, port, username);
+ System.out.println(in.readLine());
+ } else {
+ System.err.println("Failed to connect to the server");
+ }
} catch(UnknownHostException uhe) {
- return false;
+ System.err.println("Specified host does not exist");
} catch(IOException ioe) {
- return false;
+ System.err.println("Input/Output error");
}
}
+ /**
+ * Connect to the server
+ * @param hostName the name of the server
+ * @param port the port number the server is running on
+ * @return true on successful connection
+ */
+ private boolean connectToServer(String hostName, int port) throws UnknownHostException, IOException {
+ socket = new Socket(hostName, port);
+ out = new PrintWriter(socket.getOutputStream(), true);
+ in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ return true;
+ }
+ /**
+ * Send the SCP connection packet
+ * @param hostName the name of the server
+ * @param port the port number the server is running on
+ * @param username client specified username
+ * @return true on packet send
+ */
+ private boolean scpConnect(String hostName, int port, String username) {
+ String connectionString = String.format(
+ "SCP CONNECT\nSERVERADDRESS %s\nSERVERPORT %d\nREQUESTCREATED %d\nUSERNAME %s\nSCP END",
+ hostName, port, Instant.now().getEpochSecond(), username
+ );
+ out.println(connectionString);
+ return true;
+ }
}
\ No newline at end of file
diff --git a/ChatServer.java b/ChatServer.java
@@ -1,9 +1,13 @@
+import java.util.Scanner;
+import java.time.Instant;
+import java.io.BufferedReader;
+import java.io.PrintWriter;
+import java.io.InputStreamReader;
+import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
-import java.util.Scanner;
import java.net.InetAddress;
import java.net.UnknownHostException;
-import java.io.IOException;
/**
* ChatServer.java - Seng3400A1
* A socket based half duplex chat server
@@ -14,6 +18,8 @@ import java.io.IOException;
public class ChatServer {
private ServerSocket serverSocket;
private Socket cliSocket;
+ private PrintWriter out;
+ private BufferedReader in;
private static final int BACKLOG = 1; // Max length for queue of messages
public static void main(String args[]) {
ChatServer cs = new ChatServer();
@@ -25,30 +31,74 @@ public class ChatServer {
* @param args The arguments sent in with the program
*/
public void run(String args[]) {
- InetAddress hostInetAddress;
try {
- hostInetAddress = InetAddress.getLocalHost();
- int port = Integer.parseInt(args[1]);
+ InetAddress hostInetAddress = args.length > 0 ? InetAddress.getLocalHost() : InetAddress.getLocalHost();
+ int port = args.length > 1 ? Integer.parseInt(args[1]) : 3400;
+ String welcomeMessage = args.length > 2 ? args[2] : "Welcome to SCP";
System.out.println(String.format("Starting server on %s:%d", hostInetAddress.getHostAddress(), port));
startSocket(hostInetAddress, port);
- } catch(UnknownHostException uhe) {
- System.err.println("That host does not exist");
+ System.out.println("Started server");
+ System.out.println("Waiting for client to connect");
+ acceptClient();
+ System.out.println("Client successfully connected");
+ System.out.println("Waiting for client to SCP connect");
+ String username = clientConnect();
+ out.println(welcomeMessage);
+ } catch(IOException ioe) {
+ System.err.println("Input/Output error");
}
}
/**
* Start a server socket
* @param hostAddress The ip address for the host server
* @param port The port to run the server on
- * @return True on successful start else false
+ * @return True on successful start
*/
- private boolean startSocket(InetAddress hostAddress, int port) {
- try {
- serverSocket = new ServerSocket(port, BACKLOG, hostAddress);
- cliSocket = serverSocket.accept();
- return true;
- } catch(IOException ioe) {
- System.err.println("No client connected");
- return false;
+ private boolean startSocket(InetAddress hostAddress, int port) throws IOException {
+ serverSocket = new ServerSocket(port, BACKLOG, hostAddress);
+ return true;
+ }
+ /**
+ * Accept a client into the server
+ * @return True on client connection
+ */
+ private boolean acceptClient() throws IOException {
+ cliSocket = serverSocket.accept();
+ out = new PrintWriter(cliSocket.getOutputStream(), true);
+ in = new BufferedReader(new InputStreamReader(cliSocket.getInputStream()));
+ return true;
+ }
+ /**
+ * Recieve a scp connection
+ * @return true on packet reception and parse
+ */
+ private String clientConnect() throws IOException {
+ String inLine;
+ boolean isScpConnect = false;
+ String username = "";
+ while((inLine = in.readLine()).compareTo("SCP END") != 0) {
+ if(!isScpConnect) {
+ if(inLine.indexOf("SCP CONNECT") < 0) {
+ throw new IOException();
+ } else {
+ isScpConnect = true;
+ }
+ } else {
+ if(inLine.indexOf("SERVERADDRESS") > -1) {
+ // Validate
+ } else if(inLine.indexOf("SERVERPORT") > -1) {
+ // Validate
+ } else if(inLine.indexOf("REQUESTCREATED") > -1) {
+ int requestTime = Integer.parseInt(inLine.substring(inLine.indexOf(" ") + 1));
+ int currentTime = (int)Instant.now().getEpochSecond();
+ if(!(requestTime >= currentTime - 5 && requestTime <= currentTime + 5)) {
+ throw new IOException();
+ }
+ } else if(inLine.indexOf("USERNAME") > -1) {
+ username = inLine.substring(inLine.indexOf(" "));
+ }
+ }
}
+ return username;
}
}
\ No newline at end of file