简体   繁体   中英

UDP listener callback not being called in windows service

I have a windows service. As part of it's initialization, it calls this StartListener method, which opens up a UDP port for listening. I can confirm with netstat that the port is listening. But when I send a message across, the callback never triggers.

        //Called by service on start
        public static void StartListener(int port)
        {
            if (udpClient != null)
            {
                udpClient.Close();
                udpClient = null;
            }
            try
            {
                udpClient = new UdpClient(31050);
                udpClient.BeginReceive(new AsyncCallback(recv), null);
                ControlService.log.WriteEntry("Listening on port " + port);
            }
            catch (Exception e)
            {
                ControlService.log.WriteEntry(e.Message);
            }
        }

        public static void recv(IAsyncResult res)
        {
            ControlService.log.WriteEntry("UDP Callback");
            //do actual things
        }

        //From other app
        public void SendBytes(byte[] bytes, IPEndPoint RemoteIpEndPoint)
        {
            try
            {
                udpClient.Send(bytes, bytes.Length, RemoteIpEndPoint);
            }
            catch { ... }
        }

I am porting this from a unity app, which calls the same StartListener method, which does work. The calling app is the same in both cases.

Is there something I am missing that a Windows Service might require?

This isn't a complete answer, but hopefully will help anyone else with a similar issue.

The problem appears to be that the sender is a unity app. When I threw together a quick c# app that sends a UDP message, it get received by the service. By the same token, when I use that app to send a message to the old Unity receiver, it doesn't come through. My guess is Unity is doing something behind the scenes that's wrapping/unwrapping the messages beyond the normal UdpClient behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM