开发者

Java Server Socket failure

开发者 https://www.devze.com 2023-02-05 03:34 出处:网络
while (true) { ServerSocket myServerSocket = new ServerSocket(9999); 开发者_StackOverflow中文版Socket skt = myServerSocket.accept();
while (true) {

    ServerSocket myServerSocket = new ServerSocket(9999);
    开发者_StackOverflow中文版Socket skt = myServerSocket.accept();

    Handling obj = new Handling();
    obj.handle(skt);

}

When i first try this it works fine and accepts the Socket but then when it loops back it says the address is in use. How do I fix this?


Don't create a new ServerSocket in the loop - you only need to accept in the loop:

ServerSocket myServerSocket = new ServerSocket(9999);
while (true) {
    Socket skt = myServerSocket.accept();
    Handling obj = new Handling();
    obj.handle(skt);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消