© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
23 replies
Ang619

Socket Exception when creating a UDP client, and i dont know why :(

Hello everyone,
i need help understanding why my code doesnt work.
I wrote a client-server chatroom application, and i want the client to scan the network for availible servers (chatrooms). I do this by sending a discovery UDP message from the client as broadcast, and if a server receives that message it responds with its chatroom name and its IP (The port is always the same for the application - so i dont need that information).
I get a Socket exception (normaly the Socket adress can only be used once) in the line
_udpClient = new UdpClient(_discoveryPort);
_udpClient = new UdpClient(_discoveryPort);


Here is the whole class, where the failing line is:
https://codeshare.io/r9BZDE

I do understand that i can only create a single udp client with the same port, and i do only create a single one!
This is how i create the object and call the method that throws an socket exeption:
var discovery = new ChatroomsDiscovery();
            discovery.StartDiscoveryAsync();
var discovery = new ChatroomsDiscovery();
            discovery.StartDiscoveryAsync();


Here is the whole code from the Client side in case the problems lays there.
https://codeshare.io/6pZ8Zm
Its line 45. The EstablishConnection() Method is the first method that gets called in this class, there cant be any socket with the same IP or Port (thats at least the logic in my head)

I know my question is pretty big and i hope someone knows why it fails... Thanks in advance i wish you all the best.
SocketException.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Retrieving message from a UDP socket
C#CC# / help
3y ago
I never reach the return, and i dont know why?
C#CC# / help
3y ago
❔ I can't handle a ConnectionReset Exception in an UDP Server
C#CC# / help
3y ago
✅ Done! Chartjs has one wrong entry and i dont know why
C#CC# / help
4y ago