rexcardan / Evil-DICOM

A C# DICOM Library
168 stars 98 forks source link

TcpClient not being released #65

Closed rpascal closed 5 years ago

rpascal commented 5 years ago

I am reading DICOM files from a folder and then using CStore to send the DICOM files.

var daemon = new Entity("aeTitle", "ip", 9999);
var local = Entity.CreateLocal("aeTitle", 8888);
var client = new DICOMSCU(local);
ushort msgId = 1;
var storer = client.GetCStorer(daemon);

foreach (var path in dcmFiles)
{
    var dcm = DICOMObject.Read(path);
    var response = storer.SendCStore(dcm, ref msgId);
    Console.WriteLine("Response is: " + (response != null).ToString());
}

The first store works properly but then on the second store it fails to connect to the TCPClient in DICOMSCU.SendMessage, the error thrown is Only one usage of each socket address (protocol/network address/port) is normally permitted ip:port

I believe that the first client is not being released once the send completes.

jazb83 commented 5 years ago

I am having the same issue: "System.Net.Sockets.SocketException: 'Only one usage of each socket address (protocol/network address/port) is normally permitted'"

I just tried to pull the studies from one patient and error appears.

// Store the details of the daemon (Ae Title , IP , port ) Entity daemon = new Entity("VMSDBD", "10.XXX.17.XXX", XXXXX); // Store the details of the client (Ae Title , port ) -> IP address is //determined by CreateLocal() method Entity local = Entity.CreateLocal("DCMXX", XXXXX);

  // Set up a client ( DICOM SCU = Service Class User )
  var client = new DICOMSCU(local);
  // Set up a receiver to catch the files as they come in
  var receiver = new DICOMSCP(local);

  // Let the daemon know we can take anything it sends
  receiver.SupportedAbstractSyntaxes = AbstractSyntax.ALL_RADIOTHERAPY_STORAGE;

  // Set up storage location
  var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  var storagePath = System.IO.Path.Combine(desktopPath, "DICOM_Storage");
  Directory.CreateDirectory(storagePath);

  // Set the action when a DICOM files comes in
  receiver.DIMSEService.CStoreService.CStorePayloadAction = (dcm, asc) =>
  {
    var path = System.IO.Path.Combine(storagePath, dcm.GetSelector().SOPInstanceUID.Data + ".dcm");
    Console.WriteLine($" Writing_file_{ path }... ");
    dcm.Write(path);
    return true; // Lets daemom know if you successfully wrote to drive
  };

  receiver.ListenForIncomingAssociations(true);

  //// Build a finder class to help with C- FIND operations
  var finder = client.GetCFinder(daemon); \\error here `

The script works pretty well with version 2.0.2.9, but it gets the same error with version 2.0.3.1.

rexcardan commented 5 years ago

I rolled back this change. Please try 2.0.3.2