0

The method starts with ReceiveAuthPacket being called, and I pretty much want it to listen for packets for 3 seconds then to close... which I attempted with "u.Client.ReceieveTimeout = 3000" but that isn't working.. any ideas?

        private void ReceiveAuthPacket()
        {
            IPEndPoint e = new IPEndPoint(IPAddress.Any, 5001);
            UdpClient u = new UdpClient(e);

            UdpState s = new UdpState();
            s.E = e;
            s.U = u;

            Console.WriteLine("Listening for Messages: ");
            u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
        }

        private void ReceiveCallback(IAsyncResult ar)
        {
            UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).U;
            IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).E;
            u.Client.ReceiveTimeout = 3000;

            Byte[] receiveBytes = u.EndReceive(ar, ref e);
            string receiveString = Encoding.ASCII.GetString(receiveBytes);

            Console.WriteLine("Received: {0}", receiveString);
            MessageReceived = true;

            string errMsg = "";
            if (AuthMessageParser.ParseMessage(receiveString, ref errMsg))
                Authenticated = true;
            else
            {
                ErrorMessage += errMsg;
                Authenticated = false;
            }
        }
user1189352
  • 3,628
  • 12
  • 50
  • 90
  • 1
    Please read the MSDN documentation for [Socket.ReceiveTimeout](http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.receivetimeout.aspx). It says: "specifies the amount of time after which a **synchronous** Receive call will time out." However, in your code you obviously do an asynchronous receive. (Also, see [here](http://stackoverflow.com/a/12642359/2819245)) –  May 15 '14 at 22:18
  • oh darn....... missed that... any idea how i can go about what i want to achieve? – user1189352 May 15 '14 at 22:19
  • Follow the "see here" link in my previous comment, which (unfortunately) will not give you a solution, it will however give you some pointers of how you should do it. –  May 15 '14 at 22:20

0 Answers0