/// <summary> /// Creates the <see cref="CreateConnectionSocket"/> using the specified options and connects the socket. /// Assigns <see cref="LocalEndPoint"/> and <see cref="RemoteEndPoint"/> /// </summary> /// <param name="addressFamily"></param> /// <param name="socketType"></param> /// <param name="protocolType"></param> /// <param name="addressList"></param> /// <param name="port"></param> public virtual void Connect(System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType, System.Net.IPAddress[] addressList, int port) { try { //Create the socket CreateConnectionSocket(addressFamily, socketType, protocolType); //Sample the clock LasRemoteConnectionStartedDateTime = System.DateTime.UtcNow; //Connect the socket ConnectionSocket.Connect(addressList, port); //Sample the clock LastRemoteConnectionCompletedDateTime = System.DateTime.UtcNow; } finally { //Assign the NetworkInterface NetworkInterface = Common.Extensions.NetworkInterface.NetworkInterfaceExtensions.GetNetworkInterface(ConnectionSocket); //Assign the LocalEndPoint LocalEndPoint = (System.Net.IPEndPoint)ConnectionSocket.LocalEndPoint; //Assign the RemoteEndPoint RemoteEndPoint = (System.Net.IPEndPoint)ConnectionSocket.RemoteEndPoint; //Call Connect to FlagConnected and call base logic. Connect(); } }
void LoopConnect() { Console.WriteLine("Connecting..."); int attempts = 0; while (!ConnectionSocket.Connected) { try { attempts++; ConnectionSocket.Connect(ServerAddress, Port); connectionLive = true; } catch (SocketException) { Console.Clear(); Console.WriteLine("Failed, Connection attempts: " + attempts); } } Console.Clear(); Console.WriteLine("Connected to " + ServerAddress + ":" + Port); }