You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.6 KiB
54 lines
1.6 KiB
package client.net;
|
|
|
|
import client.Main;
|
|
import client.util.CeasarChipher;
|
|
|
|
import java.io.DataInputStream;
|
|
import java.io.DataOutputStream;
|
|
import java.io.IOException;
|
|
import java.net.Socket;
|
|
|
|
public class Client {
|
|
|
|
private Socket s;
|
|
private DataInputStream inputStream;
|
|
private DataOutputStream outputStream;
|
|
private Thread t;
|
|
|
|
public Client(String host, int port) {
|
|
try {
|
|
s = new Socket(host, port);
|
|
inputStream = new DataInputStream(s.getInputStream());
|
|
outputStream = new DataOutputStream(s.getOutputStream());
|
|
t = new Thread(() -> {
|
|
try {
|
|
while (true) {
|
|
int len = inputStream.readInt();
|
|
byte[] buf = new byte[len];
|
|
inputStream.readFully(buf, 0, len);
|
|
|
|
//TEST
|
|
System.out.println(new String(CeasarChipher.decrypt(buf)));
|
|
}
|
|
} catch (IOException e) {
|
|
Main.console.error("An error occurred while reading");
|
|
e.printStackTrace();
|
|
}
|
|
});
|
|
t.start();
|
|
} catch (Throwable e) {
|
|
Main.console.error("An error occurred while creating client!");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void write(byte[] buf) {
|
|
try {
|
|
outputStream.writeInt(buf.length);
|
|
outputStream.write(CeasarChipher.encrypt(buf));
|
|
} catch (Throwable e) {
|
|
server.Main.console.error("An error occurred while writing data!");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|