C
C#5mo ago
MeGaLoDoN

I need help debugging c# event-based reliable UDP implementation.

Note: I know that there are libraries that can do this and even more. But for my own reasons, I want to have full control of my code, and it is already almost done and supposed to work, I just have 1 bug. Here is code: https://pastebin.com/dXx6WPG9 Basically, OnConnectionRequest gets called correctly on the server. Then I call request.Accept, but client never receives a response and connection result is false, so OnConnected never gets called. This is not only with accept message - I tried sending a lot different messages from server to client, but client never receives any message, although server always receives messages from the client. The client is always stuck on: var msg = await _client.ReceiveAsync(token); in CheckForMessages.
Pastebin
UDP code - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
5 Replies
canton7
canton75mo ago
You're doing an awful lot of calling Task-returning functions and then throwing away the Task. Any exceptions contained in the Task will just be lost. So your whole system might be falling over and you'd never know
MeGaLoDoN
MeGaLoDoN5mo ago
I don't think I have any exceptions. I tried putting a lot of try catch before and putting break points in catch, but it never entered it. It just doesn't receive messages I will add exception handling later, I just want to get it work now
canton7
canton75mo ago
There's a lot of code there. Break the problem down a bit: is the issue that the server never sends the message, or the client never receives it? A bit of logging would help you out a lot there! I'd probably also have Wireshark open for debugging this
Lex Li
Lex Li5mo ago
"Note: I know that there are libraries that can do this and even more. But for my own reasons, I want to have full control of my code, and it is already almost done and supposed to work, I just have 1 bug." Don't be over-confident. Socket programming is known to be challenging and difficult to go right. People use well established frameworks for good reasons. BTW, UDP is connectionless, so you misused quite a few things in your code.
MeGaLoDoN
MeGaLoDoN5mo ago
What I missed I just checked with wireshark - server sends messages correctly, just client doesnt receive them So I am dumb and I was running program in release mod, and thats way break point was not working. I wrapped CheckForMessages in try catch, and I found that I get this exception on the client: "System.InvalidOperationException: You must call the Bind method before performing this operation. at System.Net.Sockets.Socket.ValidateReceiveFromEndpointAndState(EndPoint remoteEndPoint, String remoteEndPointArgumentName) at System.Net.Sockets.Socket.ReceiveFromAsync(Memory`1 buffer, SocketFlags socketFlags, EndPoint remoteEndPoint, CancellationToken cancellationToken) at System.Net.Sockets.UdpClient.ReceiveAsync(CancellationToken cancellationToken) at UdpPeer.CheckForMessages(CancellationToken token) " How to fix it? what if I dont want to bind it to specific port? @Lex Li If I bind it works. But I don't want to bind it, because what if client is behind NAT?. Receive is supposed to work without binding Binding the client to port 0 solved it. I don't know why it doesnt happen automaticly