简体   繁体   中英

Socket clientSocket = serverSocket.accept(); cannot be resolved

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.

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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM