Removed TCPClient class

This commit is contained in:
WickedJack99
2023-11-26 14:37:59 +01:00
parent 05a2be1c07
commit 1b2987cf7d

View File

@@ -1,90 +0,0 @@
/**
* @author Aaron Moser
* @date 05.11.2023
* @src https://www.geeksforgeeks.org/socket-programming-in-java/
*/
package connect.src;
import java.io.*;
import java.net.*;
import org.json.JSONObject;
public final class TCPClient {
private final int MIN_PORT = 1;
private final int MAX_PORT = 65535;
private String ipAddress = "127.0.0.1";
private int port = 65432;
private Socket socket = null;
private boolean useIPv6 = false;
private DataOutputStream dataSentToServerStream = null;
public TCPClient(String address, int port, boolean useIPv6) {
if ((null != address) && (MIN_PORT <= port) && (MAX_PORT >= port)) {
if (useIPv6) {
this.useIPv6 = useIPv6;
} else {
this.ipAddress = address;
this.port = port;
}
} else {
//TODO display error at gui
//TODO don't even allow at gui wrong inputs
//TODO sanitize input with regex
}
}
public boolean connectToServer() {
boolean connectionEstablished = false;
try {
if (this.useIPv6) {
// If client wants to connect via ipv6
InetAddress ipv6Address = InetAddress.getByName(this.ipAddress);
this.socket = new Socket(ipv6Address, this.port);
} else {
// If client wants to connect via ipv4
this.socket = new Socket(this.ipAddress, this.port);
}
System.out.println("Connected to server with ip:" + this.ipAddress + " port:" + this.port);
connectionEstablished = true;
}
catch (UnknownHostException exception) {
System.out.println(exception);
}
catch (IOException exception) {
System.out.println(exception);
}
return connectionEstablished;
}
public boolean sendData(String pathToInputFile) {
boolean dataSent = false;
try {
JSONObject file = new JSONObject(pathToInputFile);
// sends output to the socket
this.dataSentToServerStream = new DataOutputStream(this.socket.getOutputStream());
this.dataSentToServerStream.writeUTF(file.toString());
dataSent = true;
}
catch (IOException i) {
System.out.println(i);
}
return dataSent;
}
public boolean closeConnection() {
boolean connectionClosed = false;
try {
dataSentToServerStream.close();
socket.close();
connectionClosed = true;
}
catch (IOException i) {
System.out.println(i);
}
return connectionClosed;
}
}