• Anyone here worked with UDPClients? (C#)
    24 replies, posted
Im working on networking for my project. I have a UDPClient for the server and then one for the client (separate programs) I set up a simple example where the client can send a connection request, then the server ping the client ever ~4 seconds and waits for a reply. This works fine, even with multiple clients (as it was set up to work with them) but I have an issue where if I have 2 or more clients connected to the server and one of the clients disconnects then the server stops receiving data from all other connected clients. I have searched my code like a mad man and found no reason for this so I suspect its something to do with UDPClient that I missed. As I mentioned it all works until a client disconnects, I check my code and it still runs as should but does udpclient.receive does not read any data. after printing out udpclient.Available it shows that the data is building up but isnt getting read. Does anyone have any idea for why this may be happening?
Why are you using UDP? You should be using TCP sockets. Look up the difference between UDP and TCP and the first couple sentences should answer your question.
[QUOTE=brianosaur;50806302]Why are you using UDP? You should be using TCP sockets. Look up the difference between UDP and TCP and the first couple sentences should answer your question.[/QUOTE] Yeh, But I thought UDP was used for games?
[QUOTE=0V3RR1D3;50806353]Yeh, But I thought UDP was used for games?[/QUOTE] only when you need to basically chuck shittons of packets over the internet without any verification whether or not they actually arrived as you sent them
And if speed matters :)
Well I was writting a networking library to use with my real time games. Should I just go all tcp, or both?
Without seeing your code it's hard to diagnose the issue. Anyways, Any reason you want to write your own? There are already plenty of networking libraries that implement a reliable/stream layer on top of UDP(E.g. [url="https://github.com/lidgren/lidgren-network-gen3"]lidgren[/url]).
[QUOTE=high;50808075]Without seeing your code it's hard to diagnose the issue. Anyways, Any reason you want to write your own? There are already plenty of networking libraries that implement a reliable/stream layer on top of UDP(E.g. [url="https://github.com/lidgren/lidgren-network-gen3"]lidgren[/url]).[/QUOTE] Learning reasons and because I want some special features, but showing code wont really help much. Litteraly running the example code provided by Microsoft about UdpClients has the same issue.
[QUOTE=0V3RR1D3;50808119]Learning reasons and because I want some special features, but showing code wont really help much. Litteraly running the example code provided by Microsoft about UdpClients has the same issue.[/QUOTE] Which example on msdn has this problem?
[QUOTE=high;50808360]Which example on msdn has this problem?[/QUOTE] it received request from any client that sends them, but if an instance of a client closes, the server ignros all other requests from clients. [url]https://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient(v=vs.110).aspx[/url]
[QUOTE=0V3RR1D3;50808638]it received request from any client that sends them, but if an instance of a client closes, the server ignros all other requests from clients. [url]https://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient(v=vs.110).aspx[/url][/QUOTE] Are you ignoring an exception? That code works fine. The 1 exception you will tend to hit with that code is a "connection refused" exception. You will hit that exception if you send a packet to a non-listening port(If the server has ICMP PORT_UNREACHABLE enabled). You can disable that feature on the client by turning off SIO_UDP_CONNRESET (see: [url="https://github.com/lidgren/lidgren-network-gen3/blob/27b3d8e3863c2a37dc6b76a8a7db47e03cc8a4ef/Lidgren.Network/NetPeer.Internal.cs#L137"]here[/url] for code)
[QUOTE=high;50809860]Are you ignoring an exception? That code works fine. The 1 exception you will tend to hit with that code is a "connection refused" exception. You will hit that exception if you send a packet to a non-listening port(If the server has ICMP PORT_UNREACHABLE enabled). You can disable that feature on the client by turning off SIO_UDP_CONNRESET (see: [url="https://github.com/lidgren/lidgren-network-gen3/blob/27b3d8e3863c2a37dc6b76a8a7db47e03cc8a4ef/Lidgren.Network/NetPeer.Internal.cs#L137"]here[/url] for code)[/QUOTE] No I catch and trow any exceptions. As mentioned it works fine, when multiple clients connect it still works fine, its only if one client disconnects (or the program closes) that all other clients messages are ignored.
What do you mean by connect and disconnect? UDP is connectionless. It would also help if you shared some code.
[QUOTE=brianosaur;50813475]What do you mean by connect and disconnect? UDP is connectionless. It would also help if you shared some code.[/QUOTE] Yeh, I have something in the code that basicly says "Oh hey, im here" and them pings the address every 4 seconds, if no reply is received the then client is "disconnected". Thats not the issue though, the issue is that the server's UDPClient stops receiving stuff from any other client's UDPClient, so essentialy is I destroy an instance of UDPClient, even if its in a separate running application it seems that all stop communicating with each other.
Hmm, The issue seems to be fixed after coming back to it a few days later, it appears the bug must have been with either windows or somethign conflicting with it as without changing any code it worked. but I have one more questions if anyone can answer it for me. Do I need both TCP and UDP in my game, or is ok to use just UDP. I know its not 100% garanteed to get there but if I implement checks to make sure important ones make it then there is no need to implement TCP right? or do I do both.
[QUOTE=0V3RR1D3;50853079]Do I need both TCP and UDP in my game, or is ok to use just UDP. I know its not 100% garanteed to get there but if I implement checks to make sure important ones make it then there is no need to implement TCP right? or do I do both.[/QUOTE] I would say it depends on what you are sending and your specific use case. Just saying that you are building a game is too general. If you need to gurantee that the packets are delivered, then you should use TCP. How would you implement checks for UDP?
[QUOTE=brianosaur;50854596]I would say it depends on what you are sending and your specific use case. Just saying that you are building a game is too general. If you need to gurantee that the packets are delivered, then you should use TCP. How would you implement checks for UDP?[/QUOTE] After each "important" message it listens for a reply, if no replay is meet after x ms or s then it knows the packet failed.
Okay so I did some research and im really stuck on what I should use. For example if I made a FPS game then it makes sense to use UDP for stuff like movment etc but what about important stuff like handling lobby connections, sending game events and stuff like that, stuff that 100% needs to be run. that would make UDP not so good, do I implement both or is that a silly idea. Thanks for any info regarding this.
Could always just add a syn-ack to important messages, but udp on one and tcp on another port would work too I guess [editline]11th August 2016[/editline] Source engine implements its own layer of reliability on top of udp iirc
[QUOTE=LennyPenny;50865811]Could always just add a syn-ack to important messages, but udp on one and tcp on another port would work too I guess [editline]11th August 2016[/editline] Source engine implements its own layer of reliability on top of udp iirc[/QUOTE] You can use the same port number for TCP and UDP.
[url]https://www.isoc.org/INET97/proceedings/F3/F3_1.HTM[/url] says it will lead to increased udp packet loss
Unless you're using sockets as a learning exercise, I'd recommend taking a look at [url=https://github.com/lidgren/lidgren-network-gen3]Lidgren[/url]. It implements a reliable layer over the top of UDP, allowing you to choose which packets are reliable/unreliable. Even if you don't use the library itself, the code is probably a good learning resource.
If you're going to have clients connect / disconnect from servers, you should use packets for that. Any important data that needs to be verified should be TCP- I.E. setting up the information for the UDP stream I.E. Connect[tcp] -> Client/server info exchange[tcp] -> start streaming data[udp] (and then tcp for disconnect)
As someone said above, try lidgren-network-gen3. It's UDP with TCP features. Other neat features include connect, disconnect, a list of connections, sending messages to all or all except some connections and many more. I'm using it myself in a game, works great and it's easy to use.
[QUOTE=LennyPenny;50866325][url]https://www.isoc.org/INET97/proceedings/F3/F3_1.HTM[/url] says it will lead to increased udp packet loss[/QUOTE] The newest version of Windows 10 apparently has a background transfer mode for TCP sockets that should prevent this, but that of course isn't really an option due to compatibility right now. I don't think it's quite documented yet either.
Sorry, you need to Log In to post a reply to this thread.