Initial commit
This commit is contained in:
3
Java/TCPServerAPI/.idea/.gitignore
generated
vendored
Normal file
3
Java/TCPServerAPI/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
8
Java/TCPServerAPI/.idea/artifacts/NetworkAPI.xml
generated
Normal file
8
Java/TCPServerAPI/.idea/artifacts/NetworkAPI.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<component name="ArtifactManager">
|
||||
<artifact type="jar" name="NetworkAPI">
|
||||
<output-path>$PROJECT_DIR$/out/artifacts/NetworkAPI</output-path>
|
||||
<root id="archive" name="NetworkAPI.jar">
|
||||
<element id="module-output" name="TCPServerAPI" />
|
||||
</root>
|
||||
</artifact>
|
||||
</component>
|
||||
6
Java/TCPServerAPI/.idea/discord.xml
generated
Normal file
6
Java/TCPServerAPI/.idea/discord.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
</component>
|
||||
</project>
|
||||
6
Java/TCPServerAPI/.idea/misc.xml
generated
Normal file
6
Java/TCPServerAPI/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
Java/TCPServerAPI/.idea/modules.xml
generated
Normal file
8
Java/TCPServerAPI/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/TCPServerAPI.iml" filepath="$PROJECT_DIR$/TCPServerAPI.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
11
Java/TCPServerAPI/TCPServerAPI.iml
Normal file
11
Java/TCPServerAPI/TCPServerAPI.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
BIN
Java/TCPServerAPI/out/production/TCPServerAPI/Test.class
Normal file
BIN
Java/TCPServerAPI/out/production/TCPServerAPI/Test.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
35
Java/TCPServerAPI/src/de/craftix/Logger.java
Normal file
35
Java/TCPServerAPI/src/de/craftix/Logger.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package de.craftix;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class Logger {
|
||||
protected String applicationName;
|
||||
|
||||
public Logger(String applicationName) {
|
||||
this.applicationName = applicationName;
|
||||
}
|
||||
|
||||
public void info(String message) {
|
||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
System.out.println("[" + dtf.format(now) + "] [INFO] [" + applicationName + "] " + message);
|
||||
}
|
||||
|
||||
public void warning(String message) {
|
||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
System.out.println("\u001B[33m" + "[" + dtf.format(now) + "] [WARNING] [" + applicationName + "] " + message);
|
||||
}
|
||||
|
||||
public void error(String message) {
|
||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
System.err.println("[" + dtf.format(now) + "] [ERROR] [" + applicationName + "] " + message);
|
||||
}
|
||||
|
||||
public static void globalInfo(String message) { new Logger("Global").info(message); }
|
||||
public static void globalWarning(String message) { new Logger("Global").warning(message); }
|
||||
public static void globalError(String message) { new Logger("Global").error(message); }
|
||||
|
||||
}
|
||||
89
Java/TCPServerAPI/src/de/craftix/client/Client.java
Normal file
89
Java/TCPServerAPI/src/de/craftix/client/Client.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package de.craftix.client;
|
||||
|
||||
import de.craftix.Logger;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Client {
|
||||
protected InetSocketAddress serverAddress;
|
||||
protected InetSocketAddress clientAddress;
|
||||
protected Logger log;
|
||||
protected ServerSocket clientServer;
|
||||
|
||||
public Client(String server, int port, int localPort) {
|
||||
try {
|
||||
serverAddress = new InetSocketAddress(server, port);
|
||||
clientAddress = new InetSocketAddress(Inet4Address.getLocalHost().getHostAddress(), localPort);
|
||||
clientServer = new ServerSocket(localPort);
|
||||
log = new Logger("Client");
|
||||
startServerThread();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public Object sendObject(Object data, boolean answer) {
|
||||
if (serverAddress == null) return null;
|
||||
try {
|
||||
log.info("Connecting to Server... [" + serverAddress.getHostName() + ":" + serverAddress.getPort() + "]");
|
||||
Socket server = new Socket();
|
||||
server.connect(serverAddress, 10000);
|
||||
log.info("Server connected");
|
||||
|
||||
log.info("Sending data...");
|
||||
ObjectOutputStream stream = new ObjectOutputStream(new BufferedOutputStream(server.getOutputStream()));
|
||||
stream.writeObject(data);
|
||||
stream.flush();
|
||||
log.info("Data send");
|
||||
|
||||
Object out = null;
|
||||
if (answer) {
|
||||
ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(server.getInputStream()));
|
||||
out = input.readObject();
|
||||
input.close();
|
||||
log.info("Data received");
|
||||
}
|
||||
|
||||
stream.close();
|
||||
server.close();
|
||||
log.info("Task completed");
|
||||
|
||||
return out;
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
private void startServerThread() {
|
||||
if (clientServer == null) return;
|
||||
new Thread(() -> {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
log.info("Awaiting Server connection...");
|
||||
Socket client = clientServer.accept();
|
||||
log.info("Server connected [" + client.getRemoteSocketAddress() + "]");
|
||||
ObjectInputStream stream = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
|
||||
Object data = stream.readObject();
|
||||
Object answer = onServerMessage(client, data);
|
||||
if (answer != null) {
|
||||
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
|
||||
out.writeObject(answer);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
log.info("Data processed");
|
||||
stream.close();
|
||||
client.close();
|
||||
log.info("Task completed");
|
||||
} catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
protected Object onServerMessage(Socket server, Object data) { return null; }
|
||||
|
||||
}
|
||||
53
Java/TCPServerAPI/src/de/craftix/client/SqlHelper.java
Normal file
53
Java/TCPServerAPI/src/de/craftix/client/SqlHelper.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package de.craftix.client;
|
||||
|
||||
import de.craftix.server.MySQL;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class SqlHelper {
|
||||
|
||||
public static class Data {
|
||||
public String type;
|
||||
public String name;
|
||||
public int length;
|
||||
|
||||
public Data(String name, String type, int length) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public Data(String name, Type type, int length) {
|
||||
this.name = name;
|
||||
this.type = type.name;
|
||||
this.length = length;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
NUMBER("INT"),
|
||||
TEXT("VARCHAR");
|
||||
|
||||
public final String name;
|
||||
Type(String name) { this.name = name; }
|
||||
}
|
||||
|
||||
public static void createTable(MySQL con, String name, Data... variables) {
|
||||
String qry = "CREATE TABLE IF NOT EXISTS " + name + " (";
|
||||
for (Data var : variables) {
|
||||
if (variables[variables.length - 1].equals(var)) qry += var.name + " " + var.type + "(" + var.length + "))";
|
||||
else qry += var.name + " " + var.type + "(" + var.length + "), ";
|
||||
}
|
||||
con.insert(qry);
|
||||
}
|
||||
|
||||
public static ResultSet getDataset(MySQL con, String db) {
|
||||
return con.getData("SELECT * FROM " + db);
|
||||
}
|
||||
|
||||
public static ResultSet getDataset(MySQL con, String db, String var, Object value) {
|
||||
if (value instanceof String) return con.getData("SELECT * FROM " + db + " WHERE " + var + " = \"" + value + "\"");
|
||||
return con.getData("SELECT * FROM " + db + " WHERE " + var + " = " + value.toString());
|
||||
}
|
||||
|
||||
}
|
||||
76
Java/TCPServerAPI/src/de/craftix/server/MySQL.java
Normal file
76
Java/TCPServerAPI/src/de/craftix/server/MySQL.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import de.craftix.Logger;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class MySQL {
|
||||
|
||||
public String server;
|
||||
public int port = 3306;
|
||||
public String database;
|
||||
public String username;
|
||||
public String password;
|
||||
|
||||
protected Connection con;
|
||||
protected Logger log;
|
||||
|
||||
public MySQL(String server, String database, String username, String password) {
|
||||
this.server = server;
|
||||
this.database = database;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
log = new Logger("MySQL");
|
||||
}
|
||||
|
||||
public MySQL(String server, int port, String database, String username, String password) {
|
||||
this.server = server;
|
||||
this.port = port;
|
||||
this.database = database;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
log = new Logger("MySQL");
|
||||
}
|
||||
|
||||
public boolean connect() {
|
||||
if (con != null) return true;
|
||||
String connection = "jdbc:mysql://" + server + ":" + port + "/" + database;
|
||||
try {
|
||||
con = DriverManager.getConnection(connection, username, password);
|
||||
log.info("Connected successfully");
|
||||
return true;
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean disconnect() {
|
||||
if (!isConnected()) return true;
|
||||
try {
|
||||
con.close();
|
||||
con = null;
|
||||
log.info("Disconnected successfully");
|
||||
return true;
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return con != null;
|
||||
}
|
||||
|
||||
public void insert(String qry) {
|
||||
try {
|
||||
con.prepareStatement(qry).executeUpdate();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public ResultSet getData(String qry) {
|
||||
try {
|
||||
return con.prepareStatement(qry).executeQuery();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
85
Java/TCPServerAPI/src/de/craftix/server/Server.java
Normal file
85
Java/TCPServerAPI/src/de/craftix/server/Server.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import de.craftix.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Server {
|
||||
protected String ip;
|
||||
protected int port;
|
||||
protected Logger log;
|
||||
protected ServerSocket server;
|
||||
|
||||
public Server(int port) {
|
||||
try {
|
||||
this.port = port;
|
||||
this.ip = Inet4Address.getLocalHost().getHostAddress();
|
||||
this.log = new Logger("Server");
|
||||
server = new ServerSocket(port);
|
||||
log.info("Server started on " + ip + ":" + port);
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (server == null) return;
|
||||
new Thread(() -> {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
log.info("Awaiting connection...");
|
||||
Socket client = server.accept();
|
||||
log.info("Client connected [" + client.getRemoteSocketAddress() + "]");
|
||||
ObjectInputStream stream = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
|
||||
Object data = stream.readObject();
|
||||
Object answer = onConnection(client, data);
|
||||
if (answer != null) {
|
||||
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
|
||||
out.writeObject(answer);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
log.info("Data processed");
|
||||
stream.close();
|
||||
client.close();
|
||||
log.info("Task completed");
|
||||
} catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public Object sendDataToClient(Object data, boolean answer, InetSocketAddress client) {
|
||||
try {
|
||||
log.info("Connecting to Client... [" + client.getHostName() + ":" + client.getPort() + "]");
|
||||
Socket server = new Socket();
|
||||
server.connect(client, 10000);
|
||||
log.info("Client connected");
|
||||
|
||||
log.info("Sending data...");
|
||||
ObjectOutputStream stream = new ObjectOutputStream(new BufferedOutputStream(server.getOutputStream()));
|
||||
stream.writeObject(data);
|
||||
stream.flush();
|
||||
log.info("Data send");
|
||||
|
||||
Object out = null;
|
||||
if (answer) {
|
||||
ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(server.getInputStream()));
|
||||
out = input.readObject();
|
||||
input.close();
|
||||
log.info("Data received");
|
||||
}
|
||||
|
||||
stream.close();
|
||||
server.close();
|
||||
log.info("Task completed");
|
||||
|
||||
return out;
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Object onConnection(Socket client, Object data) { return null; }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user