Description
An AddressFamily member is specified to the Socket class constructors to identify the addressing scheme that the socket instance will use to resolve an address. For example, InterNetwork indicates that an IP version 4 address is expected when a Socket instance connects to an endpoint.
Example
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class AddressFamilySample
{
public static void Main()
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
Socket skt = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
try
{
IPEndPoint ep = new IPEndPoint(ip, 9999);
skt.Connect(ep);
Byte[] req = Encoding.ASCII.GetBytes("Test");
skt.SendTo(req, ep);
IPEndPoint rep = (IPEndPoint)skt.LocalEndPoint;
Console.WriteLine("LocalEndPoint details:");
Console.WriteLine("Address: {0}", rep.Address);
Console.WriteLine("AddressFamily: {0}", rep.AddressFamily);
Console.WriteLine("Port: {0}", rep.Port);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
finally
{
skt.Close();
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
}
}
The output is
LocalEndPoint details:
Address: 127.0.0.1
AddressFamily: InterNetwork
Port: 4082
Press Enter to continue
|