切换Data-Wifi时连接因错误而关闭

时间:2015-04-30 01:27:16

标签: android xmpp smack

我在Android上使用Smack 4.1.0。 我可以毫无问题地发送和接收所有内容,但如果手机从Wifi切换到数据或数据到Wifi,则连接会丢失。

这是例外:

04-28 10:18:35.237    1221-1276/com.e0.chat D/SMACK﹕ XMPPConnection closed    due to an exception (0)  
04-28 10:18:35.237    1221-1276/com.e0.chat W/System.err﹕ java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)  
04-28 10:18:35.237    1221-1276/com.e0.chat W/System.err﹕ at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:592)  
04-28 10:18:35.237    1221-1276/com.e0.chat W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:556)  
04-28 10:18:35.237    1221-1276/com.e0.chat W/System.err﹕ at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)  
04-28 10:18:35.237    1221-1276/com.e0.chat W/System.err﹕ at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)  
04-28 10:18:35.237    1221-1276/com.e0.chat W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)  
04-28 10:18:35.237    1221-1276/com.e0.chat W/System.err﹕ at java.io.InputStreamReader.read(InputStreamReader.java:231)  
04-28 10:18:35.237    1221-1276/com.e0.chat W/System.err﹕ at java.io.BufferedReader.read(BufferedReader.java:325)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at org.jivesoftware.smack.util.ObservableReader.read(ObservableReader.java:41)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1515)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:992)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at org.kxml2.io.KXmlParser.next(KXmlParser.java:349)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at org.kxml2.io.KXmlParser.next(KXmlParser.java:313)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1151)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at java.lang.Thread.run(Thread.java:818)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ Caused by: android.system.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at libcore.io.Posix.recvfromBytes(Native Method)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at libcore.io.Posix.recvfrom(Posix.java:161)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:553)  
04-28 10:18:35.242    1221-1276/com.e0.chat W/System.err﹕ ... 14 more  

我尝试使用eJabberd和Prosody。在这两个我都有同样的问题。 已尝试在Android中启用/禁用StreamManagement并且没有任何更改。

交换元素的痕迹:

