1

当我尝试让同一个应用程序的两个实例相互交谈时,ServerSocketConnectEvent 没有触发。这是我的场景:

我有一个 AS3 Adob​​e AIR 应用程序,它通过 localhost 与它的另一个实例通信。两个应用程序都侦听端口并尝试连接到彼此的端口。也就是说,实例 1 侦听端口 50000 并尝试连接到实例 2 上的端口 50001,实例 2 侦听端口 50001 并尝试连接端口 50000。应用程序每 100 毫秒尝试相互连接,直到建立连接。我使用 RawCap 在 localhost 上捕获数据,我看到用于目标端口 50000 和 50001 的数据包交错。

我的设置包括在调试模式下从 FlashDevelop 运行应用程序并单独运行捆绑的 exe(我可以拥有该应用程序的多个实例:如何获得启动多个相同 Adob​​e Air 应用程序的能力?)。我没有看到 ServerSocketConnectEvent.CONNECT 着火。以下是一些相关代码:

// triggered by clicking on a sprite
public function onClickConnect(event:MouseEvent):void
{
  m_connect.disable();
  m_serverSocket = createServerSocket(int(m_localPortText.text));

  m_remoteSocket = new Socket();
  m_remoteSocket.addEventListener(Event.CONNECT, connectionMade);
  m_remoteSocket.addEventListener( Event.CLOSE, connectionClosed );
  m_remoteSocket.addEventListener( IOErrorEvent.IO_ERROR, socketFailure );
  m_remoteSocket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityError );
  //m_remoteSocket.addEventListener( ProgressEvent.SOCKET_DATA, dataReceived);  

  m_retryTimer = new Timer(100);
  m_retryTimer.addEventListener(TimerEvent.TIMER, connectToRemote);
  m_retryTimer.start();
}

// this retries connecting to the remote client
public function connectToRemote(event:TimerEvent):void
{
  try
  {
    //Try to connect
    m_remoteSocket.connect( "127.0.0.1", int(m_remotePortText.text));
  }
  catch( e:Error ) { trace( e ); } 
}

public function createServerSocket(port:int):Socket
{
  try
  {
    // Create the server socket
    var serverSocket:ServerSocket = new ServerSocket();

    // Add the event listener
    serverSocket.addEventListener( ServerSocketConnectEvent.CONNECT, gotConnection );
    //serverSocket.addEventListener( Event.CLOSE, onClose );

    serverSocket.bind(port, "127.0.0.1" );
    // Listen for connections
    serverSocket.listen();

    // this prints fine
    trace("Listening on " + serverSocket.localAddress + ":" + serverSocket.localPort);


  }
  catch (e:Error)
  {
    trace(e);
  }

  return m_serverSocket;
}

// this is for connections from clients
// it never fires
public function gotConnection(event:ServerSocketConnectEvent):void
{
  //The socket is provided by the event object
  m_serverSocket = event.socket;

  trace("Connected to client");
}

// this is for connecting to the other app
public function connectionMade(event:Event):void
{
  trace(event);
  m_retryTimer.stop();
}

public function socketFailure(event:IOErrorEvent):void
{
  trace(event);
}

public function connectionClosed(event:Event):void
{
  trace(event);
}

public function securityError(event:SecurityErrorEvent):void
{
  trace(event);
}

有任何想法吗?

谢谢

4

1 回答 1

1

看起来您的服务器套接字超出范围并被垃圾收集。

在该方法中createServerSocket(),您使用局部变量实例化服务器套接字serverSocket。但同样的方法返回为空的 m_serverSocket。我认为您打算返回本地 var serverSocket:)

于 2012-12-11T03:17:16.640 回答