PDA

View Full Version : XMLSocket won't recieve the data.



wonsong82
December 16th, 2009, 02:01 AM
hi. this is wierd.



// action script
var socket:XMLSocket = new XMLSocket("127.0.0.1",8888);
socket.addEventListener(Event.CONNECT, connected);
socket.addEventListener(DataEvent.DATA, transData);

function connected(e:Event):void
{

}

function transData(e:DataEvent):void
{
trace ("data transmitted...");
trace (e.data);
}




//c#
TcpListener server = new TcpListener(ip, port);
server.Start();
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.getStream();
Byte[] buffer = Encoding.UTF8.GetBytes("this string won't be sent");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
}


this won't send the string "this string won't be sent" to the client(flash).
does anyone know why?

wonsong82
December 16th, 2009, 11:11 AM
hi. this is wierd.



// action script
var socket:XMLSocket = new XMLSocket("127.0.0.1",8888);
socket.addEventListener(Event.CONNECT, connected);
socket.addEventListener(DataEvent.DATA, transData);

function connected(e:Event):void
{

}

function transData(e:DataEvent):void
{
trace ("data transmitted...");
trace (e.data);
}




//c#
TcpListener server = new TcpListener(ip, port);
server.Start();
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.getStream();
Byte[] buffer = Encoding.UTF8.GetBytes("this string won't be sent");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
}


this won't send the string "this string won't be sent" to the client(flash).
does anyone know why?

Hey guys, if there was anyone interested or having the same problem but couldnt figure out why, i found out the solution.

When XMLSocket sends or receives data, it requires a nullByte terminated, which means for all data a nullByte needs to be added at the end of the string.

XMLSocket.send method automatically adds this so there is no problem if the socket server is sending back the exact data to the clients, but
if you want to send the custom written data from a socket server, you need to add a null byte at the end of the string as a mean of the data termination.

so i simply added \0 <- nullbyte and its working like a charm.

from the above code all i needed to do was


//c#
Byte[] buffer = Encoding.UTF8.GetBytes("this string won't be sent\0");