io.socket.SocketIOException:握手时出错(模拟器)

时间:2014-09-08 13:40:40

标签: java android node.js sockets

我正在尝试使用套接字创建简单的node.js服务器和Android客户端。

node.js服务器代码:

"use strict";

var http = require("http");

var server = http.createServer(processRequest).listen(3001, "192.168.1.4");

function processRequest(request, response) {

    response.writeHead(200, {
        "Content-Type": "text/html"
    });

    response.write("Simple HTML Page");

    response.end();

}


// Buradan sonrası yeni
var io = require("socket.io").listen(server);

io.sockets.on('connection', function (socket) {
    sendMessage(socket);
    console.log("User Connected");

    socket.on("disconnect", function () {

        console.log("User Disconnected");
    });
});

function sendMessage(socket) {

    setTimeout(function () {
        socket.emit("test", {
            value1: "Aa",
            value2: 49
        });

        sendMessage(socket);
    }, 1000);
}

android客户端代码:

package com.example.mobi;
import io.socket.*;

import java.net.MalformedURLException;
import java.util.*;

import org.json.JSONException;
import org.json.JSONObject;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
private TextView tView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tView = (TextView) findViewById(R.id.textView1);
     tView.setText("Başlatılıyor");
    runIO();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
public void runIO(){
    try {
        SocketIO socket = new SocketIO("http://192.168.1.4:3001");
        socket.connect(new IOCallback() {
            @Override
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {
                    tView.setText("Server said:" + json.toString(2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onMessage(String data, IOAcknowledge ack) {
                 tView.setText("Server said: " + data);
            }

            @Override
            public void onError(SocketIOException socketIOException) {
                 tView.setText("an Error occured");
                 socketIOException.printStackTrace();
            }

            @Override
            public void onDisconnect() {
                 tView.setText("Connection terminated.");
            }

            @Override
            public void onConnect() {
                 tView.setText("Connection established");
            }

            @Override
            public void on(String event, IOAcknowledge ack, Object... args) {
                 tView.setText("Server triggered event '" + event + "'");
            }
        });

        // This line is cached until the connection is establisched.
        socket.send("Hello Server!");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

错误(logcat):

  

09-08 09:33:52.670:W / System.err(1302):io.socket.SocketIOException:   握手时出错09-08 09:33:52.680:W / System.err(1302):at   io.socket.IOConnection.handshake(IOConnection.java:322)09-08   09:33:52.680:W / System.err(1302):at   io.socket.IOConnection.access $ 600(IOConnection.java:39)09-08   09:33:52.690:W / System.err(1302):at   io.socket.IOConnection $ ConnectThread.run(IOConnection.java:199)09-08   09:33:52.690:W / System.err(1302):引起:java.net.SocketException:   套接字失败:EACCES(权限被拒绝)09-08 09:33:52.710:   W / System.err(1302):at libcore.io.IoBridge.socket(IoBridge.java:576)   09-08 09:33:52.710:W / System.err(1302):at   java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)09-08   09:33:52.710:W / System.err(1302):at   java.net.Socket.checkOpenAndCreate(Socket.java:663)09-08   09:33:52.710:W / System.err(1302):at   java.net.Socket.connect(Socket.java:807)09-08 09:33:52.720:   W / System.err(1302):at   libcore.net.http.HttpConnection。(HttpConnection.java:76)09-08   09:33:52.720:W / System.err(1302):at   libcore.net.http.HttpConnection。(HttpConnection.java:50)09-08   09:33:52.720:W / System.err(1302):at   libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340)   09-08 09:33:52.720:W / System.err(1302):at   libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)   09-08 09:33:52.720:W / System.err(1302):at   libcore.net.http.HttpConnection.connect(HttpConnection.java:128)09-08   09:33:52.720:W / System.err(1302):at   libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)   09-08 09:33:52.730:W / System.err(1302):at   libcore.net.http.HttpEngine.connect(HttpEngine.java:311)09-08   09:33:52.730:W / System.err(1302):at   libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)   09-08 09:33:52.730:W / System.err(1302):at   libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)09-08   09:33:52.730:W / System.err(1302):at   libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)   09-08 09:33:52.740:W / System.err(1302):at   libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)   09-08 09:33:52.740:W / System.err(1302):at   io.socket.IOConnection.handshake(IOConnection.java:313)09-08   09:33:52.740:W / System.err(1302):... 2更多09-08 09:33:52.740:   W / System.err(1302):引起:libcore.io.ErrnoException:socket   失败:EACCES(权限被拒绝)09-08 09:33:52.750:   W / System.err(1302):at libcore.io.Posix.socket(Native Method)09-08   09:33:52.750:W / System.err(1302):at   libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)09-08   09:33:52.760:W / System.err(1302):at   libcore.io.IoBridge.socket(IoBridge.java:561)09-08 09:33:52.760:   W / System.err(1302):... 17 more

0 个答案:

没有答案