Is there any way to detect that a TCP connection was rejected using flash.net.Socket?

There is a flash movie that uses flash.net.Socket to connect to the server. But there may be a situation where the server is down, so nothing is listening to which port socket is connecting.

When I do "portname hostname port", I get an error with a fast connection. But flash.net.Socket does not cause any event (see below), but silently waits for a socket timeout. It is important for me to reduce the time it takes to discover a server that does not exist, as much as possible, in order to reconnect to another server in the cluster.

I tried the following events, but to no avail:

  • close
  • connecting
  • IOERROR
  • SecurityError
  • socketData li>

None of them are called in such a situation.

Is there any way to detect that a TCP connection was rejected using flash.net.Socket?

+3
3

catch <img src='http://target:port/' onerror='noconnect' />?

0

:

package {
    import flash.display.Sprite;

    public class SocketExample extends Sprite {

        public function SocketExample() {
            var socket:CustomSocket = new CustomSocket("localhost", 80);
        }
    }
}

import flash.errors.*;
import flash.events.*;
import flash.net.Socket;

class CustomSocket extends Socket {
    private var response:String;

    public function CustomSocket(host:String = null, port:uint = 0) {
        super(host, port);
        this.timeout=100;
        configureListeners();
    }

    private function configureListeners():void {
        addEventListener(Event.CLOSE, closeHandler);
        addEventListener(Event.CONNECT, connectHandler);
        addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
    }

    private function writeln(str:String):void {
        str += "\n";
        try {
            writeUTFBytes(str);
        }
        catch(e:IOError) {
            trace(e);
        }
    }

    private function sendRequest():void {
        trace("sendRequest");
        response = "";
        writeln("GET /");
        flush();
    }

    private function readResponse():void {
        var str:String = readUTFBytes(bytesAvailable);
        response += str;
    }

    private function closeHandler(event:Event):void {
        trace("closeHandler: " + event);
        trace(response.toString());
    }

    private function connectHandler(event:Event):void {
        trace("connectHandler: " + event);
        sendRequest();
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function socketDataHandler(event:ProgressEvent):void {
        trace("socketDataHandler: " + event);
        readResponse();
    }
}
0

Socket IOErrorEvent.IO_ERROR. , , , . , , , - ( ):

// first create the unconnected socket, and add the listener
// you *must* add the listener *before* connecting the socket
//
var mySocket = new Socket();
mySocket.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void {
    // called when error occurrs
    trace("ioErrorHandler: " + event);
});
mySocket.addEventListener(flash.events.Event.CONNECT, function(event:Event):void {
    // handle a successful connection here
    //...
});

// now initiate the connection to port 80 on "server.example.com"   
mySocket.connect( 'server.example.com', 80 );

. -. " ", IO Error .

- , . /, , " ". , ( Flash) , . - , IOErrorEvent.IO_ERROR, .

- , (~ 20-30 ). Socket.timeout Flash Player 10 Air 1.0. , - ActionScript Flash Player 10.

!

--- EDIT: , ---

, - ( ) . , , . Adobe:

XML , , SWF. , :

  • 843 ( )
  • ,
  • ,

This means that if you have not previously downloaded the policy file using Security.loadPolicyFile(), when you first try to connect your socket to the server, first try to run the policy file. In some cases, this can lead to strange connection delays or other unexpected behavior. If you think you might run into this problem, you should start by adding a listener for flash.events.SecurityErrorEvent.SECURITY_ERROR.

0
source

Source: https://habr.com/ru/post/1757828/


All Articles