Add a timeout when creating a new Socket

前端 未结 2 700
一个人的身影
一个人的身影 2021-01-17 16:03

I have a local network with DHCP and a few PCs. One of these should be my Server and get automatically connected to all others (clients). My idea was this: First, I create a

2条回答
  •  无人及你
    2021-01-17 16:46

    I've found a solution for my problem. It was just initializing the Socket not with

    Socket s1 = new Socket("192.168.1." + i, 1254);
    

    but with

    Socket s1 = new Socket();
    s1.setSoTimeout(200);
    s1.connect(new InetSocketAddress("192.168.1." + i, 1254), 200);
    

    Thanks anyway!

提交回复
热议问题