如果没有看到您的实现代码,听起来好像可用的字节数或总字节数没有被考虑在内。
这是我使用的一个实现:
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
public class WebRequestService extends EventDispatcher
{
//------------------------------
// model
//------------------------------
protected var hostname:String;
protected var port:uint;
public var requestMessage:String;
public var responseMessage:String;
private var _socket:Socket;
//------------------------------
// lifecycle
//------------------------------
public function WebRequestService(hostname:String, port:uint, requestMessage:String)
{
this.hostname = hostname;
this.port = port;
this.requestMessage = requestMessage;
_socket = new Socket();
_socket.addEventListener(Event.CONNECT, socketConnectHandler);
_socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
_socket.addEventListener(Event.CLOSE, socketCloseHandler);
_socket.addEventListener(IOErrorEvent.IO_ERROR, socketErrorHandler);
_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketSecurityErrorHandler);
_socket.connect(hostname, port);
}
protected function socketConnectHandler(event:Event):void
{
responseMessage = "";
_socket.writeUTFBytes(requestMessage);
_socket.flush();
}
protected function socketDataHandler(event:ProgressEvent):void
{
while (_socket.bytesAvailable > 4)
{
responseMessage += _socket.readUTFBytes(_socket.bytesAvailable);
}
}
protected function socketCloseHandler(event:Event):void
{
dispose();
}
protected function socketErrorHandler(event:IOErrorEvent):void
{
trace("socket error.");
}
protected function socketSecurityErrorHandler(event:SecurityErrorEvent):void
{
trace("socket security error.");
}
public function dispose():void
{
if (!_socket)
return;
if (_socket.connected)
_socket.close();
_socket.removeEventListener(Event.CONNECT, socketConnectHandler);
_socket.removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
_socket.removeEventListener(Event.CLOSE, socketCloseHandler);
_socket.removeEventListener(IOErrorEvent.IO_ERROR, socketErrorHandler);
_socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, socketSecurityErrorHandler);
}
}
}