C#C
C#3y ago
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);

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();


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
Was this page helpful?