Flex - Socket.close()问题(#2031:套接字错误)

时间:2010-11-30 14:35:15

标签: sockets flex4 flashbuilder4

我在使用Flash Builder 4中的套接字时遇到问题。下面的代码将一组字节发送到接收c#sockerServer。如果我解除了我在flash Builder中手动获得的错误,那么字节就会很好地发送,所有这些都会在127.0.0.1:10上得到应用。现在,如果我可以获得相同的结果,而不会在Flex中显示错误。

所以,我有两个问题:

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(); 

1 个答案:

答案 0 :(得分:0)

自从我发布问题以来,我终于解决了这个问题。

我不得不添加一个

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

从那里做socket.close()。

相关问题