I'm trying to make an online Chat but when I launch the code I always get java.net.BindException: Address already in use (Bind failed). I've tried changing the Port several times but I always get the same Exception. Sometimes the Code still executes and Everything works fine but the exception is still a problem for me. I know there are questions similar to mine but there are only solutions for windows users and I am a Mac user. Here's the code(It's not completely finished and the error occurs on the Server part @ new ServerSocket();)
public Server(Client c) {
try {
ServerSocket serverSocket = new ServerSocket(65531);
while (true) {
Socket s = serverSocket.accept();
new Thread() {
@Override
public void run() {
super.run();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(s.getInputStream()));
String message = bufferedReader.readLine();
System.out.println("Antwort vom Client: " + message);
c.receivedMessage(message);
System.out.println("Adresse: "+s.getInetAddress().toString());
System.out.println(s.getInetAddress().getHostAddress());
s.close();
} catch (Exception e) {
}
}
}.run();
}
} catch (IOException e) {
e.printStackTrace();
}
}
EDIT:
I've checked the port before after and while the application was running and it is only used when running so the Server Socket is kinda working but I still get an exception