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

发布于 2024-10-05 13:46:53 字数 1641 浏览 6 评论 0原文

我在 flash builder 4 中使用套接字时遇到问题。下面的代码将一组字节发送到接收 c# sockerServer。如果我手动消除在 Flash 构建器中遇到的错误,则字节会正常发送,并且所有内容都会在 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(); 

I'm having an issue using sockets in flash builder 4. The code below sends a set of bytes to a receiving c# sockerServer. If I dismiss the error I get in flash builder manually, the bytes are sent fine and all comes across as it should on 127.0.0.1:10. Now if I could just get the same results without an error being displayed in Flex.

So, I have two questions:

1) Why does it return an error when I try to close the socket? See closeConnection() below for context. I tried flushing it just before which didn't help.

2) Why is nothing sent when I use 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();
    }
}

}

I use the class like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-10-12 13:46:53

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

我必须添加一个

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

并从那里执行 socket.close() 。

I finally solved it after hammering this one since I posted the question.

I had to add a

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

and do the socket.close() from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文