serversocket listen connection
Hi,
I have been building my whole client/server program on one machine using localhost for the client.
However i have now tried to connect the client to the server across a local network.
i am not sure how i set my server to listen on my default connection.
this is my server code
Code:
public class MultiServer implements Runnable {
public static final int PORT = 8080; //set the port
ServerSocket server = null; //create a server socket variable
Socket socket = null; //create a new socket variable
public void startConnection() throws IOException {
server = new ServerSocket(PORT);//creates a new server socket called server
JOptionPane.showMessageDialog(null, "server started"); //displays a message that the server has been started
Thread thisThread = new Thread(this); //creates a new thread
thisThread.start(); //starts the new thread
}
public void run() {
try {
while (true) {
socket = server.accept(); //listens and accepts clients
try {
new ServerThread(socket, false); //start serverThread
} catch (IOException ex) //catch IOexception if serverthread has errors
{
System.err.println("IOException: " + ex); //display error
socket.close(); //close connection
}
}
} catch (IOException ex) { //catch IOexception if client cant connec to server
Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
server.close(); //close server
} catch (IOException ex) {
Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
// end of MultiServer class