scp-app

git clone git://git.codymlewis.com/scp-app.git
Log | Files | Refs | LICENSE

commit 5a9154c196ee11a4d5cfc4cf793fa84309fd0e74
parent c028bd1eea561d0b6058d4ce33ca0e41fb0617f2
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Fri, 17 Aug 2018 13:01:51 +1000

Got an accept and reject message system from the server

Diffstat:
MChatClient.java | 24+++++++++++-------------
MChatServer.java | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 72 insertions(+), 29 deletions(-)

diff --git a/ChatClient.java b/ChatClient.java @@ -28,19 +28,17 @@ public class ChatClient { */ public void run(String args[]) { try { - String hostName = args[0]; - int port = Integer.parseInt(args[1]); + String hostName = args.length > 0 ? args[0] : "localhost"; + int port = args.length > 1 ? Integer.parseInt(args[1]) : 3400; 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"); - } + 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()); + console.close(); } catch(UnknownHostException uhe) { System.err.println("Specified host does not exist"); } catch(IOException ioe) { @@ -68,7 +66,7 @@ public class ChatClient { */ 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", + "SCP CONNECT\nSERVERADDRESS %s\nSERVERPORT %d\nREQUESTCREATED %d\nUSERNAME \"%s\"\nSCP END", hostName, port, Instant.now().getEpochSecond(), username ); out.println(connectionString); diff --git a/ChatServer.java b/ChatServer.java @@ -20,6 +20,8 @@ public class ChatServer { private Socket cliSocket; private PrintWriter out; private BufferedReader in; + InetAddress hostInetAddress; + int port; private static final int BACKLOG = 1; // Max length for queue of messages public static void main(String args[]) { ChatServer cs = new ChatServer(); @@ -32,20 +34,38 @@ public class ChatServer { */ public void run(String args[]) { try { - InetAddress hostInetAddress = args.length > 0 ? InetAddress.getLocalHost() : InetAddress.getLocalHost(); - int port = args.length > 1 ? Integer.parseInt(args[1]) : 3400; + hostInetAddress = args.length > 0 ? InetAddress.getLocalHost() : InetAddress.getLocalHost(); + 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); System.out.println("Started server"); + hostConnection(welcomeMessage); + } catch(IOException ioe) { + System.err.println("Input/Output error"); + } + } + /** + * Host a client connectio + * @param welcomeMessage the welcome message sent to the client + */ + private void hostConnection(String welcomeMessage) throws IOException { + boolean disconnect = false; + while(!disconnect) { 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(); + if(username == "") { + cliSocket.close(); + System.out.println("Rejected client for time differential greater than 5"); + continue; + } + scpAccept(username); out.println(welcomeMessage); - } catch(IOException ioe) { - System.err.println("Input/Output error"); + System.out.println(String.format("User %s has connected to SCP", username)); + disconnect = true; } } /** @@ -70,7 +90,7 @@ public class ChatServer { } /** * Recieve a scp connection - * @return true on packet reception and parse + * @return Client's username */ private String clientConnect() throws IOException { String inLine; @@ -84,21 +104,47 @@ public class ChatServer { isScpConnect = true; } } else { - if(inLine.indexOf("SERVERADDRESS") > -1) { - // Validate - } else if(inLine.indexOf("SERVERPORT") > -1) { - // Validate - } else if(inLine.indexOf("REQUESTCREATED") > -1) { + 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(); + int timeDiff = findTimeDiff(requestTime); + if(timeDiff > 5) { + reject(timeDiff); + break; } } else if(inLine.indexOf("USERNAME") > -1) { - username = inLine.substring(inLine.indexOf(" ")); + username = inLine.substring(inLine.indexOf(" ") + 1); } } } return username; } -}- \ No newline at end of file + /** + * Find passes epoch time from specified time + * @param otherTime specified time + * @return The difference in times + */ + private int findTimeDiff(int otherTime) { + return Math.abs((int)Instant.now().getEpochSecond() - otherTime); + } + /** + * Reject client for taking too long + * @param timeDiff Difference in time of client request to server processing it + */ + private void reject(int timeDiff) { + out.println(String.format( + "SCP REJECT\nTIMEDIFFERENTIAL %d\nREMOTEADDRESS %s\nSCP END", + timeDiff, cliSocket.getLocalAddress().getHostAddress() + ) + ); + } + /** + * Send a SCP connect message to the client + * @param username user specified name + */ + private void scpAccept(String username) { + out.println(String.format( + "SCP ACCEPT\nUSERNAME %s\nCLIENTADDRESS %s\nCLIENTPORT %d\nSCP END" + ) + ); + } +}