这是对previous问题的跟进,这个问题在整个地方都有。
我挖得更深一些,似乎我的Flash客户端无法建立连接(在调用connect()
后,connected
仍然设置为false)但是没有触发任何相关的错误事件或我可以看到的任何例外...
奇怪的是,我仍然能够将数据发送到服务器,而不是从接收数据。此外,服务器本身(用C语言编写)似乎非常高兴有连接...非常奇怪。最后,虽然我可以将数据发送到服务器,但它最终似乎有一点点胡言乱语。不确定这是否相关,但它可能是一个比我更了解这些东西的人的线索:)
无论如何,这是最相关的文件...... ChessSocket.as:
import flash.events.*;
import flash.errors.*;
import flash.net.Socket;
public class ChessSocket extends Socket {
private var response:String;
public function ChessSocket(host:String = null, port:uint = 0) {
super();
trace("ChessSocket initialising");
configureListeners();
if (host && port){
trace("Attempting to connect to "+host+" port:"+port+"...");
try {
super.connect(host, port);
}
catch(e:Error) {
trace(e);
}
if (connected)
trace("...connected!");
else
trace("...not connected.");
}
writeln("data please!");
sendRequest();
}
private function configureListeners():void {
trace("configureListeners");
addEventListener(Event.CLOSE, closeHandler);
addEventListener(Event.CONNECT, connectHandler);
addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
trace("...listeners added!");
}
private function writeln(str:String):void {
str += "\n";
try {
writeUTFBytes(str);
}
catch(e:IOError) {
trace(e);
}
}
private function sendRequest():void {
trace("sendRequest");
response = "";
writeln("GET /");
flush();
}
private function readResponse():void {
var str:String = readUTFBytes(bytesAvailable);
trace("readResponse: " + str);
response += str;
}
private function closeHandler(event:Event):void {
trace("closeHandler: " + event);
trace(response.toString());
}
private function connectHandler(event:Event):void {
trace("connectHandler: " + event);
sendRequest();
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function socketDataHandler(event:ProgressEvent):void {
trace("socketDataHandler: " + event);
readResponse();
}
}
这是我运行时得到的那种跟踪输出:
ChessSocket initialising
configureListeners
...listeners added!
Connecting to 192.168.1.4 port:27015
...not connected.
sendRequest
我的C服务器(在另一台机器上运行)输出以下内容:
Ready to accept a connection...
当我启动swf时:
I think we have a connection...
Waiting for message...
Bytes received: 25
Data received: data please!
GET /
GET /
°»â(░ ¸☺
About to send data...
Bytes sent: 25
Waiting for message...
然后当我关闭swf:
About to send data...
Bytes sent: 25
exiting...
最后,swf正在OSX笔记本电脑上运行,我已经研究过是否存在防火墙问题,但还没有发现任何问题。最奇怪的是,当我打电话给connect()
时,它似乎没有做任何事......