Exemplo n.º 1
0
 public Server()
 {
     _udpState           = new UdpState();
     _udpState.ServerEP  = new IPEndPoint(IPAddress.IPv6Any, 80);
     _udpState.UdpClient = new UdpClient(_udpState.ServerEP);
     //_udpState.udpClient.Client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false); //accept IPv6 or IPv4 etc.
     _messenger = new Messenger();
 }
Exemplo n.º 2
0
        private void ListenerCallBack(IAsyncResult asyncResult)
        {
            UdpState udpState = (UdpState)asyncResult.AsyncState;

            byte[] receivedBytes  = udpState.UdpClient.EndReceive(asyncResult, ref udpState.ServerEP);
            string receivedString = Encoding.ASCII.GetString(receivedBytes);

            _messenger.ProcessMessage(receivedString, udpState);

            Console.WriteLine($"Received message {receivedString} from client at IP address: {udpState.ServerEP.Address}");
        }
Exemplo n.º 3
0
        private void ListenToAll()
        {
            IPEndPoint e = new IPEndPoint(IPAddress.Any, SERVER_PORT);
            UdpClient  u = new UdpClient(e);

            while (true)
            {
                messageReceived = false;

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

                Console.WriteLine("listening for messages");
                u.BeginReceive(new AsyncCallback(ReceiveCallback), s);

                while (!messageReceived)
                {
                    Thread.Sleep(50);
                }
            }
        }