Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/22/16 12:06:01 (8 years ago)
Author:
thasling
Message:

#2615:
changed behaviour when determening a new free tcp port

Location:
branches/thasling/DistributedGA
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageService.cs

    r14252 r14263  
    11using System;
    22using System.Net;
     3using System.Net.NetworkInformation;
    34using System.Net.Sockets;
    45using System.ServiceModel;
     
    2425      var serviceUrl = "DistributedGA.svc";
    2526      new Thread(() => {
    26         var baseUri = new Uri(string.Concat("net.tcp://", ip, ":", port, "/DistributedGA"));
    27         var serviceUri = new Uri(baseUri, serviceUrl);
    28         NetTcpBinding binding = new NetTcpBinding();
    29         binding.MaxReceivedMessageSize = 20000000;
    30         binding.MaxBufferSize = 20000000;
    31         binding.MaxBufferPoolSize = 20000000;
    32         binding.ReaderQuotas.MaxArrayLength = 20000000;
    33         binding.ReaderQuotas.MaxDepth = 32;
    34         binding.ReaderQuotas.MaxStringContentLength = 20000000;
     27        try {
     28          var baseUri = new Uri(string.Concat("net.tcp://", ip, ":", port, "/DistributedGA"));
     29          var serviceUri = new Uri(baseUri, serviceUrl);
     30          NetTcpBinding binding = new NetTcpBinding();
     31          binding.MaxReceivedMessageSize = 20000000;
     32          binding.MaxBufferSize = 20000000;
     33          binding.MaxBufferPoolSize = 20000000;
     34          binding.ReaderQuotas.MaxArrayLength = 20000000;
     35          binding.ReaderQuotas.MaxDepth = 32;
     36          binding.ReaderQuotas.MaxStringContentLength = 20000000;
    3537
    36         using (var host = new ServiceHost(messageContract, serviceUri)) {
    37           host.AddServiceEndpoint(typeof(IMessageContract), binding, serviceUri);
     38          using (var host = new ServiceHost(messageContract, serviceUri)) {
     39            host.AddServiceEndpoint(typeof(IMessageContract), binding, serviceUri);
    3840
    39           host.Open();
     41            host.Open();
    4042
    41           _ResetEvent.WaitOne();
     43            _ResetEvent.WaitOne();
     44          }
     45        }
     46        catch (Exception ex) {
     47          int k = 0;
     48          int j = 0;
    4249        }
    4350      }).Start();
    4451      return port;
     52
    4553    }
    4654
     
    5058    }
    5159
     60    private bool IsPortAvaiable(int port) {
     61      //int port = 456; //<--- This is your value
     62      bool isAvailable = true;
     63
     64      // Evaluate current system tcp connections. This is the same information provided
     65      // by the netstat command line application, just in .Net strongly-typed object
     66      // form.  We will look through the list, and if our port we would like to use
     67      // in our TcpClient is occupied, we will set isAvailable to false.
     68      IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
     69      TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
     70      foreach (TcpConnectionInformation tcpi in tcpConnInfoArray) {
     71        if (tcpi.LocalEndPoint.Port == port) {
     72          isAvailable = false;
     73          break;
     74        }
     75      }
     76      foreach (var item in ipGlobalProperties.GetActiveTcpListeners()) {
     77        if (item.Port == port) {
     78          isAvailable = false;
     79          break;
     80        }
     81      }
     82      return isAvailable;
     83    }
     84
    5285    private int FreeTcpPort() {
    53       TcpListener l = new TcpListener(IPAddress.Loopback, 0);
    54       l.Start();
    55       int port = ((IPEndPoint)l.LocalEndpoint).Port;
    56       l.Stop();
    57       return port;
     86      for (int i = 11000; i <= 11100; i++) {
     87        if (IsPortAvaiable(i))
     88          return i;
     89      }
     90      return 0;
    5891    }
     92
     93    //private int FreeTcpPort() {
     94    //  TcpListener l = new TcpListener(IPAddress.Loopback, 0);
     95    //  l.Start();
     96    //  int port = ((IPEndPoint)l.LocalEndpoint).Port;
     97    //  l.Stop();
     98    //  return port;
     99    //}
    59100
    60101    private void OnMessageRecieved(object sender, MessageRecieveEventArgs e) {
  • branches/thasling/DistributedGA/DistributedGA.sln

    r14261 r14263  
    5050    HideSolutionNode = FALSE
    5151  EndGlobalSection
    52   GlobalSection(Performance) = preSolution
    53     HasPerformanceSessions = true
    54   EndGlobalSection
    5552EndGlobal
Note: See TracChangeset for help on using the changeset viewer.