开发者

Java Server Socket failure

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