当我切换 wifi 网络时,TCP/UDP serversocket 会发生什么? [英] What will happen to a TCP/UDP serversocket when I switch wifi network?

查看:45
本文介绍了当我切换 wifi 网络时,TCP/UDP serversocket 会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我突然改变wifi网络时,我的应用程序中的serversocket会发生什么?我想它会关闭,因为我的设备将获得一个新 IP,至少在 TCP 中,UDP MulticastSocket 是否也容易出现这种情况?以及如何在网络变化时结束之前的Server socket线程并启动一个新的?一种解决方案是使用超时,另一种是使用一个标志来指示无限循环是否应该结束,但由于侦听套接字是一个阻塞函数,它无论如何都会产生异常/错误.

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! :)

我的服务器线程示例.

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 连接将中断.所以客户端必须重新连接.

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

UDP 将可以,前提是您的 IP 在重新连接后没有改变.当然,如果你传输 UDP,它不会对那台机器产生影响.

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.

你应该得到一个你可以处理的 TCPIP 异常.

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

这篇关于当我切换 wifi 网络时,TCP/UDP serversocket 会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