I'm building a simple client / server program on Java using Sockets. I've followed several tutorials on creating a simple client / server program which has got me this far, but when I try and run my server class, it either freezes when I type in certain ports and does nothing, or with some other ports it gives me the exception "java.net.BindException: Address already in use: JVM_Bind".
Here's some of the key parts to my server class;
public void start() throws IOException {
isRunning = true;
serverSocket = new ServerSocket(getPort());
while (isRunning) {
try {
clientConnection();
setupStreams();
} catch (IOException ex) {
addOutput("Connection unsuccessful.");
System.out.println(ex);
System.exit(1);
}
}
}
private void clientConnection() throws IOException {
addOutput("Listening for client...");
socket = serverSocket.accept();
addOutput("Connection successful."); // print out username which connected
}
private void setupStreams() throws IOException {
serverIn = new ObjectInputStream(socket.getInputStream());
serverOut = new ObjectOutputStream(socket.getOutputStream());
}
private void btnConnectActionPerformed(ActionEvent evt) {
if (evt.getSource() == this.btnConnect) {
try {
start();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
Like I said, I've followed tutorials and there's not much difference if any between what I've done compared to them, but I'm getting issues and my server is just doing nothing and freezes, or gives that exception.
Any ideas?
telnetas a simple client…