So I'm building this pretty simple top trumps card game as part of an assignment.
I have the game logic built, the swing GUI is in place and I plan to do all the calculation on the server app and have it update the client up with the game's state, round, turn to play and decks.
Obviously, I need a multitude of threads.
So I've built this method which I call by pressing a button in MainGUIServer.java
:
public void startRunning() { // Initiates the server.
try {
while (true) {
try {
// Connecting and communicating.
ServerThread st = new ServerThread(this.packet, this.jta, this.port, this.out, this.in, this.server, this.connection);
Thread newThread = new Thread(st);
newThread.start();
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e + "in line 40");
}
}
} catch (Exception e) {
System.out.println(e + "in line 37.");
}
}
Now, there is a class I called ServerThread and it does this:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prinsu.toptrumps;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
/**
*
* @author Mirza
*/
public class ServerThread implements Runnable {
private Packet packet;
private JTextArea jta;
private int port;
private ObjectOutputStream out;
private ObjectInputStream in;
private ServerSocket server;
private Socket connection;
public ServerThread(Packet packet, JTextArea jta, int port, ObjectOutputStream out, ObjectInputStream in, ServerSocket server, Socket connection) {
this.packet = packet;
this.jta = jta;
this.port = port;
this.out = out;
this.in = in;
this.server = server;
this.connection = connection;
}
@Override
public void run() {
try {
this.server = new ServerSocket(this.port);
while (true) {
try {
// Connecting and communicating.
waitForConnection();
System.out.println("1");
setupStreams();
System.out.println("2");
whileChatting();
System.out.println("3");
} catch (EOFException e) {
System.out.println(e + " RUN1");
} finally {
cleanUp();
}
}
} catch (IOException e) {
System.out.println(e + " RUN2");
}
}
private void waitForConnection() throws IOException { // Waits for client connection.
showMessage("Waiting for client...");
while (true) {
this.connection = this.server.accept();
showMessage("Client connected from " + connection.getInetAddress().getHostAddress());
}
}
private void setupStreams() throws IOException { // Send and recieve data (IO stream creation).
this.out = new ObjectOutputStream(this.connection.getOutputStream());
this.out.flush();
this.in = new ObjectInputStream(this.connection.getInputStream());
}
private void whileChatting() throws IOException { // Allows communication.
String message = "you're connected";
do {
try {
this.packet = (Packet) this.in.readObject();
showMessage(message);
} catch (ClassNotFoundException e) {
System.out.println(e+ "in line 77.");
System.out.println("Can't read packet or its empty.");
}
} while (!message.equals("CLIENT - END"));
}
private void cleanUp() { // Closes the connection.
showMessage("Closing connection...");
try {
this.out.close();
this.in.close();
this.connection.close();
} catch (IOException e) {
System.out.println(e + "in line 89.");
}
showMessage("Connection closed!");
}
private void showMessage(String msg) { // Prints messages in GUI's console.
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jta.append("\n"+msg);
}
});
}
}
The console prints this:
run:
java.net.BindException: Address already in use: JVM_Bind RUN2
java.net.BindException: Address already in use: JVM_Bind RUN2
java.net.BindException: Address already in use: JVM_Bind RUN2
java.net.BindException: Address already in use: JVM_Bind RUN2
java.net.BindException: Address already in use: JVM_Bind RUN2
java.net.BindException: Address already in use: JVM_Bind RUN2
java.net.BindException: Address already in use: JVM_Bind RUN2
java.net.BindException: Address already in use: JVM_Bind RUN2
java.net.BindException: Address already in use: JVM_Bind RUN2
BUILD STOPPED (total time: 7 seconds)
During outputting, the GUI freezes (even tho I ran a separate thread, I think). Googling the error message revealed I might be calling ServerSocket
on the same port twice, thus blocking it. But I'm not.
What do I fail to see?
PS
Thank you for reading this.