Client can now receive messages from clients
Added colored logging to the server
This commit is contained in:
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -10,16 +10,22 @@ public class Client {
|
|||||||
private DataOutputStream out;
|
private DataOutputStream out;
|
||||||
private DataInputStream in;
|
private DataInputStream in;
|
||||||
private static Scanner scanner;
|
private static Scanner scanner;
|
||||||
|
private static String name;
|
||||||
private boolean success;
|
private boolean success;
|
||||||
|
|
||||||
public Client(String ip, int port) {
|
public static final String ANSI_RESET = "\u001B[0m";
|
||||||
|
public static final String ANSI_CYAN = "\u001B[36m";
|
||||||
|
|
||||||
|
|
||||||
|
public Client(String ip, int port, String name) {
|
||||||
try {
|
try {
|
||||||
scanner = new Scanner(System.in);
|
scanner = new Scanner(System.in);
|
||||||
Socket serverSocket = new Socket(ip, port);
|
Socket serverSocket = new Socket(ip, port);
|
||||||
out = new DataOutputStream(serverSocket.getOutputStream());
|
out = new DataOutputStream(serverSocket.getOutputStream());
|
||||||
in = new DataInputStream(serverSocket.getInputStream());
|
in = new DataInputStream(serverSocket.getInputStream());
|
||||||
System.out.println(in.readUTF());
|
|
||||||
success = true;
|
success = true;
|
||||||
|
this.name = name;
|
||||||
|
System.out.println(name);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -27,9 +33,14 @@ public class Client {
|
|||||||
|
|
||||||
public void handshake() {
|
public void handshake() {
|
||||||
try {
|
try {
|
||||||
out.writeUTF("849465467842158");
|
out.writeInt(165313125);
|
||||||
out.flush();
|
out.flush();
|
||||||
success = in.readInt() == 200;
|
success = in.readInt() == 200;
|
||||||
|
if (success){
|
||||||
|
out.writeUTF(name);
|
||||||
|
System.out.printf(ANSI_CYAN + "You successfully connected to %s%n"+ANSI_RESET, in.readUTF());
|
||||||
|
}
|
||||||
|
System.out.println(success);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -41,7 +52,7 @@ public class Client {
|
|||||||
String message = scanner.nextLine();
|
String message = scanner.nextLine();
|
||||||
try {
|
try {
|
||||||
out.writeUTF(message);
|
out.writeUTF(message);
|
||||||
out.flush();
|
System.out.println(in.readInt());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -64,8 +75,16 @@ public class Client {
|
|||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName(){return name;}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Client client = new Client("localhost", 2589);
|
Client client;
|
||||||
|
if (args.length > 0) {
|
||||||
|
client = new Client("localhost", 2589, args[0]);
|
||||||
|
} else {
|
||||||
|
client = new Client("localhost", 2589, "GenericName");
|
||||||
|
}
|
||||||
client.handshake();
|
client.handshake();
|
||||||
client.oneSidedMessage();
|
client.oneSidedMessage();
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/server/logging/LogType.java
Normal file
7
src/server/logging/LogType.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package server.logging;
|
||||||
|
|
||||||
|
public enum LogType {
|
||||||
|
Log,
|
||||||
|
Message,
|
||||||
|
Error
|
||||||
|
}
|
||||||
39
src/server/logging/Logger.java
Normal file
39
src/server/logging/Logger.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package server.logging;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
|
public class Logger {
|
||||||
|
|
||||||
|
private static final String ANSI_RESET = "\u001B[0m";
|
||||||
|
private static final String ANSI_BLACK = "\u001B[30m";
|
||||||
|
private static final String ANSI_RED = "\u001B[31m";
|
||||||
|
private static final String ANSI_GREEN = "\u001B[32m";
|
||||||
|
private static final String ANSI_YELLOW = "\u001B[33m";
|
||||||
|
private static final String ANSI_BLUE = "\u001B[34m";
|
||||||
|
private static final String ANSI_PURPLE = "\u001B[35m";
|
||||||
|
private static final String ANSI_CYAN = "\u001B[36m";
|
||||||
|
private static final String ANSI_WHITE = "\u001B[37m";
|
||||||
|
|
||||||
|
public void printLog(String message, String name, LogType logType){
|
||||||
|
switch (logType){
|
||||||
|
case Log:
|
||||||
|
System.out.printf(ANSI_CYAN + "%s %s%n"+ANSI_RESET, new Timestamp(System.currentTimeMillis()), message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Error:
|
||||||
|
System.out.printf(ANSI_RED + "%s %s%n"+ ANSI_RESET, new Timestamp(System.currentTimeMillis()), message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Message:
|
||||||
|
System.out.printf(ANSI_WHITE + " %s %s %s%n" +ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.err.println("NO VALID LOGTYPE GIVEN");;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printLog(String message, LogType logType){
|
||||||
|
this.printLog(message, "", logType);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package server.networking;
|
package server.networking;
|
||||||
|
|
||||||
|
import server.logging.LogType;
|
||||||
|
import server.logging.Logger;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
@@ -9,8 +12,10 @@ import java.util.Scanner;
|
|||||||
public class Server {
|
public class Server {
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
private HashMap<Integer, Socket> clients;
|
private HashMap<Integer, Socket> clients;
|
||||||
|
private HashMap<Socket, String> clientNames;
|
||||||
private HashMap<Socket, DataOutputStream> outstreams;
|
private HashMap<Socket, DataOutputStream> outstreams;
|
||||||
private HashMap<Socket, DataInputStream> instreams;
|
private HashMap<Socket, DataInputStream> instreams;
|
||||||
|
private Logger logger;
|
||||||
private Scanner scanner;
|
private Scanner scanner;
|
||||||
private int requiredConnections;
|
private int requiredConnections;
|
||||||
|
|
||||||
@@ -18,10 +23,14 @@ public class Server {
|
|||||||
try {
|
try {
|
||||||
serverSocket = new ServerSocket(port);
|
serverSocket = new ServerSocket(port);
|
||||||
clients = new HashMap<>();
|
clients = new HashMap<>();
|
||||||
|
clientNames = new HashMap<>();
|
||||||
outstreams = new HashMap<>();
|
outstreams = new HashMap<>();
|
||||||
instreams = new HashMap<>();
|
instreams = new HashMap<>();
|
||||||
scanner = new Scanner(System.in);
|
scanner = new Scanner(System.in);
|
||||||
|
logger = new Logger();
|
||||||
requiredConnections = 2;
|
requiredConnections = 2;
|
||||||
|
|
||||||
|
logger.printLog("Server started successfully", LogType.Log);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -30,14 +39,13 @@ public class Server {
|
|||||||
public void connectClients(){
|
public void connectClients(){
|
||||||
try {
|
try {
|
||||||
int id = 0;
|
int id = 0;
|
||||||
System.out.printf("Waiting for %d clients to connect%n", requiredConnections);
|
logger.printLog(String.format("Waiting for %d clients to connect ...", requiredConnections), LogType.Log);
|
||||||
while(clients.size() < requiredConnections) {
|
while(clients.size() < requiredConnections) {
|
||||||
Socket momentaryClient = serverSocket.accept();
|
Socket momentaryClient = serverSocket.accept();
|
||||||
clients.put(id, momentaryClient);
|
clients.put(id, momentaryClient);
|
||||||
outstreams.put(momentaryClient, new DataOutputStream(momentaryClient.getOutputStream()));
|
outstreams.put(momentaryClient, new DataOutputStream(momentaryClient.getOutputStream()));
|
||||||
instreams.put(momentaryClient, new DataInputStream(momentaryClient.getInputStream()));
|
instreams.put(momentaryClient, new DataInputStream(momentaryClient.getInputStream()));
|
||||||
id++;
|
id++;
|
||||||
System.out.printf("networking.Client %d got connected%n", id);
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -47,57 +55,54 @@ public class Server {
|
|||||||
public void handshake(){
|
public void handshake(){
|
||||||
for (Socket client: clients.values()) {
|
for (Socket client: clients.values()) {
|
||||||
try {
|
try {
|
||||||
String handshakeValue = instreams.get(client).readUTF();
|
int handshakeValue = instreams.get(client).readInt();
|
||||||
if (handshakeValue.equals("849465467842158")) {
|
if (handshakeValue == 165313125) {
|
||||||
outstreams.get(client).writeInt(200);
|
outstreams.get(client).writeInt(200);
|
||||||
|
outstreams.get(client).flush();
|
||||||
|
clientNames.put(client, instreams.get(client).readUTF());
|
||||||
|
outstreams.get(client).writeUTF(serverSocket.getInetAddress().getHostAddress()+":"+serverSocket.getLocalPort());
|
||||||
|
outstreams.get(client).flush();
|
||||||
|
logger.printLog(String.format("%s got connected", clientNames.get(client)), LogType.Log);
|
||||||
} else {
|
} else {
|
||||||
outstreams.get(client).writeInt(403);
|
outstreams.get(client).writeInt(403);
|
||||||
|
outstreams.get(client).flush();
|
||||||
}
|
}
|
||||||
outstreams.get(client).flush();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (Socket client: clients.values()) {
|
|
||||||
try {
|
|
||||||
String response = instreams.get(client).readUTF();
|
|
||||||
System.out.println(response);
|
|
||||||
if (response.equals("1")){
|
|
||||||
outstreams.get(client).writeUTF("Connection confirmed :)");
|
|
||||||
outstreams.get(client).writeUTF("Send me a message ...");
|
|
||||||
System.out.println("Connection confirmed :)");
|
|
||||||
} else {
|
|
||||||
outstreams.get(client).writeUTF("Connection failed");
|
|
||||||
System.out.println("Connection failed");
|
|
||||||
client.close();
|
|
||||||
}
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getMessage(){
|
public void getMessages(){
|
||||||
for (Socket client: clients.values()) {
|
for (Socket client: clients.values()) {
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
String message = instreams.get(client).readUTF();
|
String message = instreams.get(client).readUTF();
|
||||||
System.out.println(message);
|
if (!message.equalsIgnoreCase("exit()")) {
|
||||||
if(message.equalsIgnoreCase("exit()"))
|
logger.printLog(message, clientNames.get(client), LogType.Message);
|
||||||
|
} else {
|
||||||
|
outstreams.get(client).writeInt(200);
|
||||||
|
logger.printLog(String.format("%s closed the connection",clientNames.get(client)), LogType.Log);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
outstreams.get(client).writeInt(200);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
try {
|
||||||
|
client.close();
|
||||||
|
} catch (IOException ioException) {
|
||||||
|
ioException.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Server server = new Server(2589);
|
Server server = new Server(2589);
|
||||||
System.out.println("networking.Server got started");
|
|
||||||
server.connectClients();
|
server.connectClients();
|
||||||
server.handshake();
|
server.handshake();
|
||||||
server.getMessage();
|
server.getMessages();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user