且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误的Andr​​oid客户端连接到一个的NodeJS socket服务器

更新时间:2022-10-25 10:59:10

您无法从仿真器连接到本地机器上直接使用本地主机 127.0.0.1 - 你必须使用一个特殊的地址

地址连接到您的本地机器上运行的服务器(从仿真器)是 10.0.2.2 作为开发者文档描述

I got a Socket Server running in NodeJS. I've connected from a Console Application with this library:

https://github.com/Gottox/socket.io-java-client

I used the example app (from the link) and it works correctly. Then, I translate that code to an Android application, but it doesn't connect to the Server. I put the Network Permissions of the app inside the "androidmanifest" file, but it didn't work either.

Bellow is the error when I run the app. Thanks in advance for the help...

02-04 00:56:51.717: D/dalvikvm(971): Late-enabling CheckJNI
02-04 00:56:51.749: D/dalvikvm(971): Debugger has detached; object registry had 1 entries
02-04 00:56:51.761: E/Trace(971): error opening trace file: No such file or directory (2)
02-04 00:56:51.837: W/dalvikvm(971): Unable to resolve superclass of Lio/socket/WebsocketTransport; (851)
02-04 00:56:51.837: W/dalvikvm(971): Link of class 'Lio/socket/WebsocketTransport;' failed
02-04 00:56:51.837: I/dalvikvm(971): Could not find method io.socket.WebsocketTransport.create, referenced from method io.socket.IOConnection.connectTransport
02-04 00:56:51.837: W/dalvikvm(971): VFY: unable to resolve static method 5178: Lio/socket/WebsocketTransport;.create (Ljava/net/URL;Lio/socket/IOConnection;)Lio/socket/IOTransport;
02-04 00:56:51.837: D/dalvikvm(971): VFY: replacing opcode 0x71 at 0x001a
02-04 00:56:51.901: I/System.out(971): an Error occured
02-04 00:56:51.901: W/System.err(971): io.socket.SocketIOException: Error while handshaking
02-04 00:56:51.901: W/System.err(971):  at io.socket.IOConnection.handshake(IOConnection.java:322)
02-04 00:56:51.901: W/System.err(971):  at io.socket.IOConnection.access$7(IOConnection.java:292)
02-04 00:56:51.901: W/System.err(971):  at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
02-04 00:56:51.901: W/System.err(971): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 3000) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
02-04 00:56:51.905: W/System.err(971):  at libcore.io.IoBridge.isConnected(IoBridge.java:224)
02-04 00:56:51.905: W/System.err(971):  at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
02-04 00:56:51.905: W/System.err(971):  at libcore.io.IoBridge.connect(IoBridge.java:112)
02-04 00:56:51.905: W/System.err(971):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-04 00:56:51.905: W/System.err(971):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
02-04 00:56:51.905: W/System.err(971):  at java.net.Socket.connect(Socket.java:842)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
02-04 00:56:51.905: W/System.err(971):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
02-04 00:56:51.905: W/System.err(971):  at io.socket.IOConnection.handshake(IOConnection.java:313)
02-04 00:56:51.909: W/System.err(971):  ... 2 more
02-04 00:56:51.909: W/System.err(971): Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
02-04 00:56:51.909: W/System.err(971):  at libcore.io.IoBridge.isConnected(IoBridge.java:208)
02-04 00:56:51.909: W/System.err(971):  ... 19 more
02-04 00:56:51.909: I/io.socket(971): Cleanup
02-04 00:56:51.969: D/libEGL(971): loaded /system/lib/egl/libEGL_emulation.so
02-04 00:56:52.021: D/(971): HostConnection::get() New Host Connection established 0xb8a83f48, tid 971
02-04 00:56:52.025: D/libEGL(971): loaded /system/lib/egl/libGLESv1_CM_emulation.so
02-04 00:56:52.029: D/libEGL(971): loaded /system/lib/egl/libGLESv2_emulation.so
02-04 00:56:52.101: W/EGL_emulation(971): eglSurfaceAttrib not implemented
02-04 00:56:52.121: D/OpenGLRenderer(971): Enabling debug mode 0
02-04 00:56:52.181: D/OpenGLRenderer(971): TextureCache::get: create texture(0xb8a3c8e8): name, size, mSize = 1, 1048576, 1048576
02-04 00:56:52.557: D/OpenGLRenderer(971): TextureCache::get: create texture(0xb8a53d18): name, size, mSize = 2, 5184, 1053760
02-04 00:56:52.653: D/OpenGLRenderer(971): TextureCache::get: create texture(0xb8a3a470): name, size, mSize = 4, 20736, 1074496
02-04 00:56:52.665: D/OpenGLRenderer(971): TextureCache::get: create texture(0xb8a37848): name, size, mSize = 6, 2304, 1076800

You can't connect from the emulator to your local machine directly using localhost or 127.0.0.1 - you have to use a special address.

The address to connect to a server running on your local machine (from the emulator) is 10.0.2.2, as described in the developer documentation.