0

I'm running into a problem calling GetSocketOption in C# using .net core 5.0 on Linux.

Calling:

_socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger);

This throws an exception "Operation not supported". Ok, so that option isn't support in the env I'm using it in.

The problem is that the exception closes (disconnects) the socket. The _socket.Connected property is true before this statement and false after it. Is there a way to check this in C# without risking the generation of a socket disconnect? Is there a "safe" way to check socket options?

While writing OS specific code for this is perhaps pragmatic I'm looking for a way to handle this in a non-OS specific way, that is after all one of the draws of .net core.

Update: FWIW, I posted this as an issue and it can be tracked here: GetSocketOption/SetSocketOption should not close underlying socket

Dweeberly
  • 4,668
  • 2
  • 22
  • 41
  • Take a look at this SO post https://stackoverflow.com/questions/5116977/how-to-check-the-os-version-at-runtime-e-g-on-windows-or-linux-without-using You should be able to check the OS family at runtime and conditionally avoid specific APIs – emagers Sep 08 '21 at 22:50
  • Does this answer your question? [How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement](https://stackoverflow.com/questions/5116977/how-to-check-the-os-version-at-runtime-e-g-on-windows-or-linux-without-using) – emagers Sep 08 '21 at 22:51
  • https://github.com/dotnet/runtime/issues/25554 – Lex Li Sep 08 '21 at 22:58
  • @EricMagers I would prefer to not have to write OS dependent code as the purpose of the code is to run on both Linux and WIndows. – Dweeberly Sep 08 '21 at 23:04
  • @LexLi That is unfortunate, but seems to be the answer, perhaps I will enter a ticket since I would not expect GetSocketOption to disconnect a functioning socket. Thank you for pointing this out. I suppose I'll have to switch to checking SocketOptionName.Linger or the LingerState property. – Dweeberly Sep 08 '21 at 23:07

0 Answers0