开发者

C# UDP listener not accepting packets untill another program sends something

开发者 https://www.devze.com 2023-03-20 21:07 出处:网络
Right, hopefully a quick question & answer. My application is supposed to listen on an UDP port and from any IP Address from a certain showcontrol server. Which just sends out plain ASCII text/str

Right, hopefully a quick question & answer. My application is supposed to listen on an UDP port and from any IP Address from a certain showcontrol server. Which just sends out plain ASCII text/strings.

Since we do not have such a S.C. server at the office I also wrote a very small simple console application that just broadcasts UDP packets.

This is the console app:

    static void Main(string[] args)
    {

        Thread.Sleep(1000);

        Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint e = new IPEndPoint(IPAddress.Broadcast, 35000);

        IPAddress broadcast = IPAddress.Parse("192.168.1.255");

        e = new IPEndPoint(broadcast, 35000);

        string s;
        Byte[] sendBytes;

        while ("exit" != (s = Console.ReadLine()))
        {
            sendBytes = Encoding.ASCII.GetBytes((s.EndsWith("\n") ? s : s + "\n"));
            soc.SendTo(sendBytes, e);
        }

        sendBytes = Encoding.ASCII.GetBytes("EXIT\n");
        soc.SendTo(sendBytes, e);

        soc.Close();
    }

I've tried two things on the listener part. Using UdpClient and using a socket connection and both are giving me the exact same problem which I will describe below both my listener code:

The Socket version:

    public MediaPlayer(Output outputWindow)
    {
        Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, i_inPort);

        serverSocket.Bind(ipEndPoint);

        IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);

        EndPoint epSender = (EndPoint)ipeSender;

        SocketState state = new SocketState();
        state.e = epSender;
        state.s = serverSocket;

        beginReceiveFrom(state);
    }
    public void beginReceiveFrom(SocketState state)
    {
        state.byteData = new byte[state.byteData.Length];
        IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
        state.e = (EndPoint)ipeSender;

        state.s.BeginReceiveFrom(state.byteData, 0, state.byteData.Length, SocketFlags.None, ref state.e, onReceiveFrom, state);
    }

    public void onReceiveFrom(IAsyncResult ar)
    {
        SocketState state = (SocketState)ar.AsyncState;
        Socket serverSocket = state.s;
        //EndPoint epSender = state.e;

        IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
        EndPoint epSender = (EndPoint)ipeSender;
        try
        {
            serverSocket.EndReceiveFrom(ar, ref epSender);

            string s = Encoding.ASCII.GetString(state.byteData);
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex);
        }
        finally
        {
            beginReceiveFrom(state);
        }
    }

The UdpClient version:

    public MediaPlayer(Output outputWindow)
    {
        UdpClient client = new UdpClient(i_inPort);
        IPEndPoint endPoint = null;

        UdpState state = new UdpState();

        state.e = endPoint;
        state.u = client;
    }

    private void BeginReceive(UdpState state)
    {
        state.u.BeginReceive(new AsyncCallback(ReceiveCallback), state);
    }

    private void ReceiveCallback(IAsyncResult ar)
    {
        UdpState state = null;

        try
        {
            state = (UdpState)ar.AsyncState;

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

            executive.Receive(receiveString);

            BeginReceive(state);
        }
        catch(System.Exception ex)
        {
            SetText(ex.ToString());
        }       
    }

There is some extra code in the UpdClient ReceiveCallback version because that is the one I initially used. The problem is, is that when I am sending/broadcasting stuff with the console app within anyway in my companies network and on any computer it just works, no problem. Multiple medi开发者_StackOverflow社区aplayers can be playing on any amount of computers with no problem.

However, when I am using a different UDP sender (Something like Hercules from hw-group.com) then it simply wont work, >untill< I send one command with my console app. Only then, after sending a text/packet/data with my console app will my applcation accept data from the hercules application.

Is there anyone seeing what I am doing wrong? I've read multiple code examples, from here, codeproject and guru-something, and they are all using the same type of programming that I used.

Also, sorry for the wall of text.


probable the header of the packets it's not present in the message you're sending to the devices, double check that and make sure you message is being decoded correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消