tags:

views:

67

answers:

1

Hi everyone, I'm working on a chat/IM system for my game (which is written in Flex) and wanted to connect it to my server (which is written in C#) via sockets.

So, I've successfully connected them together using XMLSocket on Flex, and Socket on the server side, the client gets a connected event, sends data correctly, but when I try to send back data from the server to the client, nothing happens (even though the BeginSend callback shows that the entire buffer was sent).

Client:

private var hostName:String = "localhost";
    private var port:uint = 4444;
    private var socket:XMLSocket;
    public var app:DotNETServerTest; 

    public function DotNetSocketExample() {
        socket = new XMLSocket();
        configureListeners(socket);
        socket.connect(hostName, port);
    }

    public function send(data:Object):void {
        socket.send(data);
    }

    public function disconnect():void {
        socket.close();
        closeHandler(null);
    }      

    private function configureListeners(dispatcher:IEventDispatcher):void {
        dispatcher.addEventListener(Event.CLOSE, closeHandler);
        dispatcher.addEventListener(Event.CONNECT, connectHandler);
        dispatcher.addEventListener(DataEvent.DATA, dataHandler);
    }

    private function closeHandler(event:Event):void {
        trace("closeHandler: " + event);
        app.send_btn.enabled = false;
        app.disconnect_btn.enabled = false;
    }

    private function connectHandler(event:Event):void {
        trace("connectHandler: " + event);
        app.send_btn.enabled = true;
        app.disconnect_btn.enabled = true;
    }

    private function dataHandler(event:DataEvent):void {
        trace("dataHandler: " + event);
        Alert.show(event.data);
    }

Server (Only specific parts):

// This is the call back function, which will be invoked when a client is connected
    public static void OnClientConnect(IAsyncResult asyn)
    {
        try
        {
            SocketClient NewConnection = new SocketClient(SocketServer.EndAccept(asyn));

            // This is a test message I'm attempting to send
            SendMessageTo(NewConnection, "<test></test>");
            Clients.Add(NewConnection);
            WaitForData(NewConnection);
            LogMessage(NewConnection, "Client # {0} connected", Clients.Count);

            SocketServer.BeginAccept(new AsyncCallback(OnClientConnect), null);
        }
        catch (ObjectDisposedException)
        {
            LogMessage("OnClientConnection: Socket has been closed.");
        }
        catch (SocketException se)
        {
            Console.WriteLine(se.Message);
        }
    }

    internal static void SendMessageTo(SocketClient client, string Data)
    {
        byte[] byteData = Encoding.ASCII.GetBytes(Data);
        SendMessageTo(client, byteData);
    }

    internal static void SendMessageTo(SocketClient client, byte[] byteData)
    {
        try
        {
            client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);
        }
        catch (Exception e)
        {
            LogMessage(client, "Error sending data to client: {0}", e.Message);
        }
    }

    internal static void SendCallback(IAsyncResult ar)
    {
        // Retrieve the socket from the async state object.
        SocketClient handler = (SocketClient)ar.AsyncState;
        try
        {
            int bytesSent = handler.socket.EndSend(ar);
        }
        catch (Exception e)
        {
            LogMessage(handler, e.Message);
        }
    }

Please help! Thanks, Ron

A: 

Found the problem - I didn't send out \0 at the end of each send.

Changed this:

byte[] byteData = Encoding.ASCII.GetBytes(Data);
client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);

To this:

byte[] byteData = Encoding.ASCII.GetBytes(Data + "\0");
client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);
Ron Rejwan