Socket error shown.

   public Communicator(IPAddress toconnectto)
        {
            this.toconnectto = new IPEndPoint(toconnectto, port);
            local = new IPEndPoint(Tools.GetIp(), port);
            all = new IPEndPoint(IPAddress.Any, port); 
            udpClient = new UdpClient(AddressFamily.InterNetwork);
            udpClient.Client.Bind(new IPEndPoint(Tools.GetIp(), port));
            udpClient.AllowNatTraversal(true);
            udpClient.EnableBroadcast = true;
        }

        public void SendData(string data)
        {
            try
            {
                byte[] bdata = ASCIIEncoding.ASCII.GetBytes(data);
                udpClient.Send(bdata,bdata.Length,toconnectto);
            }
            catch(Exception ex)
            {
                ;
            }
        }
           public Communicator(IPAddress toconnectto)
    {
        this.toconnectto = new IPEndPoint(toconnectto, port);
        local = new IPEndPoint(Tools.GetIp(), port);
        all = new IPEndPoint(IPAddress.Any, port); 
        udpClient = new UdpClient(AddressFamily.InterNetwork);
        udpClient.Client.Bind(new IPEndPoint(Tools.GetIp(), port));
        udpClient.AllowNatTraversal(true);
        udpClient.EnableBroadcast = true;
    }

    public void SendData(string data)
    {
        try
        {
            byte[] bdata = ASCIIEncoding.ASCII.GetBytes(data);
            udpClient.Send(bdata,bdata.Length,toconnectto);
        }
        catch(Exception ex)
        {
            ;
        }
    }   public Communicator(IPAddress toconnectto)
    {
        this.toconnectto = new IPEndPoint(toconnectto, port);
        local = new IPEndPoint(Tools.GetIp(), port);
        all = new IPEndPoint(IPAddress.Any, port); 
        udpClient = new UdpClient(AddressFamily.InterNetwork);
        udpClient.Client.Bind(new IPEndPoint(Tools.GetIp(), port));
        udpClient.AllowNatTraversal(true);
        udpClient.EnableBroadcast = true;
    }

    public void SendData(string data)
    {
        try
        {
            byte[] bdata = ASCIIEncoding.ASCII.GetBytes(data);
            udpClient.Send(bdata,bdata.Length,toconnectto);
        }
        catch(Exception ex)
        {
            ;
        }
    }   public Communicator(IPAddress toconnectto)
    {
        this.toconnectto = new IPEndPoint(toconnectto, port);
        local = new IPEndPoint(Tools.GetIp(), port);
        all = new IPEndPoint(IPAddress.Any, port); 
        udpClient = new UdpClient(AddressFamily.InterNetwork);
        udpClient.Client.Bind(new IPEndPoint(Tools.GetIp(), port));
        udpClient.AllowNatTraversal(true);
        udpClient.EnableBroadcast = true;
    }

    public void SendData(string data)
    {
        try
        {
            byte[] bdata = ASCIIEncoding.ASCII.GetBytes(data);
            udpClient.Send(bdata,bdata.Length,toconnectto);
        }
        catch(Exception ex)
        {
            ;
        }
    }

Dani AI

Generated

is spot on: the empty catch blocks remove the single most useful diagnostic detail. Logging the full exception (ex.ToString()) and, for SocketException, the SocketErrorCode will reveal the real failure instead of masking it.

Common causes to check (quick checklist):

  • Binding: binding to a specific local IP that is not present will fail. Prefer binding to IPAddress.Any or avoid binding at all when only sending. Confirm the value returned by Tools.GetIp() is actually assigned to a local interface.
  • Port conflicts: binding a port already in use yields AddressAlreadyInUse. Multiple UdpClient instances bound to the same port can collide unless socket reuse is explicitly handled.
  • Broadcast vs unicast: EnableBroadcast = true is only meaningful when sending to a broadcast address. It does not fix routing or NAT issues for unicast targets.
  • Firewall / NAT: UDP can be blocked by host firewalls or upstream NAT. Verify on the same LAN first (or localhost) before testing across networks.
  • Resource lifecycle: make sure the UdpClient is not disposed before Send is called and that sockets are closed/disposed between runs.

Safer send pattern and minimal logging example:

using (var udp = new UdpClient())
{
    udp.EnableBroadcast = true;
    udp.Connect(remoteIpAddress, remotePort);
    var payload = System.Text.Encoding.UTF8.GetBytes(message);
    try
    {
        udp.Send(payload, payload.Length);
    }
    catch (SocketException sx)
    {
        Console.WriteLine($"SocketException: {sx.SocketErrorCode} - {sx.Message}");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Practical debug steps: try sending to 127.0.0.1 to confirm basic code works, use netstat/ss to inspect bound sockets, capture packets with Wireshark to see if any UDP leaves the host, and check firewall rules on sender and receiver. The full exception text and any SocketErrorCode are the most useful diagnostics for narrowing a socket error.

Before you do anything else fix your empty catch blocks by printing the complete details of any exception.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.