简体   繁体   中英

What will happen to a TCP/UDP serversocket when I switch wifi network?

what will happen to the serversocket in my app when I suddenly change the wifi network? I guess it will shut down since my device will get a new IP, at least in TCP, is the UDP MulticastSocket prone to this as well? And how to end the previous Server socket thread and start a new one when the network changes? One solution is using time outs, another is using a flag that will indicate whether the infinite loop should end or not but since listening to a socket is a blocking function it will produce an exception/error anyways.

Any thoughts will be appreciated! :)

EDIT: sample of my server thread.

ServerSocket ss = new ServerSocket(4445);
while(true){
    Socket socket = ss.accept();
    ObjectInputStream in  = new ObjectInputStream(socket.getInputStream());
    Object obj = in.readObject();
    Log.i("TAG", "Received: " + obj.toString());
    in.close();
    socket.close();
}

TCPIP connection will break. So client would have to connect again.

UDP will be ok provided your IP does not change after reconnection. Of course if you transmit UDP its not going to make a difference for that machine.

You should get an exception in case of TCPIP which you can handle.

UDP sockets that are not bound to the address will remain open, as they are stateless. TCP listening sockets not bound to the address will remain open as well.

Conntected TCP sockets may be severed (RST) or just linger until a timeout hits.

It is a little known fact that IP mandates it that a device by default will accept packets directed to any address it has configured on any interface, no matter on which interface the packet arrives. If this were not so, routing would be broken. One can use packet filters to filter out packets with non-matching addresses depending on the interface.

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