Skip to content

Commit bbf7e27

Browse files
committed
Changes to Server class
1 parent b95c55f commit bbf7e27

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
package com.company;
22

3+
import components.ComponentManager;
4+
import networking.Server;
5+
36
public class Main {
47
public static void main(String[] args) throws Exception {
58
ConfigurationManager configurationManager = new ConfigurationManager();
9+
ComponentManager componentManager = new ComponentManager();
10+
Server server = new Server(configurationManager.getPort(), configurationManager.getKeyword());
11+
12+
System.out.println("Connection Information");
13+
System.out.println("Local connection IP: " + configurationManager.getLocalIp() + " Port: " + configurationManager.getPort());
14+
System.out.println("Keyword: " + configurationManager.getKeyword());
15+
server.start();
16+
17+
while (server.isConnected())
18+
{
19+
20+
}
21+
22+
23+
24+
625

726
}
827
}

RemoteMonitor Desktop/src/main/java/networking/Server.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ public Server(int port, String connectionKeyword) throws IOException
1818
connectionSocket = new ServerSocket(listeningPort);
1919
}
2020

21-
public void start() throws IOException
21+
public boolean start() throws IOException
2222
{
2323
listener = connectionSocket.accept();
2424
connectedIp = listener.getRemoteSocketAddress().toString();
25-
System.out.println(connectedIp);
2625
writer = new PrintWriter(listener.getOutputStream(), true);
2726
reader = new BufferedReader(new InputStreamReader(listener.getInputStream()));
27+
//Will keep looping the function till a client connects with a correct keyword
28+
if (!checkAuthed())
29+
{
30+
System.out.println(connectedIp + " tried connecting with a wrong keyword");
31+
listener.close();
32+
return start();
33+
}
34+
else
35+
{
36+
return true;
37+
}
2838
}
2939

3040
public Boolean write(String json) throws IOException {
@@ -44,13 +54,21 @@ public Boolean write(String json) throws IOException {
4454
public Boolean checkAuthed() throws IOException
4555
{
4656
String input = reader.readLine();
47-
if (input.equals(keyword)){
48-
isAuthenticated = true;
57+
if (input != null){
58+
if (input.equals(keyword))
59+
{
60+
isAuthenticated = true;
61+
}
4962
}
5063
return isAuthenticated;
5164
}
5265

5366
public String getConnectedIp() {
5467
return connectedIp;
5568
}
69+
70+
public Boolean isConnected()
71+
{
72+
return listener.isConnected();
73+
}
5674
}
823 Bytes
Binary file not shown.
291 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)