public Connect ( |
||
remoteEP | ||
return | void |
using System.Net; using System.Net.Sockets; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse("192.168.1.1"); IPEndPoint remoteEP = new IPEndPoint(ip, 80); socket.Connect(remoteEP);
using System; using System.Net; using System.Net.Sockets; class Program { static void Main(string[] args) { IPAddress[] hostAddresses = Dns.GetHostAddresses("www.google.com"); IPAddress ipAddress = hostAddresses[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, 80); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(remoteEP); } }In this example, we use the GetHostAddresses method of the Dns class to retrieve the IP addresses associated with a host name. We then create an instance of the IPEndPoint class using the first IP address and a port number. Finally, we create a new Socket object, set its properties, and use the Connect method to connect the socket to the remote endpoint. Both examples are using the System.Net.Sockets package library.