3

我正在构建一个 Android 聊天应用程序。我在服务器端使用nodejs并尝试为socketIO实现android客户端。我越来越SocketIO exception。代码有什么问题?

服务器

var http = require('http'),fs = require('fs');

var app = http.createServer(function (req, res) {

res.end();
                                                      }).listen(8000, '127.0.0.1');

var io = require('socket.io').listen(app);

io.sockets.on('connection', function(socket) {
socket.on('echo', function(data) {
socket.emit('echoback', data);
});
});

客户

package com.jack.pri;
import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import io.socket.*;

public class MainActivity extends Activity {
//private SocketIO socket;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t=(TextView) findViewById(R.id.et1);
    t.setText("uio");
    //////

   // System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    SocketIO socket = null;
    try {
        socket = new SocketIO("http://10.0.2.2:8000");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    socket.connect(new IOCallback() {
        @Override
        public void on(String event, IOAcknowledge ack, Object... args) {
            if ("echoback".equals(event) && args.length > 0) {
                Log.d("SocketIO", "" + args[0]);
                t.setText("s ");
                // -> "hello"
            }
        }

        @Override
        public void onMessage(JSONObject json, IOAcknowledge ack) {}
        @Override
        public void onMessage(String data, IOAcknowledge ack) {}
        @Override
        public void onError(SocketIOException socketIOException) { socketIOException.printStackTrace();}
        @Override
        public void onDisconnect() {}
        @Override
        public void onConnect() {}
    });
    socket.emit("echo", "hello");        
    ///
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

错误日志

09-10 13:49:34.587: W/System.err(1963): io.socket.SocketIOException: Error while handshaking
09-10 13:49:34.587: W/System.err(1963):     at io.socket.IOConnection.handshake(IOConnection.java:322)
09-10 13:49:34.597: W/System.err(1963):     at io.socket.IOConnection.access$600(IOConnection.java:39)
09-10 13:49:34.597: W/System.err(1963):     at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
09-10 13:49:34.637: W/System.err(1963): Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
4

1 回答 1

6

检查你是否放入INTERNET PERMISSIONManifest. 如果你不这样做,请输入:

<uses-permission android:name="android.permission.INTERNET"/>
于 2013-09-10T13:54:20.973 回答