TCP Socket Packet Loss?
So I'm making a Flash game and it seems to be having packet loss issues. I'm using socket and have an AIR application that is the server, and though the clients communicate with each other, occasionally one of the messages will apparently not reach one of them. This is especially odd considering that I have a system that, when a key is pressed on one of the clients, it sends a message to the server, and then the server sends that signal back to both of them in order for anything to actually take effect in the game. Like ONE of the clients won't get the message but the other one will. I just don't understand what's going on.
Here is the server:
var security:URLLoader = new URLLoader();
var securityContent:XML;
security.load(new URLRequest("security-app.xml"));
security.addEventListener(Event.COMPLETE, loadComplete);
function loadComplete(e:Event):void
{
securityContent = XML(security.data);
securityContent.ignoreWhite = true;
security.removeEventListener(Event.COMPLETE, loadComplete);
}
import flash.net.URLLoader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.ServerSocketConnectEvent;
import flash.net.ServerSocket;
import flash.net.Socket;
import flash.utils.ByteArray;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
var serverSocket:ServerSocket;
var clientSockets:Array = new Array();
var tf:TextField = new TextField();
tf.wordWrap = true;
tf.width = 550;
tf.height = 200;
tf.autoSize = TextFieldAutoSize.NONE;
sp_com.addChild(tf);
ServerSocketExample();
function log(str)
{
tf.appendText("\n" + str);
}
function ServerSocketExample()
{
try
{
serverSocket = new ServerSocket();
serverSocket.addEventListener( Event.CONNECT, connectHandler );
serverSocket.addEventListener( Event.CLOSE, onClose );
serverSocket.bind(8080);
serverSocket.listen();
log( "Listening on port " + serverSocket.localPort + ".");
}
catch (e:SecurityError)
{
log(e);
}
}
function connectHandler(event:ServerSocketConnectEvent):void
{
var socket:Socket = event.socket as Socket;
clientSockets.push( socket );
socket.addEventListener( ProgressEvent.SOCKET_DATA, socketDataHandler);
socket.addEventListener( Event.CLOSE, onClientClose );
socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError );
log( "A client has successfully connected to the server.");
}
function socketDataHandler(event:ProgressEvent):void
{
var socket:Socket = event.target as Socket;
var message:String = socket.readUTFBytes(socket.bytesAvailable);
for each (var socket:Socket in clientSockets)
{
socket.writeUTFBytes(message);
socket.flush();
continue;
}
}
function onClientClose( event:Event ):void
{
log( "A client has disconnected from the server." );
}
function onIOError( errorEvent:IOErrorEvent ):void
{
log( "IOError: " + errorEvent.text );
}
function onClose( event:Event ):void
{
log( "Server closed." );
var socket:Socket = event.target as Socket;
var message:String = socket.readUTF();
socket = null();
socket.writeUTFBytes(null);
socket.flush();
socket.close();
}
And on the server, when certain conditions are met...
sock.writeUTFBytes(msg);
sock.flush();
function onSocketData(e:ProgressEvent):void
{
if (sock.bytesAvailable > 0)
{
var msg:String = sock.readUTFBytes(sock.bytesAvailable);
...
}
}
What is going on here??? This is really odd considering that TCP is supposed to be lossless. This is especially frustrating to me since I just got done using UDP, and after finally getting that to work, apparently it is just a lossy protocol. So even though I switched to this, I don't see much improvement... any ideas? Thank you.
