/// <summary> /// Establishes a connection to the specified remote host. /// </summary> /// <returns>True if we connected, false if we didn't</returns> public void Connect() { _dest = Address.Resolve(_hostname, _port); if (_dest == null) return; Logger.InfoFormat(this, "Connecting to: {0} on port {1}", _dest.IP.ToString(), _port.ToString()); if (!_dest.IPV6) { Logger.Debug(this, "Connecting using IPv4"); _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } else { Logger.Debug(this, "Connecting using IPv6"); _socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); } try { //_socket.Connect(_dest.EndPoint); _socket.BeginConnect(_dest.EndPoint, new AsyncCallback(FinishConnect), null); if(_resetEvent.WaitOne(_timeout, false)) { if (_connected) { _netstream = new NetworkStream(_socket, true); _stream = _netstream; _stream.BeginRead(_buff, 0, _buff.Length, new AsyncCallback(Receive), null); _states.State = new ConnectedState(); _states.Execute(null); } } else { Errors.Instance.SendError(this, ErrorType.ConnectionTimeout, "Timed out while connecting to server."); } } catch (SocketException) { //We Failed to connect //TODO: Return an error using the Errors class so that the hosting application can take action. } }
private static Address ResolveDNS(string hostname, int port) { Address temp; temp = new Address(hostname, port); Logger.Debug(typeof(Address), "Getting DNS addresses"); NetworkInterface[] net = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface n in net) { if (n.OperationalStatus == OperationalStatus.Up && n.NetworkInterfaceType != NetworkInterfaceType.Loopback) { IPInterfaceProperties i = n.GetIPProperties(); foreach (IPAddress dns in i.DnsAddresses) { if (dns.AddressFamily == AddressFamily.InterNetwork) { _dns.Add(dns); Logger.DebugFormat(typeof(Address), "Dns Address: {0}", dns.ToString()); } } } } try { string lookup; SRVRecord[] srv = Resolver.SRVLookup("_xmpp-client._tcp." + hostname, _dns[0]); if (srv.Length > 0) { Logger.DebugFormat(typeof(Address), "SRV Address: {0}", srv[0].Target); lookup = srv[0].Target; _port = srv[0].Port; } else lookup = hostname; Request req = new Request(); req.AddQuestion(new Question(lookup, DnsType.ANAME, DnsClass.IN)); Response resp = Resolver.Lookup(req, _dns[0]); if (resp.Answers.Length > 0 && resp != null) { foreach (Answer ans in resp.Answers) { if (ans.Type == DnsType.ANAME) { ANameRecord rec = (ANameRecord)ans.Record; temp.IP = rec.IPAddress; } else { Logger.Debug(typeof(Address), "DNS Type: " + ans.Type.ToString()); } } } else { Logger.Error(typeof(Address), "Error resolving DNS address."); return null; } temp._end = new IPEndPoint(temp.IP, _port); } catch (Exception e) { Logger.ErrorFormat(typeof(Address), "Error resolving DNS address: {0}", e); return null; } return temp; }
private static Address ResolveSystem(string hostname, int port) { Address temp; temp = new Address(hostname, port); try { IPAddress addr = Dns.GetHostEntry(hostname).AddressList[0]; temp._ip = addr; temp._end = new IPEndPoint(addr, _port); } catch (Exception e) { Logger.ErrorFormat(typeof(Address), "Error resolving address: {0}", e); return null; } return temp; }
public AsyncSocket() { _dest = new Address(); }