Program.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (c) 2012-2020 fo-dicom contributors.
  2. // Licensed under the Microsoft Public License (MS-PL).
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading.Tasks;
  8. using Dicom.Network;
  9. using DicomClient = Dicom.Network.Client.DicomClient;
  10. namespace Dicom.CStoreSCU
  11. {
  12. internal static class Program
  13. {
  14. private static string _storeServerHost = "www.dicomserver.co.uk";
  15. private static int _storeServerPort = 11112;
  16. private static readonly string _storeServerAET = "STORESCP";
  17. private static readonly string _aet = "FODICOMSCU";
  18. static async Task Main(string[] args)
  19. {
  20. var storeMore = "";
  21. _storeServerHost = GetServerHost();
  22. _storeServerPort = GetServerPort();
  23. Console.WriteLine("***************************************************");
  24. Console.WriteLine("Server AE Title: " + _storeServerAET);
  25. Console.WriteLine("Server Host Address: " + _storeServerHost);
  26. Console.WriteLine("Server Port: " + _storeServerPort);
  27. Console.WriteLine("Client AE Title: " + _aet);
  28. Console.WriteLine("***************************************************");
  29. var client = new DicomClient(_storeServerHost, _storeServerPort, false, _aet, _storeServerAET);
  30. client.NegotiateAsyncOps();
  31. do
  32. {
  33. try
  34. {
  35. Console.WriteLine();
  36. Console.WriteLine("Enter the path for a DICOM file:");
  37. Console.Write(">>>");
  38. string dicomFile = Console.ReadLine();
  39. while (!File.Exists(dicomFile))
  40. {
  41. Console.WriteLine("Invalid file path, enter the path for a DICOM file or press Enter to Exit:");
  42. dicomFile = Console.ReadLine();
  43. if (string.IsNullOrWhiteSpace(dicomFile))
  44. {
  45. return;
  46. }
  47. }
  48. var request = new DicomCStoreRequest(dicomFile);
  49. request.OnResponseReceived += (req, response) => Console.WriteLine("C-Store Response Received, Status: " + response.Status);
  50. await client.AddRequestAsync(request);
  51. await client.SendAsync();
  52. }
  53. catch (Exception exception)
  54. {
  55. Console.WriteLine();
  56. Console.WriteLine("----------------------------------------------------");
  57. Console.WriteLine("Error storing file. Exception Details:");
  58. Console.WriteLine(exception.ToString());
  59. Console.WriteLine("----------------------------------------------------");
  60. Console.WriteLine();
  61. }
  62. Console.WriteLine("To store another file, enter \"y\"; Othersie, press enter to exit: ");
  63. Console.Write(">>>");
  64. storeMore = Console.ReadLine().Trim();
  65. } while (storeMore.Length > 0 && storeMore.ToLower()[0] == 'y');
  66. }
  67. private static string GetServerHost()
  68. {
  69. var hostAddress = "";
  70. var localIP = GetLocalIPAddress();
  71. do
  72. {
  73. Console.WriteLine("Your local IP is: " + localIP);
  74. Console.WriteLine("Enter \"1\" to use your local IP Address: " + localIP);
  75. Console.WriteLine("Enter \"2\" to use defult: " + _storeServerHost);
  76. Console.WriteLine("Enter \"3\" to enter custom");
  77. Console.Write(">>>");
  78. string input = Console.ReadLine().Trim().ToLower();
  79. if (input.Length > 0)
  80. {
  81. if (input[0] == '1')
  82. {
  83. hostAddress = localIP;
  84. }
  85. else if (input[0] == '2')
  86. {
  87. hostAddress = _storeServerHost;
  88. }
  89. else if (input[0] == '3')
  90. {
  91. Console.WriteLine("Enter Server Host Address:");
  92. Console.Write(">>>");
  93. hostAddress = Console.ReadLine();
  94. }
  95. }
  96. } while (hostAddress.Length == 0);
  97. return hostAddress;
  98. }
  99. private static int GetServerPort()
  100. {
  101. Console.WriteLine("Enter Server port, or \"Enter\" for default \"" + _storeServerPort + "\":");
  102. Console.Write(">>>");
  103. var input = Console.ReadLine().Trim();
  104. return string.IsNullOrEmpty(input) ? _storeServerPort : int.Parse(input);
  105. }
  106. public static string GetLocalIPAddress()
  107. {
  108. var host = Dns.GetHostEntry(Dns.GetHostName());
  109. foreach (var ip in host.AddressList)
  110. {
  111. if (ip.AddressFamily == AddressFamily.InterNetwork)
  112. {
  113. return ip.ToString();
  114. }
  115. }
  116. return "";
  117. }
  118. }
  119. }