【发布时间】:2014-12-04 19:56:36
【问题描述】:
从 android 套接字编程连接到 node.js 时出错。我正在使用 socket.io 连接到服务器,但一次又一次地发生超时,任何帮助将不胜感激
代码
SocketIO socket = new SocketIO("http://10.10.10.125:1337");
socket.connect
(
new IOCallback(){
@Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
}
@Override
public void onConnect() {
System.out.println("Connection established");
//socket.send("Hello Server!");
//System.out.println("send msg to server");
}
@Override
public void onMessage(String arg0,
IOAcknowledge arg1) {
// TODO Auto-generated method stub
System.out.println("Server said: " + arg0);
}
@Override
public void onMessage(JSONObject arg0,
IOAcknowledge arg1) {
// TODO Auto-generated method stub
try {
System.out.println("Server said:" + arg0.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void on(String arg0, IOAcknowledge arg1,
Object... arg2) {
// TODO Auto-generated method stub
System.out.println("on....");
}
}
);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
错误
12-04 16:30:30.510: W/System.err(8986): io.socket.SocketIOException: Error while handshaking
12-04 16:30:30.510: W/System.err(8986): at io.socket.IOConnection.handshake(IOConnection.java:322)
12-04 16:30:30.510: W/System.err(8986): at io.socket.IOConnection.access$600(IOConnection.java:39)
12-04 16:30:30.510: W/System.err(8986): at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
12-04 16:30:30.510: W/System.err(8986): Caused by: java.net.SocketTimeoutException
12-04 16:30:30.510: W/System.err(8986): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
12-04 16:30:30.510: W/System.err(8986): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
12-04 16:30:30.510: W/System.err(8986): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
12-04 16:30:30.510: W/System.err(8986): at java.io.InputStream.read(InputStream.java:163)
12-04 16:30:30.510: W/System.err(8986): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
12-04 16:30:30.510: W/System.err(8986): at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
12-04 16:30:30.510: W/System.err(8986): at libcore.io.Streams.readAsciiLine(Streams.java:201)
12-04 16:30:30.510: W/System.err(8986): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
12-04 16:30:30.510: W/System.err(8986): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
12-04 16:30:30.510: W/System.err(8986): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
12-04 16:30:30.510: W/System.err(8986): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
12-04 16:30:30.510: W/System.err(8986): at io.socket.IOConnection.handshake(IOConnection.java:313)
12-04 16:30:30.510: W/System.err(8986): ... 2 more
【问题讨论】:
-
你如何定义你的套接字?请发布您的那部分代码。
-
@MohammadRahchamani 我已经更新了代码,请检查...
-
你的服务器的socket.io版本是什么,你用哪个库做安卓客户端?
-
我正在使用 socketio.jar