0

I followed link to create an Async TCP server. However, all examples cover only sending data as a response to receive. My question is how to Push data to a certain TCP client.

Let us say tat a byte[] needs to be sent/pushed to client Id 1. How can I achieve this using SocketAsyncEventArgs architecture?

I know I have to keep a list of active connections and client ids. What should this list look like in order to push data?

    public bool Send(Socket socket, byte[] message)
    {

        SocketAsyncEventArgs completeArgs = new SocketAsyncEventArgs();

        if (socket.Connected)
        {
            // Prepare arguments for send/receive operation.
            completeArgs.SetBuffer(message, 0, message.Length);
            completeArgs.UserToken = socket;
            completeArgs.AcceptSocket = socket;
            completeArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnIOCompleted);

            // Start sending asyncronally.
            socket.SendAsync(completeArgs);
        }


        return true;


    }
pats
  • 1,273
  • 2
  • 20
  • 43
  • Just call `socket.Send` or whatever the method is called, why would you need to worry about socket events? – DavidG Mar 30 '16 at 00:19
  • Thanks for your response. I added some code here. This is what I have so far. But Completed event of SocketAsyncEventArgs has to be registered to TCP server OnIOCompleted method in order to do post send processing. What would be the best way to keep the active connections. Dictionary of ? or . In these cases how to push and register Completed event of TCP server? – pats Mar 30 '16 at 00:32

0 Answers0