简体   繁体   中英

SocketIO exception while using Android client for socketIO

I am building an Android chat application. I am using nodejs at server end and trying to implement android client for socketIO. I am getting SocketIO exception . What is wrong with the codes ?

Server

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);
});
});

Client

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;
}

}

Errorlog

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)

Check if you put INTERNET PERMISSION in the Manifest . If you don't, put:

<uses-permission android:name="android.permission.INTERNET"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM