0

I really can't work out why this wont work... It seems to work everywhere else I've seen it.

try {
    ServerSocket serverSocket = new ServerSocket(6644);
} catch (IOException e) {
    e.printStackTrace();
}
while(true){
    Socket clientSocket = serverSocket.accept();

}

Eclipse keeps telling me 'cannot be resolved' when I hover my mouse over serverSocket on the clientSocket = serverSocket.accept(); line. The variable obviously exists and there isn't a typo so what's the matter?

I've already imported java.io.* and java.net.* so I'm pretty sure I have everything I need.

Neguido
  • 235
  • 2
  • 13

1 Answers1

1

Your serverSocket variable is visible only inside the try block. Move the declaration outside, maybe like this:

ServerSocket serverSocket = null;
try {
    serverSocket = new ServerSocket(6644);
} catch (IOException e) {
    e.printStackTrace();
}
while(true){
    Socket clientSocket = serverSocket.accept();

}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136