04-28 10:18:12.482    1221-1275/com.e0.chat D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='localhost' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='user@localhost' xml:lang='en'>  
04-28 10:18:14.332    1221-1276/com.e0.chat D/SMACK﹕ RECV (0): <?xml version='1.0'?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='localhost' id='9db93977-6dd2-4d93-9195-f852c1ee8e90' xml:lang='en' xmlns='jabber:client'><stream:features><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>SCRAM-SHA-1</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms></stream:features>  
04-28 10:18:14.342    1221-1275/com.e0.chat D/SMACK﹕ SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='SCRAM-SHA-1'>biwsbj11c2VyLHI9al9RdGp3I1hbdXE5aC48bFN1Xmsge3lMPi8zQCp1Zy0=</auth>  
04-28 10:18:15.072    1221-1221/com.e0.chat I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@15cdd1a4 time:68067213  
04-28 10:18:16.157    1221-1276/com.e0.chat D/SMACK﹕ RECV (0): <challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1qX1F0ancjWFt1cTloLjxsU3VeayB7eUw+LzNAKnVnLWYxMjRhMmU0LWJjNzctNDEzOC05YmNhLTU5ZDQ3NTY0MDM2OSxzPU5EUmtaRGd6TldVdE1tVTJaaTAwWW1FMkxXRmlaVEF0TURreVpUQXdOREl4WW1ZNSxpPTQwOTY=</challenge>  
04-28 10:18:16.557    1221-1275/com.e0.chat D/SMACK﹕ SENT (0): <response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>Yz1iaXdzLHI9al9RdGp3I1hbdXE5aC48bFN1Xmsge3lMPi8zQCp1Zy1mMTI0YTJlNC1iYzc3LTQxMzgtOWJjYS01OWQ0NzU2NDAzNjkscD1zOVloWitBYW96WDNhZytKTlh3d2k5WjUyTFE9</response>  
04-28 10:18:16.927    1221-1276/com.e0.chat D/SMACK﹕ RECV (0): <success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1mMUZGTFMyTG15WStLZjN6M0ZTVDZzN2c0UkE9</success>  
04-28 10:18:16.932    1221-1275/com.e0.chat D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='localhost' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='user@localhost' id='9db93977-6dd2-4d93-9195-f852c1ee8e90' xml:lang='en'>  
04-28 10:18:17.132    1221-1276/com.e0.chat D/SMACK﹕ RECV (0): <?xml version='1.0'?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='localhost' id='420a3e3c-3061-496f-999c-acb62ff8b73d' xml:lang='en' xmlns='jabber:client'><stream:features><c hash='sha-1' ver='ZBWApSGFMsTZkuVThHtyU5xv1Mk=' node='http://prosody.im' xmlns='http://jabber.org/protocol/caps'/><ver xmlns='urn:xmpp:features:rosterver'/><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><required/></bind><session xmlns='urn:ietf:params:xml:ns:xmpp-session'><optional/></session></stream:features>  
04-28 10:18:17.137    1221-1275/com.e0.chat D/SMACK﹕ SENT (0): <iq id='rwE4r-3' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>localhost</resource></bind></iq>  
04-28 10:18:17.207    1221-1276/com.e0.chat D/SMACK﹕ RECV (0): <iq id='rwE4r-3' type='result'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>user@localhost/localhost</jid></bind></iq>  
04-28 10:18:17.217    1221-1242/com.e0.chat D/SMACK﹕ User logged (0): user@localhost:5222/localhost  
04-28 10:18:17.227    1221-1242/com.e0.chat D/SMACK﹕ XMPPConnection authenticated (0)  
04-28 10:18:17.227    1221-1242/com.e0.chat I/System.out﹕ ##########################################################  
04-28 10:18:17.227    1221-1275/com.e0.chat D/SMACK﹕ SENT (0): <iq id='rwE4r-5' type='get'><query xmlns='jabber:iq:roster'></query></iq>  
04-28 10:18:17.232    1221-1275/com.e0.chat D/SMACK﹕ SENT (0): <presence id='rwE4r-6'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='NfJ3flI83zSdUDzCEICtbypursw='/></presence>  
04-28 10:18:17.242    1221-1275/com.e0.chat D/SMACK﹕ SENT (0): <message to='admin@localhost' id='rwE4r-8' type='chat'><body>Howdy!</body><thread>6c23f0fc-b30f-474f-97e6-4114ccc5df7b</thread></message>  
04-28 10:18:17.247    1221-1275/com.e0.chat D/SMACK﹕ SENT (0): <message to='admin@localhost' id='rwE4r-9' type='chat'><body>ASDASDASDASD</body><thread>6c23f0fc-b30f-474f-97e6-4114ccc5df7b</thread></message>  
04-28 10:18:17.422    1221-1276/com.e0.chat D/SMACK﹕ RECV (0): <iq id='rwE4r-5' type='result' to='user@localhost/localhost'><query ver='1' xmlns='jabber:iq:roster'/></iq><presence id='rwE4r-6' from='user@localhost/localhost'><c hash='sha-1' ver='NfJ3flI83zSdUDzCEICtbypursw=' node='http://www.igniterealtime.org/projects/smack' xmlns='http://jabber.org/protocol/caps'/></presence>  
04-28 10:18:32.082    1221-1221/com.e0.chat E/Sulod sa network reciever﹕ Sulod sa network reciever  
04-28 10:18:35.142    1221-1276/com.e0.chat W/AbstractXMPPConnection﹕ Connection closed with error  
    java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)  
            at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:592)  
            at libcore.io.IoBridge.recvfrom(IoBridge.java:556)  
            at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)  
            at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)  
            at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)  
            at java.io.InputStreamReader.read(InputStreamReader.java:231)  
            at java.io.BufferedReader.read(BufferedReader.java:325)  
            at org.jivesoftware.smack.util.ObservableReader.read(ObservableReader.java:41)  
            at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1515)  
            at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:992)  
            at org.kxml2.io.KXmlParser.next(KXmlParser.java:349)  
            at org.kxml2.io.KXmlParser.next(KXmlParser.java:313)  
            at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1151)  
            at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)  
            at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)  
            at java.lang.Thread.run(Thread.java:818)  
     Caused by: android.system.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)  
            at libcore.io.Posix.recvfromBytes(Native Method)  
            at libcore.io.Posix.recvfrom(Posix.java:161)  
            at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)  
            at libcore.io.IoBridge.recvfrom(IoBridge.java:553)  
            at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)  
            at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)  
            at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)  
            at java.io.InputStreamReader.read(InputStreamReader.java:231)  
            at java.io.BufferedReader.read(BufferedReader.java:325)  
            at org.jivesoftware.smack.util.ObservableReader.read(ObservableReader.java:41)  
            at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1515)  
            at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:992)  
            at org.kxml2.io.KXmlParser.next(KXmlParser.java:349)  
            at org.kxml2.io.KXmlParser.next(KXmlParser.java:313)  
            at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1151)  
            at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)  
            at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)  
            at java.lang.Thread.run(Thread.java:818)  

连接完成的代码:

   SmackConfiguration.DEBUG = true;  
                XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();  


                configBuilder.setUsernameAndPassword("user", "user");  
                configBuilder.setServiceName("localhost");  
                configBuilder.setHost(host);  
                configBuilder.setPort(5222);  
                //configBuilder.setSendPresence(true);  


                //configBuilder.setCompressionEnabled(true);  
                configBuilder.setResource("localhost");  
                configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);  


                connection = new XMPPTCPConnection(configBuilder.build());  
               // XMPPTCPConnection.setUseStreamManagement(true);  
                connection.setUseStreamManagement(false);  
                connection.setUseStreamManagementResumption(false);  
                // Connect to the server  
                try {  
                    connection.connect();  
                } catch (SmackException e) {  

                    Log.d(Smack", "except");  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    Log.d("IOE", "except");  
                    e.printStackTrace();  
                } catch (XMPPException e) {  
                    Log.d("XMPP", "except");  
                    e.printStackTrace();  
                }  
                // Log into the server  
                try {  
                    connection.login();  
                    System.out.println("##########################################################");  
                    Chat chat = ChatManager.getInstanceFor(connection).createChat("admin@localhost", new ChatMessageListener() {  
                        @Override  
                        public void processMessage(Chat chat, Message message) {  
                            // Print out any messages we get back to standard out.  
                            System.out.println("Received message: " + message);  
                        }  
                    });  
                    Message me = new Message();  
                    chat.sendMessage("Howdy!");  
                    chat.sendMessage("ASDASDASDASD");  

                } catch (XMPPException e) {  
                    Log.d("XMPP", "except");  
                    e.printStackTrace();  
                } catch (SmackException e) {  
                    Log.d("Smack", "except");  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    Log.d("IO", "except");  
                    e.printStackTrace();  
                }  

我希望你能帮助我!

谢谢!

0 个答案:

没有答案