public BeginConnect ( |
||
remoteEP | ||
callback | AsyncCallback | |
state | object | |
return | IAsyncResult |
using System; using System.Net; using System.Net.Sockets; public class Example { public static void Main() { IPAddress address = IPAddress.Parse("192.0.2.1"); int port = 80; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.BeginConnect(address, port, ConnectCallback, socket); Console.WriteLine("Connecting to {0}:{1}", address, port); Console.ReadLine(); } public static void ConnectCallback(IAsyncResult asyncResult) { Socket socket = (Socket) asyncResult.AsyncState; try { socket.EndConnect(asyncResult); Console.WriteLine("Connected!"); } catch (SocketException) { Console.WriteLine("Unable to connect."); } } }
using System; using System.Net; using System.Net.Sockets; public class Example { public static void Main() { string domain = "www.example.com"; int port = 443; IPHostEntry host = Dns.GetHostEntry(domain); IPAddress address = host.AddressList[0]; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.BeginConnect(address, port, ConnectCallback, socket); Console.WriteLine("Connecting to {0}:{1}", domain, port); Console.ReadLine(); } public static void ConnectCallback(IAsyncResult asyncResult) { Socket socket = (Socket) asyncResult.AsyncState; try { socket.EndConnect(asyncResult); Console.WriteLine("Connected!"); } catch (SocketException) { Console.WriteLine("Unable to connect."); } } }This example performs a DNS lookup for the domain "www.example.com" and initiates an asynchronous connection attempt to the HTTPS server at the first IP address in the resulting list on port 443. The ConnectCallback function is called upon completion of the connection attempt, which will display a message indicating success or failure. Package library: System.dll
public BeginConnect ( |
||
remoteEP | ||
callback | AsyncCallback | |
state | object | |
return | IAsyncResult |