Here’s what I got for the Server so far, yet everytime I run it (it creates a new thread fine), but it keeps saying ‘Connection reset’, after a new client connects.
// Constructor for the Server class.
public Server() {
running = true, keepSending = true;
connect();
}
protected final void connect() {
try {
serverSocket = new ServerSocket(port);
log(System.out, "Server listening on port: " + port + "...");
new Thread() {
{
start();
}
public void run() {
// Wait for client to connect on 4442
try {
clientSocket = serverSocket.accept();
clientSocket.setSoLinger(true, MAX_PRIORITY);
log(System.out, "Connected a new Client!");
// Create a reader
bufferedReader = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
// Get the client message
while ((inputLine = bufferedReader.readLine()) != null)
System.out.println(inputLine);
} catch (SocketException e1) {
log(System.err, "Client disconnected!");
} catch (IOException e) {
System.out.println(e);
}
// close the socket
closeCons();
}
};
} catch (IOException e) {
log(System.err, e.getMessage());
}
}
private static void closeCons() {
try {
serverSocket.close();
clientSocket.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Here’s the output in the console after launching the client:
It might have something to do with the Client class, yet it’s still not sending the String?
// Constructor for the Client class.
public Client() {
connect(port);
}
private void connect(int port2) {
try {
socket = new Socket("localhost", port2);
log(System.out, "Client connected");
log(System.out, socket);
printWriter = new PrintWriter(socket.getOutputStream(), true);
sendData("Hello Socket!");
sendData("WOOOOOOOOOOOOO");
} catch (SocketException e1) {
log(System.err, "Client disconnected!??!");
} catch (Exception e) {
log(System.err, e.getMessage());
}
}
Both on the same ports, 4442, if anyone can see why it’s not working correctly please let me know.
I’ll continue to try and fix it, i’d like the Client not to disconnect.