Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

UDP client: an exception has appeared when trying to start BeginReceive method

$
0
0

Hi. I wanna create client-server application with UDP protocol. Server works good, but not a client. If server is offline and I trying to start BeginReceive method on client - i get an exception which tells me that before starting receive a data I have to use "Bind" method. But if i send some data to server before start BeginReceive - it's works good, BUT! If I sent the data and server is offline, then calling BeginReceive creates an exception "An existing connection was forcibly closed by the remote host". So why I have getting that exception? I do not want to use Bind method for client, and don't wanna send some data to server before using BeginReceive method, is it possible? Thanks.

Here is client code, tell me if you need server's code.

    class Program
    {
        static void Main(string[] args)
        {
            Client.Start();
            for (;;)
            {
                Client.Send(Console.ReadLine());
            }
        }
    }

    public static class Client
    {
        private static string host = "127.0.0.1";
        private static int portReceive = 9061, portSend = 9060;

        private static UdpClient client = new UdpClient(host,portSend);
        private static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        private static byte[] buffer = new byte[1024];
        private static IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(host), portSend);
        private static EndPoint endPointSend = (EndPoint)ipEndPoint;
        public static IPEndPoint endPointReceive = new IPEndPoint(IPAddress.Parse(host), 0);
        private static bool isConnected = false;

        private static int ID=0;

        public static void Start()
        {
            Send("");
            try
            {
                client.BeginReceive(new AsyncCallback(ReceiveCallback), client);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private static void ReceiveCallback(IAsyncResult ar)
        {
            UdpClient client = (UdpClient) ar.AsyncState;
            byte[] buff = client.EndReceive(ar,ref endPointReceive);
            if (ID == 0 && buff.Length == 4)
                ID=BitConverter.ToInt32(buff, 0);
            else if (ID != 0)
            {
                Console.WriteLine(Encoding.UTF8.GetString(buff, 0, buff.Length));
            }
            client.BeginReceive(new AsyncCallback(ReceiveCallback),client);
        }

        public static void Send(string message)
        {
            byte[] buffer = GetPacket(message);
            try
            {
                client.BeginSend(buffer,buffer.Length,new AsyncCallback(SendCallback),client);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private static void SendCallback(IAsyncResult ar)
        {
            UdpClient client = (UdpClient) ar.AsyncState;
            client.EndSend(ar);
        }

        public static void Connect()
        {
            string message = "connect";
            byte[] buff = Encoding.UTF8.GetBytes(message);
            socket.SendTo(buff, 0, buff.Length, SocketFlags.None, endPointSend);
        }

        static byte[] GetPacket(string s)
        {
            if (ID != 0)
            {
                byte[] b1 = Encoding.UTF8.GetBytes(s);
                byte[] buff = new byte[b1.Length+6];
                b1.CopyTo(buff,6);
                BitConverter.GetBytes(ID).CopyTo(buff,2);
                byte[] tmp=new byte[buff.Length-2];
                Array.Copy(buff,2,tmp,0,tmp.Length);
                BitConverter.GetBytes(GetHash(tmp)).CopyTo(buff,0);
                return buff;
            }
            else
            {
                return Encoding.UTF8.GetBytes("1");
            }
        }

        static ushort GetHash(byte[] data)
        {
            int lenght = data.Length;
            byte[] buff;
            if (lenght % 2 == 1)
            {
                buff = new byte[lenght + 1];
                Array.Copy(data, buff, lenght);
                buff[lenght] = 0;
            }
            else
            {
                buff = data;
            }

            ushort hash = 0;
            for (int i = 0; i < lenght; i += 2)
            {
                hash ^= (ushort)BitConverter.ToInt16(buff, i);
            }
            return hash;
        }

    }


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>