Unable to reconnect socket to existing IP / Port combination

Greetings, I am trying to find a way to decouple a socket from a specific IP / Port combination. My pseudo code is as follows:

ClassA a = new ClassA(); //(class A instantiates socket and binds it to 127.0.0.1:4567) //do something //...much later, a has been garbage-collected away. ClassA aa = new ClassA(); //crash here. 

At this point .Net tells me that I already have a socket bound to 127.0.0.1:4567, which is technically true. But no matter what code I insert into the ClassA destructor, or no matter what functions I call on the socket (I tried .Close () and .Disconnect (true)), the socket remains proudly tied to 127.0.0.1:4567 . What can I do to not bind a socket?


EDIT: I do not rely solely on garbage collection (although I also tried this approach). I tried calling a.Close () or a.Disconnect () and only then instantiated aa; this does not solve the problem.


EDIT: I also tried to implement IDisposable, but the code never got it without my method call (which was equivalent to earlier attempts, as the method would just try .Close and .Disconnect). Let me try calling. Ask directly and get back to you.


EDIT (many corrections, apologies): the implementation of IDisposable and the call to the .Dispose () function, from where "a" loses its scope, does not work. My Dispose implementation should still call either. Close, or .Disconnect (true) (or .Shutdown (Both)), but none of them untie the socket.

Any help would be appreciated!

+4
source share
5 answers

(that's what finally got everything to work for me)

Ensure that the EVERY connector to which the connector in A is connected has

 socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress, true); 

after launch.

+3
source

you cannot rely on an object being garbage collected in C # (I assume that you are using C # based on tags) if it contains resources, such as binding to a network resource, as in your example, or carrying out any Another type of stream, file stream will be a common example.

You must assure that the resources that are stored in the object are freed so that they can be collected in garbage. otherwise it will not be garbage collection, but will remain somewhere in memory. your pseudo-code example does not provide that you are freeing resources, you are simply indicating that the object is receiving (should receive) garbage collection.

0
source

The garbage collector does not guarantee that the socket will be closed. For a complete example, read this MSDN example .

The main thing is to actually call Socket.Close() as soon as possible. For example, ClassA can implement IDisposable and use it as follows:

 using (ClassA a = new ClassA()) { // code goes here } // 'a' is now disposed and the socket is closed 
0
source

The garbage collector starts the finalizer of the object at some indefinite time. You can implement the IDisposable interface and call the Dispose () method before the object loses scope - or let the using statement do it for you.

see http://msdn.microsoft.com/en-us/library/system.idisposable.aspx and http://msdn.microsoft.com/en-us/library/yh598w02.aspx

edit: works fine for me

  using System;
 using System.Net.Sockets;
 using System.Net;

 namespace ConsoleApplication1
 {
     class program
     {
         class ClassA: IDisposable
         {
             protected socket s;
             public ClassA ()
             {
                 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                 s.Bind (new IPEndPoint (IPAddress.Any, 5678));
             }

             public void Dispose ()
             {
                 s.Close ();
             }
         }

         static void Main (string [] args)
         {
             using (ClassA a = new ClassA ()) {
             }
             using (ClassA b = new ClassA ())
             {
             }
         }
     }
 }
0
source

The best solution is to try to bind the socket several times (2-3). On the first attempt, if this fails, I found that it will correctly (and for a long time) close the original socket.

NTN

_NT

0
source

Source: https://habr.com/ru/post/1276585/


All Articles