123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- // Copyright (c) 2012-2020 fo-dicom contributors.
- // Licensed under the Microsoft Public License (MS-PL).
- using System;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading.Tasks;
- using Dicom.Network;
- using DicomClient = Dicom.Network.Client.DicomClient;
- namespace Dicom.CStoreSCU
- {
- internal static class Program
- {
- private static string _storeServerHost = "www.dicomserver.co.uk";
- private static int _storeServerPort = 11112;
- private static readonly string _storeServerAET = "STORESCP";
- private static readonly string _aet = "FODICOMSCU";
- static async Task Main(string[] args)
- {
- var storeMore = "";
- _storeServerHost = GetServerHost();
- _storeServerPort = GetServerPort();
- Console.WriteLine("***************************************************");
- Console.WriteLine("Server AE Title: " + _storeServerAET);
- Console.WriteLine("Server Host Address: " + _storeServerHost);
- Console.WriteLine("Server Port: " + _storeServerPort);
- Console.WriteLine("Client AE Title: " + _aet);
- Console.WriteLine("***************************************************");
- var client = new DicomClient(_storeServerHost, _storeServerPort, false, _aet, _storeServerAET);
- client.NegotiateAsyncOps();
- do
- {
- try
- {
- Console.WriteLine();
- Console.WriteLine("Enter the path for a DICOM file:");
- Console.Write(">>>");
- string dicomFile = Console.ReadLine();
- while (!File.Exists(dicomFile))
- {
- Console.WriteLine("Invalid file path, enter the path for a DICOM file or press Enter to Exit:");
- dicomFile = Console.ReadLine();
- if (string.IsNullOrWhiteSpace(dicomFile))
- {
- return;
- }
- }
- var request = new DicomCStoreRequest(dicomFile);
- request.OnResponseReceived += (req, response) => Console.WriteLine("C-Store Response Received, Status: " + response.Status);
- await client.AddRequestAsync(request);
- await client.SendAsync();
- }
- catch (Exception exception)
- {
- Console.WriteLine();
- Console.WriteLine("----------------------------------------------------");
- Console.WriteLine("Error storing file. Exception Details:");
- Console.WriteLine(exception.ToString());
- Console.WriteLine("----------------------------------------------------");
- Console.WriteLine();
- }
- Console.WriteLine("To store another file, enter \"y\"; Othersie, press enter to exit: ");
- Console.Write(">>>");
- storeMore = Console.ReadLine().Trim();
- } while (storeMore.Length > 0 && storeMore.ToLower()[0] == 'y');
- }
- private static string GetServerHost()
- {
- var hostAddress = "";
- var localIP = GetLocalIPAddress();
- do
- {
- Console.WriteLine("Your local IP is: " + localIP);
- Console.WriteLine("Enter \"1\" to use your local IP Address: " + localIP);
- Console.WriteLine("Enter \"2\" to use defult: " + _storeServerHost);
- Console.WriteLine("Enter \"3\" to enter custom");
- Console.Write(">>>");
- string input = Console.ReadLine().Trim().ToLower();
- if (input.Length > 0)
- {
- if (input[0] == '1')
- {
- hostAddress = localIP;
- }
- else if (input[0] == '2')
- {
- hostAddress = _storeServerHost;
- }
- else if (input[0] == '3')
- {
- Console.WriteLine("Enter Server Host Address:");
- Console.Write(">>>");
- hostAddress = Console.ReadLine();
- }
- }
- } while (hostAddress.Length == 0);
- return hostAddress;
- }
- private static int GetServerPort()
- {
- Console.WriteLine("Enter Server port, or \"Enter\" for default \"" + _storeServerPort + "\":");
- Console.Write(">>>");
- var input = Console.ReadLine().Trim();
- return string.IsNullOrEmpty(input) ? _storeServerPort : int.Parse(input);
- }
- public static string GetLocalIPAddress()
- {
- var host = Dns.GetHostEntry(Dns.GetHostName());
- foreach (var ip in host.AddressList)
- {
- if (ip.AddressFamily == AddressFamily.InterNetwork)
- {
- return ip.ToString();
- }
- }
- return "";
- }
- }
- }
|