-1

I'm still fairly new to coding.

I made a simple client server following a tutorial using "NETWORKSAPI LIBRARY".

When a client is forcibly closed, due to crash / hang or even unplug internet. The server hangs and gives this error:

Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.

What can I do, so the server doesn't hang or crash because of a client hang?

The exception comes up on a line where I have a infinite loop sending messages to the client.

yappy
  • 25
  • 1
  • 2
  • 5
  • 2
    Catch the specific exception, clean up resources, and continue, – Michael Liu Mar 03 '17 at 18:12
  • 3
    Please don't add nonsense text to get around the duplicate title restriction. Instead, *read the question with the duplicate title* and its answers. If you find that they don't answer your question, come up with a title that doesn't just regurgitate the error message. – Heretic Monkey Mar 03 '17 at 18:22
  • Possible duplicate of [An existing connection was forcibly closed by the remote host](http://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host) – Ňɏssa Pøngjǣrdenlarp Mar 03 '17 at 19:14

1 Answers1

2

I think what you're looking for is a way to handle errors? If you're new to coding, then what you're after will be the try block, which'll allow you to try code, catch errors, clean up any resources, and move on. The usual simple structure is as follows:

try {
   ... actual code you want to run here ...
} catch (Exception ex) {
   ... handle the exception here (you can read around a lot on this) ...
} finally {
   ... clean up any resources
}

If you just want to roll over and ignore the error, then you don't need to do anything specific in the catch step, but try/catch/finally is a structure you really should become familiar with.

Rob Wilkins
  • 1,650
  • 2
  • 16
  • 20