Mark Denom
Mark Denom

Reputation: 1057

How do I keep accepting data on my async socket?

So I've setup a very basic server and a client, utilizing the Socket class in C#. I did one earlier where it wasnt async so I decided to try it out.

The issue I am having is that I keep sending data from the server to the client, but the client only gets the first one and then it's as if it stops listening. What ways are there to keep checking if there is incoming data and then if there is just grab it?

Client

private static Socket _clientSocket;
        private static byte[] buffer = new byte[2048];

        static void Main(string[] args)
        {
            Connect();
            Console.ReadLine();
        }

        private static void Connect()
        {
            try
            {
                _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _clientSocket.BeginConnect(new IPEndPoint(IPAddress.Loopback, 1234), new AsyncCallback(ConnectCallback), null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        private static void ConnectCallback(IAsyncResult AR)
        {
            try
            {
                _clientSocket.EndConnect(AR);
                Console.WriteLine("Connected to the server");


                _clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None,
                    new AsyncCallback(ReceiveCallback), _clientSocket);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        private static void ReceiveCallback(IAsyncResult AR)
        {
            Socket clientSocket = AR.AsyncState as Socket;
            var length = clientSocket.EndReceive(AR);

            byte[] packet = new byte[length];
            Array.Copy(buffer, packet, packet.Length);


            var data = Encoding.ASCII.GetString(packet);
            Console.WriteLine("Received " + data);
        }

And the server

private static Socket _clientSocket;
        private static byte[] buffer = new byte[2048];

        static void Main(string[] args)
        {
            Connect();
            Console.ReadLine();
        }

        private static void Connect()
        {
            try
            {
                _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _clientSocket.BeginConnect(new IPEndPoint(IPAddress.Loopback, 1234), new AsyncCallback(ConnectCallback), null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        private static void ConnectCallback(IAsyncResult AR)
        {
            try
            {
                _clientSocket.EndConnect(AR);
                Console.WriteLine("Connected to the server");


                _clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None,
                    new AsyncCallback(ReceiveCallback), _clientSocket);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        private static void ReceiveCallback(IAsyncResult AR)
        {
            Socket clientSocket = AR.AsyncState as Socket;
            var length = clientSocket.EndReceive(AR);

            byte[] packet = new byte[length];
            Array.Copy(buffer, packet, packet.Length);


            var data = Encoding.ASCII.GetString(packet);
            Console.WriteLine("Received " + data);
        }

Upvotes: 1

Views: 104

Answers (1)

Klaus Gütter
Klaus Gütter

Reputation: 11977

In your ReceiveCallback, call BeginReceive again to continue receiving.

Upvotes: 2

Related Questions