我在使用 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();