Exemplo n.º 1
0
        private void ReadCallback(IAsyncResult ar)
        {
            Console.WriteLine("Reading data");

            // Get the socket input buffer from the callback result
            HS_SocketInputBuffer state = (HS_SocketInputBuffer)ar.AsyncState;
            Socket socket = state.TargetSocket;

            // Read data from the client
            int bytesRead = socket.EndReceive(ar);

            if (bytesRead > 0)
            {
                state.AppendData(Encoding.UTF8.GetString(state.Buffer, 0, bytesRead));

                // Pop a message if one is available
                if (state.HasMessage)
                {
                    string message = state.PopMessage();
                    Console.WriteLine("Message: {0}", message);
                    callback(message);
                }

                //Keep reading
                socket.BeginReceive(state.Buffer, 0, HS_SocketInputBuffer.BufferSize, 0,
                                    new AsyncCallback(ReadCallback), state);
            }
        }
Exemplo n.º 2
0
        public HS_SocketInputWorker(Socket socket, HS_MessageCallback callback)
        {
            this.callback = callback;

            //Start the reader listening on the socket
            HS_SocketInputBuffer state = new HS_SocketInputBuffer(socket);

            socket.BeginReceive(state.Buffer, 0, HS_SocketInputBuffer.BufferSize, 0,
                                new AsyncCallback(ReadCallback), state);
        }