samedi 21 novembre 2015

Adobe AIR ServerSocket remote connection

Im having trouble establishing remote connection between 2 applications with Sockets. I have a server socket running on my desktop machine:

    server = new ServerSocket();
    server.bind(9999);
    server.addEventListener(ServerSocketConnectEvent.CONNECT, serverConnectHandler);
    server.listen();

private static function serverConnectHandler(e:ServerSocketConnectEvent):void {
    var socket:Socket = e.socket;
    clients.push(socket);
    socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
    socket.addEventListener(flash.events.Event.CLOSE, closeHandler)
    trace("connected");
}

private static function closeHandler(event:Event):void {
    Socket(event.target).close();
    clients.splice(clients.indexOf(Socket(event.target)), 1);
    trace("clisiug");
}

private static function socketDataHandler(e:ProgressEvent):void {

    var socket:Socket = e.currentTarget as Socket;
    var data:String= socket.readUTFBytes(socket.bytesAvailable);
    trace("Data received:", data);

}

public static function send(message:String):void {

    trace("sending");
    for each(var client:Socket in clients){
        client.writeUTFBytes(message);
        client.flush();
    }
}

This is the client code running on my android device :

    var host:String = "my-ip-address from http://ift.tt/1J5bfMJ";
    socket = new Socket();
    socket.addEventListener(Event.CONNECT, connectHandler);
    socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
    socket.addEventListener(flash.events.Event.CLOSE, closeHandler)
    socket.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    socket.connect(host, 9999);

But this throws IOError every time.

What could be wrong here?

EDIT: error also happens if I'm running both apps on the same machine but trying to connect using my public ip address

Aucun commentaire:

Enregistrer un commentaire