Diom
Diom

Reputation: 111

Sending data from server

I tried to send data to client using this function

protected void SendData(Socket so, string sendData)
{
    byte[] data = Encoding.ASCII.GetBytes(sendData);
    so.Send(data);

    Console.WriteLine("Sending data back to Client\n");
}

I call this function on this function

protected void ProcessData(int x, string data, Socket so)
{
    if (x < mybuffersize)
    {
        data = data.Trim();
        SendData(so, data);
    }
}

its work well.. and send the data that server received from client. but when I changed it to this,

protected void ProcessData(int x, string data, Socket so)
{
    if (x < mybuffersize)
    {
        data = data.Trim();
        SendData(so, data);
        string sendData = "Testing send string";
        SendData(so, sendData);
    }
}

Client will only receive the data he send to me, but not the string Testing send string. Am I doing something wrongly or are there any restriction?

Upvotes: 2

Views: 4149

Answers (3)

ja_mesa
ja_mesa

Reputation: 1969

Because we don't know what the server does nor how it gets the data, can you please try this and see what the server gets:

protected void ProcessData(int x, string data, Socket so)   
{
    if (x < mybuffersize)
    {
        string sendData = "Testing send string\0";
        SendData(so, sendData);
        data = data.Trim();
        SendData(so, data);
    } 
}

Upvotes: 0

Diom
Diom

Reputation: 111

I found out that the problem is with sending the data,

it seems my client(flash) will receive the string if i add something like this:

protected void ProcessData(int x, string data, Socket so)
{
    if (x < mybuffersize)
    {
        data = data.Trim();
        SendData(so, data);
        string sendData = "Testing send string\0";
        SendData(so, sendData);
    }
}

if anyone can explain why, please add at the comment of this answer, thx

Upvotes: 0

Ramesh Durai
Ramesh Durai

Reputation: 2686

We can use TCP messages to send data to client using asynchronous communication.

Initialize Server Socket and try to connect:

// Specify the size according to your need.
private byte[] bytData = new byte[1024 * 50000];
private Socket oServerSocket;

private void WindowLoaded(object sender, RoutedEventArgs e)
{
    try
    {
        // We are using TCP sockets
        this.oServerSocket = new Socket(
        addressFamily: AddressFamily.InterNetwork,
        socketType: SocketType.Stream,
        protocolType: ProtocolType.Tcp);

        // Assign the any IP of the hardware and listen on port number which the hardware is sending(here it's 5656)
        var oIPEndPoint = new IPEndPoint(address: IPAddress.Any, port: 5656);

        // Bind and listen on the given address
        this.oServerSocket.Bind(localEP: oIPEndPoint);
        this.oServerSocket.Listen(backlog: 4);

        // Accept the incoming clients
        this.oServerSocket.BeginAccept(this.OnAccept, null);
    }
    catch (Exception ex)
    {
        // Handle Exception
    }
}

On Successful connection:

private void OnAccept(IAsyncResult ar)
{
    try
    {
        var oClientSocket = this.oServerSocket.EndAccept(asyncResult: ar);

        // Start listening for more clients
        this.oServerSocket.BeginAccept(callback: this.OnAccept, state: null);

        // Once the client connects then start receiving the commands from her
        oClientSocket.BeginReceive(
        buffer: this.bytData,
        offset: 0,
        size: this.bytData.Length,
        socketFlags: SocketFlags.None,
        callback: this.OnReceive,
        state: oClientSocket);
    }
    catch (Exception ex)
    {
        // Handle Exception
    }
}

Process the message received:

private void OnReceive(IAsyncResult ar)
{
    try
    {
        var oClientSocket = (Socket)ar.AsyncState;
        oClientSocket.EndReceive(asyncResult: ar);

        /* Process the data here

        BitConverter.ToInt32(value: this.bytData, startIndex: 0);
        string SomeString= Encoding.ASCII.GetString(bytes: this.bytData, index: 8, count: 5);

        */

        // Specify the size according to your need.
        this.bytData = null;
        this.bytData = new byte[1024 * 50000];
        oClientSocket.BeginReceive(
            buffer: this.bytData,
            offset: 0,
            size: this.bytData.Length,
            socketFlags: SocketFlags.None,
            callback: this.OnReceive,
            state: oClientSocket);
    }

    catch (Exception ex)
    {
        // Handle Exception
    }
}

Upvotes: 1

Related Questions