Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 17th, 2012  |  syntax: None  |  size: 3.02 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Is using TPL to create a collection of Sockets overkill or is there a better way to create multiple sockets?
  2. static void Main(string[] args)
  3.         {
  4.             Console.WriteLine("Creating connections.....");
  5.             var sockets = CreateListeners(5);
  6.  
  7.             Console.WriteLine("Socket Info:");
  8.  
  9.             foreach (var socket in sockets)
  10.             {                
  11.                 if (socket.Result != null)
  12.                 {
  13.                     var con = socket.Result;
  14.                     IPEndPoint ipep = (IPEndPoint)con.LocalEndPoint;
  15.                     string port = ipep.Port.ToString();
  16.                     Console.WriteLine("Socket #{0} - Listening on Port {1}:", socket.Id.ToString(), port);
  17.                 }
  18.             }
  19.             Console.WriteLine("Press any key to exit..");
  20.             Console.ReadLine();
  21.         }
  22.        
  23. private static List<Task<Socket>> CreateListeners(int numberToCreate)
  24.             {
  25.                 var sockets = new List<Task<Socket>>();
  26.                 for (int n = 0; n < numberToCreate; n++)
  27.                 {
  28.                     var currentSocket = Task<Socket>.Factory.StartNew(() => CreateSocketConnection(n));
  29.                     sockets.Add(currentSocket);
  30.                 }
  31.  
  32.                 return sockets;            
  33.             }
  34.        
  35. private static Socket CreateSocketConnection(int port)
  36.             {
  37.  
  38.                 try
  39.                 {
  40.                     IPAddress ipAddress = null;
  41.                     IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
  42.                     if (Socket.OSSupportsIPv6)
  43.                     {
  44.                         ipAddress = ipHostInfo.AddressList[1];
  45.                     }
  46.                     else
  47.                     {
  48.                         ipAddress = ipHostInfo.AddressList[0];
  49.                     }
  50.  
  51.  
  52.                     //Create a new connection on specified port
  53.                     IPEndPoint endPoint = new IPEndPoint(ipAddress, port+6000);
  54.                     Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  55.                     listener.Bind(endPoint);
  56.                     //listener.Blocking = false;
  57.                     listener.Listen(500);
  58.  
  59.                     return listener;
  60.  
  61.                 }
  62.                 catch (Exception ex)
  63.                 {
  64.                    throw ex;        
  65.                 }
  66.                 finally
  67.                 {
  68.  
  69.                 }
  70.  
  71.                 return null;            
  72.             }
  73.        
  74. Task<Socket>.Factory.StartNew(start, CancellationToken.None,
  75.     TaskCreationOptions.LongRunning, // The interpretation depends on scheduler
  76.     TaskScheduler.Default // The default one place them in their own thread
  77.     );
  78.        
  79. throw ex;
  80.        
  81. throw; // To re-throw the same exception
  82. throw new Exception("bla", ex); // To specify a new message, or change the
  83.                                 // exception type. A new stack trace is created
  84.                                 // but you can still access the original one
  85.                                 // using InnerException