PDA

View Full Version : XMLSocket c#


LOLFlash
07-08-2007, 05:08 PM
Hi all

I give up with java and started c# XMLSocket for Flash.


Right now what I want just send data to flash in XML view form command line in single client connection. I found some code but mostly for chat multiply client where data received and send to clients right away without XML formatting.

I want receives data from System and send it to Flash in XML.

Please any help…..

hunnysharma102
07-02-2010, 07:46 AM
I Guess It can Help You.........

C# Coding

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication9
{
public class serv
{

public static void Main() {
serv obj = new serv();
obj.hunny();

}

public void hunny()
{

try
{
IPAddress ipAd = IPAddress.Parse("127.0.0.1"); //use local m/c IP address, and use the same in the client
TcpListener myList = new TcpListener(ipAd, 8001);

myList.Start();

Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");


Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));

ASCIIEncoding asen = new ASCIIEncoding();
//s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\\nSent Acknowledgement");
s.SendTo(b, s.RemoteEndPoint);


s.Close();


myList.Stop();



}

catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}

}


}



flash Coding:

var xmlSocket:XMLSocket = new XMLSocket();
xmlSocket.connect("127.0.0.1",8001);
xmlSocket.onConnect = onConnection

function onConnection(suc){
if(suc){
trace("Successfully Connected");
sendVar()
}
else {
trace("Error in Connection")
}
}




function sendVar(){

xmlSocket.send(new XML("hemant sharma"));
xmlSocket.onData = onDataEnter

}
function onDataEnter(msg){
if(msg){
trace("Data Recieved");
xmlSocket.close();
}
else {
trace("error in collecting data");

}

}