# Stateless Frustrations Frustratingly Stated (Handling Improperly Disconnected UDP Clients?)

725 views

So, after weeks of strange and seemingly intermittent/coincidental UDP server crashes(Receive Thread Halt/Un-catchable Exception) and the ensuing frustration that that causes when you aren't even trying to work on the server itself.  I sat down and spent a day trying to figure out the problem..  After an almost complete server rewrite from scratch with no real improvement in the problem.. I turned to random poking about on the internets..

Hmmm...  It seems like the problem only happens when I'm restarting the client..

Here's a post!!

And Here!

Yup, that's the problem for sure.

It appears that even though UDP isn't supposed to care about the other ends state, apparently this isn't true if the state is Port Unreachable.  When the server attempts to receive data (BeginReceiveFrom/BeginReceive/EndRecieveFrom/EndReceive/etc..) from an async connection and the client has (Crashed/Quit/Etc.) an ICMP port unavailable packet is generated and this causes the UDP socket to generate an exception that (even if you try/catch the hell out of it) causes the async receive thread to halt(I assume, this is the effect anyhow).

The best solution I've found, as stated in the link(s) above is to change the underlying IOControl behavior(Prevent the Exception from being thrown).

//This value tells the underlying IO system to IGNORE the ICMP error.
public const int SIO_UDP_CONNRESET = -1744830452;
private Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

_socket.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { 0, 0, 0, 0 }, null);
_socket.Bind(new IPEndPoint(IPAddress.Any, port));

The first line is the value that needs to be set, and the second to last line is how you can set it.  And these are the live bits of code that are WORKING without error in my server, Finally!

So, there, I've posted it again because it seems way more obscure than it should be.

Enjoy.

### Images

View the entire Unsettled World album

I guess it's worth adding that my server handles client timeouts as part of it's core behavior, simple timeouts for inactivity/etc, and doesn't use any of the UDP connections "state" info for the decision process..  So the only potential negative side-effect of not properly trapping this exception and removing the user's EndPoint immediately is that a few extra packet sends get attempted to the dead client before the player's EndPoint gets forgotten anyhow.  So far, it's not an issue, hopefully it stays that way.

##### Link to comment

Additionally I may be experimenting with rewriting my server so that it doesn't use the VERY same thread for every single receive operation, doing this probably would have prevented my server from having issues in the first place as only the thread serving the dead client would be halting.  And the extra exception was already going unnoticed anyways. lol...

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Similar Content

• By Brizzler
Hey Everyone,
I'd like to review some of your indies games! I'm interested in games made for the Universal Windows Platform (UWP).
Reply to this post if your interested in having your games reviewed and youtubed on my channel:
UWPdeveloper

Cheers,

