C# UDP Socket Issue

void Start() {    

        UdpClient u = new UdpClient();
        u.Client.ConnectAsync("127.0.0.1", 9050);
        Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
        u.SendAsync(sendBytes, sendBytes.Length);
        StartNetworking();

    }

    public async Task StartNetworking()
    {

        var receivedResults = await u.ReceiveAsync();
        Debug.Log(receivedResults.ToString());
        StartNetworking();

    }

Here is a client UDP script. It’s able to send messages properly but it isn’t able to receive them at all.

Here is my server script, which is able to receive messages just fine.

void Start() {

         u = new UdpClient(9050);
         StartNetworking();

    }

    public async Task StartNetworking()
    {

        var receivedResults = await u.ReceiveAsync();
        Byte[] sendBytes = Encoding.ASCII.GetBytes("Send 2 Client");
        u.SendAsync(sendBytes, sendBytes.Length, receivedResults.RemoteEndPoint);    
        StartNetworking();

    }

Any help would be greatly appreciated.

I also modified my firewall to not block any connections to unity. Did not work.

Fixed. Issue seemed to be that I was creating a new udpclient in start, and StartNetworking() had no info about it, hence not able to receive messages.

Yep, if you have a command in a Start() method that’s looking for something else, that something else needs to be created earlier, in its Awake() etc.