C
C#2y 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();
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.
6 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ang619
Ang619OP2y ago
thank you for the quick response, i will try your solution. Thank you really much Your solution unfortunatly didnt solve my problem. But i think i found out what causes the problem: Both the client side and the server side are unfortunatly in the same project. When the server is running, the server EndPoint seems to reserve the port on which it listens for incoming clients. Cause once i dont start the server, my discovery code works as intended. I tried to open one instance by clicking the .exe in the windows folder, and start another instance from VS, but then the error still occurs. I need to further think about this.
Buddy
Buddy2y ago
The clients should bind to a random available port, not a fixed one.
Ang619
Ang619OP2y ago
Unfortunatly i cant do that. The application should communicate on a specific port.
Buddy
Buddy2y ago
Use trackers if it's p2p Which helps client A find client B, and so forth When you want to Receive is when you plug in udp servers Ip. Udp is not similar to tcp
Ang619
Ang619OP2y ago
I found the solution to my problem: I changed _udpClient = new UdpClient(_discoveryPort); to
_udpClient = new UdpClient();
_udpClient.EnableBroadcast = true;
_udpClient = new UdpClient();
_udpClient.EnableBroadcast = true;
Then later i can use it like that await _udpClient.SendAsync(discoveryMessage, discoveryMessage.Length, new IPEndPoint(IPAddress.Broadcast, _discoveryPort));
Want results from more Discord servers?
Add your server