rt

An obligatory Java Project
Log | Files | Refs | LICENSE

commit d189b24ec42c186ef4b78f33674533a1e4d8da2d
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date:   Sat, 23 Nov 2019 02:22:21 +0100

Initial commit

Diffstat:
AMakefile | 19+++++++++++++++++++
Aacc.class | 0
Aacc.java | 47+++++++++++++++++++++++++++++++++++++++++++++++
Aclient.class | 0
Aclient.java | 46++++++++++++++++++++++++++++++++++++++++++++++
Aserver.class | 0
Aserver.java | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Autil.class | 0
Autil.java | 32++++++++++++++++++++++++++++++++
9 files changed, 252 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,19 @@ +all: server client util acc + +server: + javac server.java + +client: + javac client.java + +util: + javac util.java + +acc: + javac acc.java + +.PHONY: clean run + +clean: + rm server.class client.class util.class acc.class + diff --git a/acc.class b/acc.class Binary files differ. diff --git a/acc.java b/acc.java @@ -0,0 +1,47 @@ +package space.nocebo; + +import java.util.Random; + +public class acc { + private String uname; + private String pass; + private int score; + boolean online; + int gid; + + public acc(String n, String p) { + uname = n; + pass = p; + score = getRandomInt(); + online = true; + gid = 0; + } + + public String getUname() { + return uname; + } + + public String getPass() { + return pass; + } + + public int getScore() { + return score; + } + + public boolean getOnline() { + return online; + } + + public int getGid() { + return gid; + } + + private int getRandomInt() { + int res = 1; + Random r = new Random(); + while ((res = r.nextInt(5)) != 0) + return res; + return res; + } +}; diff --git a/client.class b/client.class Binary files differ. diff --git a/client.java b/client.java @@ -0,0 +1,46 @@ +package space.nocebo; + +import java.net.*; +import java.io.*; + +public class client { + public static void main(String[] args) { + int port = 1234; + String ip = "127.0.0.1"; + try { + Socket connection = new Socket(ip, port); + System.out.println("Connected! " + connection); + + DataInputStream inputStr = new DataInputStream( + new BufferedInputStream(connection.getInputStream())); + DataOutputStream outputStr = new DataOutputStream( + new BufferedOutputStream(connection.getOutputStream())); + + String serv_mes = ""; + String s = ""; + while (s.compareTo("q") != 0) { + serv_mes = inputStr.readUTF(); + System.out.print(serv_mes + "> "); + + BufferedReader stdin = new BufferedReader( + new InputStreamReader(System.in)); + if ((s = stdin.readLine()) != null && s.length() != 0) { + outputStr.writeUTF(s); + outputStr.flush(); + } + } + // kill the server + outputStr.writeUTF(s); + outputStr.flush(); + + // kill the client + outputStr.close(); + inputStr.close(); + + // close the connection + connection.close(); + } catch (IOException ex) { + System.out.println("Couldn't establish connection: " + ex); + } + } +} diff --git a/server.class b/server.class Binary files differ. diff --git a/server.java b/server.java @@ -0,0 +1,108 @@ +package space.nocebo; + +import java.net.*; +import java.io.*; +import java.util.*; +import space.nocebo.util; + +public class server { + + private ArrayList<acc> Accounts = new ArrayList<acc>(); + + private DataOutputStream outputStr; + + private int login(String m) { + String[] inp = m.split(" ", 2); + for (acc c : Accounts) + if (((inp[0].compareTo(c.getUname())) == 0) && + ((inp[1].compareTo(c.getPass())) ==0)) + return 1; + return 0; + } + + private void register(String m) { + String[] inp = m.split(" ", 2); + Accounts.add(new acc(inp[0], inp[1])); + } + + private void notifyClient(DataOutputStream str, String m) { + try { + str.writeUTF(m + "\n"); + str.flush(); + } catch (IOException ex) { + System.out.println("Connection error: " + ex); + System.exit(1); + } + } + + public static void main(String[] args) throws IOException { + try { + ServerSocket tcpserver = new ServerSocket(1234); + System.out.println("Wait for connections"); + Socket connection = tcpserver.accept(); + server serv = new server(); + System.out.println("TCP Client connected " + connection); + + DataInputStream inputStr = new DataInputStream( + new BufferedInputStream(connection.getInputStream())); + DataOutputStream outputStr = new DataOutputStream( + new BufferedOutputStream(connection.getOutputStream())); + + serv.notifyClient(outputStr, + "Welcome to RT\nSelect one:\n1.Register\n2.Signup"); + + String message = inputStr.readUTF(); + int val = 0; + while (message.compareTo("q") != 0) { // use 'q' to quit + if (util.isNumeric(message)) { + val = Integer.parseInt(message); + if (val == 1) { + System.out.println("in 1"); + outputStr.writeUTF("Enter your username and password" + + " in the format:" + + " username password\n"); + outputStr.flush(); + message = inputStr.readUTF(); + if (util.isValidCred(message)) { + serv.register(message); + serv.notifyClient(outputStr, + "Registration Successful\n" + + "Please press 2 to login"); + } + else + serv.notifyClient(outputStr, "Invalid Input"); + } else if (val == 2) { + System.out.println("in 2"); + outputStr.writeUTF("Kindly enter your preferred" + + " username and password" + + " in the format: username password\n"); + outputStr.flush(); + message = inputStr.readUTF(); + if (util.isValidCred(message)) { + if (serv.login(message) == 1) + serv.notifyClient(outputStr, "Login Successful"); + else + serv.notifyClient(outputStr, "Login Failed\n" + + "Please try again later"); + } + else + serv.notifyClient(outputStr, "Invalid input"); + } + } else { + serv.notifyClient(outputStr, "Invalid Input"); + } + System.out.println("TCP Client> " + message); + message = inputStr.readUTF(); + } + + inputStr.close(); + outputStr.close(); + connection.close(); + tcpserver.close(); + + } catch (IOException ex) { + System.out.println("Couldn't establish connection: " + ex); + } + + } +} diff --git a/util.class b/util.class Binary files differ. diff --git a/util.java b/util.java @@ -0,0 +1,32 @@ +package space.nocebo; + +import java.util.regex.PatternSyntaxException; + +public class util { + public static boolean isNumeric(String s) { + try { + int d = Integer.parseInt(s); + } catch (NumberFormatException | NullPointerException ex) { + return false; + } + return true; + } + + public static boolean isDouble(String s) { + try { + double d = Double.parseDouble(s); + } catch (NumberFormatException | NullPointerException ex) { + return false; + } + return true; + } + + public static boolean isValidCred(String s) { + try { + String[] a = s.split(" ", 2); + } catch (PatternSyntaxException ex) { + return false; + } + return true; + } +}