B
• By JustHusk
Hi there,
So i'm an inexperienced student learning games dev at college and im studying c#, in my spare time im trying to work on a text adventure game with a narrative. My issue is that i dont know how to make the decisions, well not simply anyhow.
I could write out 50 variables for each decision to read from the player but that would become very tedious and too messy to even comprehend when im adding content. I was thinking of adding functions to hold each decision or area but im at a lack of knowledge on how to jump to a function or if it's even a good solution. I know i could use an array and each decision be a number but that just doesn't seem like a good solution to me either.
Any advice would be appreciated and this whole topic might sound dumb to a professional so be understanding please haha.
Thanks,
Luke
• By Septopus
Okay, looking for some constructive feedback/criticism here...
What follows is the code of my c# UDP Socket Class.  I've written this based on a number of online examples and a few distant memories from the past, so I really have no idea how "bad" the code is/could be in a 100+ concurrent connection scenario(my dev environment is just two machines at the moment)...  It works, I know that, it is fairly stable(I can't break it when using it normally), and it behaves how I believe I want it to.
It is a dual purpose class for handling both Servers and Clients.  It uses an asynchronous receive and synchronous send(I may switch to async send if it proves beneficial later on).  My servers use Client sockets to communicate with each other and to perform internal connection tests in a multiple server, "single shard" type environment.  I'll be devising some heavy load testing methods a little further down the line, but I'm hoping to fish out most of the major gotchas before I get there.
So, please, let me know how to fix it up if it smells terribly to you, or if not, that's even better...
Sorry for the large code dump, but THANKS for checking it out!
public static bool running = true; static void UDPServer() { using (s = new UDPSocket(MessageDictionary, UWDataStore)) { s.Server("all", 37373); while(running) { //Servery Stuff Goes Here. //Like reiteratively dequeuing the Message Dictionary Queues and processing/replying to all commands/etc... } } } All processing of individual messages from clients is handled with Task.Factory tasks, and a reference to the server's socket varible (s), and the client's EndPoint is sent along the ride for use in replying to clients.  There's no game logic and there are no client replies that happen directly from within the UDP server's main while loop, mostly just connection status checking and reorganizing of the Message Queues into Tasks.
Thanks again for making it this far.
• By MatsK
I'm creating a textbox for my UI, and I found a way to place the cursor where the user clicks on the text.
Here is all of my code:
/// <summary> /// Adds a newline to m_Text, and updates the cursor. /// </summary> private void AddNewline() { m_Text.Add("\n"); m_Cursor.CharacterIndex++; m_Cursor.Position.X = m_TextEditPosition.X; m_Cursor.LineIndex++; //Scroll the text up if it's gone beyond the borders of the control. if ((m_TextEditPosition.Y - TextSize().Y) < (m_TextEditPosition.Y - m_TextEditSize.Y)) { m_TextPosition.Y -= CapitalCharacterHeight; m_CanMoveCursorDown = false; m_UpdateCharPositions = true; } if (m_CanMoveCursorDown) m_Cursor.Position.Y += CapitalCharacterHeight; m_NumLinesInText++; } I'm guessing that I need to take m_TextPosition.Y into consideration here:
foreach (Rectangle Hitbox in m_HitBoxes) { if (Hitbox.Contains(new Vector2(m_Input.MousePosition.X, m_Input.MousePosition.Y)) && m_Input.IsNewPress(MouseButtons.LeftButton)) { m_Cursor.Position = new Vector2(Hitbox.X, Hitbox.Y); int CharIndex = m_CharacterPositions.FirstOrDefault(x => x.Value == m_Cursor.Position).Key; if (CharIndex != -1) m_Cursor.CharacterIndex = CharIndex; } } But I'm not entirely sure what to do. I tried both multiplying and adding m_TextPosition.Y to m_Input.MousePosition.Y and Hitbox.Y, but none of them seemed to work.
• By zuhane
Hello forum dwellers!
I've recently decided to dive into using ECS, as it looks like it solves so many issues with game development, especially with the game I am currently making (which involved many complex entities seamlessly interacting at the same time). It all seemed relatively straightforward. So far, I understand that:
1.) The entity is simply a container for components (which can also have an identifier)
2.) The components are interchangeable blocks of data which change the way the entity will behave (only containing data - no methods/functions/logic)
3.) The systems loop through all the entities and checks to see which components they contain. If it contains the appropriate components, act on them.
So far, this has been easy to implement and worked for the basic design. For example, I have a player, block and a ball. The rendering component is present in all 3, colliders in all 3, physics in the player and ball, and input only in the player. I recently looked up how components interact with one another, and that also seems simple. For example, if I want a player to animate due to a button press, I would create a system which requires a rendering component AND input component. The problem I seem to be having trouble understanding is exactly how to nest more complex entities and components.
For example, I want my player to be able to punch the ball around. If the player presses the "E" key, I want to create a fist which punches to the left (drawn over the player without the player animation changing). However, the fist itself punching the ball technically has a sprite that needs rendering and also a collision box, so should I create a "fist" component which contains 2 components (rendering and collision), or should I generate a new entity which contains those 2 components and let the systems act naturally on it? I feel like by nesting components within components, I end up with this spaghetti code where systems are calling other systems, removing the whole point of an ECS. It feels more "natural" to just leave the systems to wait and act on entities which contain the correct components. However, if I create a new "fist" entity, this needs to be tied to the player, so I ended up with an entity containing an entity which also seems ugly, or a weird entity with some kind of reference of linking ID to its creator. Am I missing something?

On another note, is it unsafe to add multiple instances of the same component? I've heard that it's bad practice too. For example, I might have entities which all require rendering, but some might want to use animated sprites, multiple layers of sprites, shaders, etc. Is it better to have multiple rendering components which can optionally interact, or possibly use a rendering component interface which multiple rendering components can inherit from? Is this inheritance not defeating the purpose of using an ECS in the first place?

Any help would be greatly appreciated!
Thanks, Zuhane
×