2010-11-30 44 views
0

我在使用闪存构建器4中的套接字时遇到问题。下面的代码向接收的c#sockerServer发送一组字节。如果我忽略手动获取Flash Builder中的错误,则字节会正常发送,并且所有字符都会在127.0.0.1:10上显示。现在,如果我可以获得相同的结果,而不会在Flex中显示错误。Flex - Socket.close()问题(#2031:套接字错误)

所以,我有两个问题:

1)当我尝试关闭套接字为什么它返回一个错误?请参阅下面的closeConnection()以获取上下文。我试图冲洗它之前没有帮助。

2)为什么我在使用socket.flush()时没有发送任何消息?

package 
{ 
import flash.events.IOErrorEvent; 
import flash.net.Socket; 
import flash.utils.ByteArray; 

public class socketClient 
{ 
    private var socket:Socket; 
    public function openConnection(address:String, port:int):void 
    { 
     if (socket != null && socket.connected) 
      socket.close(); 

     socket = new Socket(); 
     try { 
      socket.connect(address, port);    
     } 
     catch(e:Error) { }    
    } 
    public function sendProtocol(p:socketProtocol):void { 
     //p.serialize() gets me a bunch of bytes in a ByteArray 
     var buffer:ByteArray = p.serialize(); 
     socket.writeBytes(buffer, 0, buffer.length); 
     //Nothing happens when I flush 
     socket.flush(); 
    } 
    public function closeConnection():void { 
     //As soon as I get to socket.close(), I get this 
     //"Unhandled IOErrorEvent:. text=Error #2031: Socket Error." 
     socket.close(); 
    } 
} 

}

我使用的类是这样的:

var socket:socketClient = new socketClient(); 

//works fine, I see the connection on the server 
socket.openConnection("127.0.0.1", 10); 

//no errors, but nothing sent 
socket.sendProtocol(protocol); 

//returns the error. (if manually dismissed, data is sent) 
socket.closeConnection(); 
+0

类似问题:http://stackoverflow.com/questions/3645988/socket-error-sometimes – 2010-11-30 14:46:42

回答

0

我终于解开了它,因为我张贴的问题锤击这一个了。

我不得不从那里添加

socket.addEventListener(flash.events.Event.CLOSE, closeHandler) 

并做socket.close()。