How to choose which IP Address you connect from

When a PC has multiple IP Addresses, sometimes people would like to specify one of them to be used for connecting out to other DICOM AEs. This can be easily achieved in the .NET version of DicomObjects. The following VB.NET 2005 sample code shows how this could be done:

Dim cn As New DicomObjects.DicomAssociation
Dim socket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

socket.Bind(New IPEndPoint(IPAddress.Parse("192.168.100.1"), 0))
socket.Connect(RemoteIPAddress, RemotePortNumber)

Dim netStream As New NetworkStream(socket)
cn.Open(socket, netStream, "scuAET", "scpAET")

In the code above, IP Address "192.168.100.1" is one of the multiple IP Addresses the machine has. RemoteIPAddress and RemotePortNumber are the IP Address and port number it is going to connect to.

Note: The above code requires System.Net and System.Net.Sockets name spaces.

Relevance: