←Select platform

Listen(string,int,int,DicomNetIpTypeFlags) Method

Summary
Establishes a connection for listening for incoming connection requests.
Syntax
C#
C++/CLI
public void Listen( 
   string hostAddress, 
   int hostPort, 
   int maxNumberOfPeers, 
   DicomNetIpTypeFlags ipType 
) 
public: 
void Listen(  
   String^ hostAddress, 
   int hostPort, 
   int maxNumberOfPeers, 
   DicomNetIpTypeFlags ipType 
)  

Parameters

hostAddress
The IP address of the host computer (the SCP's address).

hostPort
The port number of the host computer (the SCP's port).

maxNumberOfPeers
Backlog parameter.

ipType
The type of ip address supported (IPv4, IPv6, or both)

Remarks

If hostAddress is an empty string or a null reference (Nothing in VB), the IP address will be the local computer's address.

If hostAddress is "*", the IP address will be all of the local computer's addresses. This is useful if the local computer has more than one network interface and address.

If hostPort is 0, the port number will be the number of the first available port.

The maxNumberOfPeers parameter is the value passed to the WinSock listen() function for the backlog parameter that limits the size of the queue for waiting connections. As an example, suppose that the value is set to 3 and that 4 people try to connect at exactly the same time. In such a case, all 4 will be rejected because the connection backlog queue is full. But if one of the connections has been accepted by the time the 4th is made, then all will work.

To determine how many clients are connected and impose a limit on the number of connections, perform the following steps:

  1. Use the DicomNet.GetClientCount function in the "OnReceiveAssociateRequest" handler.
  2. Compare that value with the maximum number of connections allowed.
  3. If the number of clients connected is greater than the maximum number of connections allowed, send a "SendAssociateReject" command to the client trying to connect.

To connect to a server as a client, you must first create and initialize a DicomNet object. Then call Connect to establish the connection.

To use your computer as an SCP, you must first create a DicomNet object. Then call Listen to listen for incoming connection requests.

This overload of the DicomNet.Connect method allows you to specify which type of Internet Protocol Version to use. Pass DicomNetIpTypeFlags.Ipv4 for [ipType](" id="iptypeparameterlink" class="popuplink.html) to support the Internet Protocol version 4 (IPv4), which is the standard "dotted quad" 32-bit address format that has been in use since 1981. An example of an IPv4 address is 192.168.0.195

Pass DicomNetIpTypeFlags.Ipv6 for [ipType](" id="iptypeparameterlink" class="popuplink.html) to support Internet Protocol Version 6 (IPv6). IPv6 uses a 128-bit address format. An example of an IPv6 address is fe80::18bd:81f:6b02:759f

To support both IPv4 and Ipv6 addresses, pass DicomNetIpTypeFlags.Ipv4OrIpv6 for [ipType](" id="iptypeparameterlink" class="popuplink.html).

If the call to Connect fails, make sure that the IP address passed for hostAddress is a valid address accessible within your network. You can verify the accessibility of both IPv4 and IPv6 addresses using the Windows ping command. For example, to verify that 192.168.0.195 is accessible within your network, perform the following steps:

  • Open a command prompt, and type the following command
  • ping 192.168.0.195

Note that the following are equivalent:

  • Listen( hostAddress, hostPort, maxNumberOfPeers);
  • Listen( hostAddress, hostPort, maxNumberOfPeers, DicomNetIpTypeFlags.Ipv4);
Example
C#
using Leadtools; 
using Leadtools.Dicom; 
 
 
/// 
[StructLayout(LayoutKind.Sequential)] 
public struct MYMSG 
{ 
   public IntPtr hwnd; 
   public uint message; 
   public IntPtr wParam; 
   public IntPtr lParam; 
   public uint time; 
   public System.Drawing.Point p; 
} 
public enum MyWaitReturn 
{ 
   Complete, 
   Timeout, 
} 
 
class MyUtils 
{ 
   [DllImport("user32.dll")] 
   [return: MarshalAs(UnmanagedType.Bool)] 
   static extern bool PeekMessage(out MYMSG lpMsg, IntPtr hWnd, 
                                  uint wMsgFilterMin, uint wMsgFilterMax, 
                                  uint wRemoveMsg); 
 
   [DllImport("user32.dll")] 
   static extern bool TranslateMessage([In] ref MYMSG lpMsg); 
   [DllImport("user32.dll")] 
   static extern IntPtr DispatchMessage([In] ref MYMSG lpmsg); 
 
   const uint PM_REMOVE = 1; 
 
   public static MyWaitReturn WaitForComplete(double mill, WaitHandle wh) 
   { 
      TimeSpan goal = new TimeSpan(DateTime.Now.AddMilliseconds(mill).Ticks); 
 
      do 
      { 
         MYMSG msg = new MYMSG(); 
 
         if (PeekMessage(out msg, IntPtr.Zero, 0, 0, PM_REMOVE)) 
         { 
            TranslateMessage(ref msg); 
            DispatchMessage(ref msg); 
         } 
 
         if (wh.WaitOne(new TimeSpan(0, 0, 0), false)) 
         { 
            return MyWaitReturn.Complete; 
         } 
 
         if (goal.CompareTo(new TimeSpan(DateTime.Now.Ticks)) < 0) 
         { 
            return MyWaitReturn.Timeout; 
         } 
 
      } while (true); 
   } 
} 
 
class MyClient : DicomNet 
{ 
   DicomExceptionCode _LastError = DicomExceptionCode.Success; 
   AutoResetEvent waitEvent = new AutoResetEvent(false); 
 
   public MyClient() 
      : base(null, DicomNetSecurityMode.None) 
   { 
   } 
 
   public DicomExceptionCode LastError 
   { 
      get 
      { 
         return _LastError; 
      } 
   } 
 
   public bool Wait() 
   { 
      _LastError = DicomExceptionCode.Success; 
      MyWaitReturn ret = MyUtils.WaitForComplete((5 * 60) * 1000, waitEvent); 
 
      return (ret == MyWaitReturn.Complete); 
   } 
 
   protected override void OnConnect(DicomExceptionCode error) 
   { 
      Debug.WriteLine("Client: Receive OnConnect"); 
      _LastError = error; 
      waitEvent.Set(); 
   } 
 
   protected override void OnReceiveReleaseResponse() 
   { 
      Debug.WriteLine("Client: Receive OnReceiveReleaseResponse"); 
      waitEvent.Set(); 
   } 
} 
 
class MyServerConnection : DicomNet 
{ 
   public bool Fail = false; 
 
   public MyServerConnection() 
      : base(null, DicomNetSecurityMode.None) 
   { 
   } 
 
   protected override void OnReceiveReleaseRequest() 
   { 
      Debug.WriteLine("Server: Receive OnReceiveReleaseRequest"); 
      SendReleaseResponse(); 
   } 
}; 
 
class MyServer : DicomNet 
{ 
   MyServerConnection client; 
 
   public MyServer() 
      : base(null, DicomNetSecurityMode.None) 
   { 
   } 
 
   public bool Fail 
   { 
      get 
      { 
         if (client == null) 
            return false; 
 
         return client.Fail; 
      } 
 
      set 
      { 
         if (client != null) 
         { 
            client.Fail = value; 
         } 
      } 
   } 
 
   protected override void OnAccept(DicomExceptionCode error) 
   { 
      Debug.WriteLine("Server: Receive OnAccept"); 
      client = new MyServerConnection(); 
 
      Accept(client); 
      string s = string.Format("\tPeerAddress: {0}\n\tPeerPort: {1}", client.PeerAddress, client.PeerPort); 
      Debug.WriteLine(s); 
   } 
} 
 
public void DicomDataSet_ListenExample() 
{ 
   DicomEngine.Startup(); 
   DicomNet.Startup(); 
 
   using (MyServer server = new MyServer()) 
   { 
      using (MyClient client = new MyClient()) 
      { 
         // The * below means to listen on all local IP addresses. 
         // The DicomNetIpTypeFlags.Ipv4OrIpv6 means to listen on both IPv4 and IPv6 addresses 
         server.Listen("*", 104, 1, DicomNetIpTypeFlags.Ipv4OrIpv6); // start server  
 
         // The client is connecting to the server using an IPv6 address. 
         // Note that "::1" is the IPv6 loopback address 
         // Alternatively, you can get type ipconfig from a command box on the local computer to get the local IPv6 address. 
         // The following is an example of a local IPv6 address: 
         //    "fe80::18bd:81f:6b02:759f" 
         client.Connect(null, 1000, "::1", 104, DicomNetIpTypeFlags.Ipv6); // connect to server  
 
         if (!client.Wait()) // wait for connection to finish  
         { 
            Assert.Fail("Connection timed out"); 
         } 
 
         if (client.LastError == DicomExceptionCode.NetAddressInUse) 
         { 
            client.CloseForced(true); 
            server.CloseForced(true); 
            DicomEngine.Shutdown(); 
            DicomNet.Shutdown(); 
         } 
 
         Assert.IsTrue(client.LastError == DicomExceptionCode.Success, $"Connection failed: {client.LastError} "); 
         Assert.IsTrue(client.IsConnected() == true, $"Client {client} not connected"); 
 
         client.SendReleaseRequest(); 
         if (!client.Wait()) // wait for connection to finish  
         { 
            Assert.Fail("Timed Out"); 
         } 
 
         // Close the connections 
         client.CloseForced(true); 
      } 
      server.CloseForced(true); 
   } 
 
   DicomEngine.Shutdown(); 
   DicomNet.Shutdown(); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Dicom Assembly