Ekaterina
Ekaterina

Reputation: 534

SocketError: An existing connection was forcibly closed by the remote host

What should I do? Close the socket? I can't see the problem, google doesn't help What can cause this error? as I understand this is System type exception. Exception

 Console.WriteLine("Connection Recieved");
        while (true)
        {
            var handler = socket.Accept();
            try
            {
                while (handler.Connected)
                {
                    var x = new byte[200000];
                    try
                    {
                        handler.Receive(x);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.InnerException);
                        Console.WriteLine(ex.StackTrace);
                    }
                    var command = new string(Encoding.UTF8.GetChars(x.Where(t => t != 0).ToArray()));
                    if (string.IsNullOrEmpty(command))
                    {
                        break;
                    }
                    var data = (Dictionary<string, object>)JsonConvert.DeserializeObject(command, (typeof(Dictionary<string, object>)));
                    if (data["command"].ToString() != "getLog") continue;
                    var response = GetStatus();
                    handler.Send(Encoding.UTF8.GetBytes(response));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.InnerException);
                Console.WriteLine(ex.StackTrace);
            }
        }

Upvotes: 1

Views: 779

Answers (1)

Anonymous Coward
Anonymous Coward

Reputation: 36

Well "well abstracted networking with multiple layers of protocols" while dealing with various disconnections (soft-transient:eg.timeout, recoverable:eg.prepaid data plan limit exceeded , transient hardware:slim flimsy lenovos rj45 jacks get loose, unrecoverable hardware)...

is issue that is hard because the libraries and devs and end users each have different needs in relation to the error conditions that depend on the type of lib or app or usage involved.

John Carmack just tweeted something about "abstractions 5uck" but in networking it's not reasonable that everyone reimplements TLS or whatever. What can be looked at what type of interfaces are practical (some C guys have argued against streams probably because of these error flow issues) .. and how errors and warnings flow during runtime and could library writers add hints for the compiler that the "compiler flows" during the development experience such that devs get warned about which type of errors may propagate from lower level and through the abstractions if there are. Multiple return values offer one way for error,status,warning propagation but supporters of exceptions have argued that exceptions are faster when not thrown but this may boil to language and compiler implementation - eg. jit could turn to use exceptions in hot loop and transform the exception into propagaetd error etc.

When starting out with C# the first thing I ran into was how to handle all possible kinds of disconnections and found that .. yes BCL/C# looks nice but devil is in the details and surprising runtime exceptions that are different and pop up from different places depending on timeout/disconnection/cable-unplug/hw-fault etc are probably something that languages and libraries could use as a "use case" around which to architect the language and standard libraries (and error,status etc flow paradigms) such that their users were better aware of these instead of having to guess which error or exception and from where it might prop up. Now trying to add error handling in advance is hard and some of these conditions are more and less exceptional (glitchy rj45 port in a laptop - works most of the time) - or what about something like Surface Books detachable display where you transition from one GPU to another etc.

This stuff is hard and abstractions often make it worse yet we also need some abstractions (like network protocols etc).

Infact this stuff is so hard, if you look for it, there isn't a whole lot written about it in terms of "how to make things better for people who aren't John Carmack level programmers". I saw one presentation suggesting functional programming could have some answers but it didn't go deep into all those things that could happend in networking so I wasn't left 100% convinced it was the answer but it's a start.

https://vimeo.com/113707214

That was the presentation I saw but yeah, can that model work for everything (and possible more) that I outlined in terms of needs?

Upvotes: 1

Related Questions