本文整理汇总了C#中AddressFamily类的典型用法代码示例。如果您正苦于以下问题:C# AddressFamily类的具体用法?C# AddressFamily怎么用?C# AddressFamily使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AddressFamily类属于命名空间,在下文中一共展示了AddressFamily类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateSocket
public Socket CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
{
SetAddressFamily(addressFamily);
SetProtocolType(protocolType);
SetSocketType(socketType);
return new Socket(this.addressFamily, this.socketType, this.protocolType);
}
示例2: IPAddress
/// <devdoc>
/// <para>
/// Constructor for an IPv6 Address with a specified Scope.
/// </para>
/// </devdoc>
public IPAddress(byte[] address,long scopeid) {
if (address==null) {
throw new ArgumentNullException("address");
}
if(address.Length != IPv6AddressBytes){
throw new ArgumentException(SR.GetString(SR.dns_bad_ip_address), "address");
}
m_Family = AddressFamily.InterNetworkV6;
for (int i = 0; i < NumberOfLabels; i++) {
m_Numbers[i] = (ushort)(address[i * 2] * 256 + address[i * 2 + 1]);
}
//
// Consider: Since scope is only valid for link-local and site-local
// addresses we could implement some more robust checking here
//
if ( scopeid < 0 || scopeid > 0x00000000FFFFFFFF ) {
throw new ArgumentOutOfRangeException("scopeid");
}
m_ScopeId = scopeid;
}
示例3: GetDisposedSocket
private static Socket GetDisposedSocket(AddressFamily addressFamily = AddressFamily.InterNetwork)
{
using (var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp))
{
return socket;
}
}
示例4: UdpClient
public UdpClient(int port, AddressFamily family)
{
IPEndPoint point;
this.m_Buffer = new byte[0x10000];
this.m_Family = AddressFamily.InterNetwork;
if (!ValidationHelper.ValidateTcpPort(port))
{
throw new ArgumentOutOfRangeException("port");
}
if ((family != AddressFamily.InterNetwork) && (family != AddressFamily.InterNetworkV6))
{
throw new ArgumentException(SR.GetString("net_protocol_invalid_family"), "family");
}
this.m_Family = family;
if (this.m_Family == AddressFamily.InterNetwork)
{
point = new IPEndPoint(IPAddress.Any, port);
}
else
{
point = new IPEndPoint(IPAddress.IPv6Any, port);
}
this.createClientSocket();
this.Client.Bind(point);
}
示例5: init
/// <summary>
/// Initializes the socket
/// </summary>`
public void init(int offset, string ipAddress)
{
socketType = SocketType.Stream;
protocolType = ProtocolType.Tcp;
//addressFamily = AddressFamily.InterNetwork;
addressFamily = AddressFamily.InterNetwork;
try
{
ipEntry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress[] addr = ipEntry.AddressList;
//endpoint = new IPEndPoint(addr[0], port);
for (int i = 0; i < addr.Length; i++)
{
Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
}
if (ipAddress == "")
{
address = addr[addr.Length - offset];
}
else
{
address = IPAddress.Parse(ipAddress);
}
Console.WriteLine("Using the Address {0}: {1}", address.ToString(), port);
endpoint = new IPEndPoint(address, port);
}
catch (SocketException ex)
{
System.Console.WriteLine(ex.Message);
}
createSocket();
}
示例6: TestAddressFamilyObsolete
public void TestAddressFamilyObsolete(bool ipv6, AddressFamily addressFamily)
{
#pragma warning disable 618
var settings = new MongoServerSettings { IPv6 = ipv6 };
Assert.AreEqual(addressFamily, settings.AddressFamily);
#pragma warning restore
}
示例7: SocketManager
public SocketManager(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
{
_addressFamily = addressFamily;
_socketType = socketType;
_protocolType = protocolType;
_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
示例8: IPAddressRange
/// <param name="ipRangeStr">
/// e.g)
/// "10.23.0.0/24",
/// "127.0.0.1" (equals to "127.0.0.1/32"),
/// "2001:0db8:bd05:01d2:288a:1fc0:0001:0000/16",
/// "::1" (equals to "::1/128")
/// </param>
public IPAddressRange(string ipRangeString)
{
if (string.IsNullOrEmpty(ipRangeString))
throw new InvalidOperationException("IP Address is null or empty.");
var vals = ipRangeString.Split('/');
IPAddress ipAddr;
if (!IPAddress.TryParse(vals[0], out ipAddr))
throw new InvalidOperationException(string.Format("IP Address({0}) is invalid format.", ipRangeString));
_addressFamily = ipAddr.AddressFamily;
if (_addressFamily != AddressFamily.InterNetwork && _addressFamily != AddressFamily.InterNetworkV6)
throw new InvalidOperationException(string.Format("IP Address({0}) is not ip4 or ip6 address famiry.", ipRangeString));
var maxMaskRange = _addressFamily == AddressFamily.InterNetwork ? 32 : 128;
int maskRange;
if (vals.Length > 1)
{
if (!int.TryParse(vals[1], out maskRange) || maskRange < 0 || maskRange > maxMaskRange)
throw new InvalidOperationException(string.Format("IP Address({0}) is invalid range.", ipRangeString));
}
else
maskRange = maxMaskRange;
_netowrkAddressBytes = ipAddr.GetAddressBytes();
_subnetMaskBytes = Enumerable.Repeat<byte>(0xFF, _netowrkAddressBytes.Length).ToArray();
for (int i = 0; i < (maxMaskRange - maskRange); i++)
_subnetMaskBytes[_subnetMaskBytes.Length - 1 - i / 8] -= (byte)(1 << (i % 8));
}
示例9: SocketServer
public SocketServer(int portNumber, AddressFamily addressFamily, SocketType socketType,
ProtocolType protocolType)
{
this.portNumber = portNumber;
this.addressFamily = addressFamily;
this.socketType = socketType;
this.protocolType = protocolType;
clientAddresses = new List<string>();
clientTable = new Dictionary<string, StateObject>();
recentlyReceivedStates = new List<StateObject>();
removeList = new List<string>();
listenEvent = new ManualResetEvent(false);
shutDownEvent = new ManualResetEvent(false);
sendEvent = new ManualResetEvent(false);
listener = null;
sendBufferSize = INITIAL_BUFFER_SIZE;
recvBufferSize = INITIAL_BUFFER_SIZE;
ShutdownWaitTimeout = 5000;
enableEncryption = false;
initialized = false;
copyingRecvBuffer = false;
isShuttingDown = false;
transmittingData = false;
receivingData = false;
}
示例10: RawSocket
public RawSocket(AddressFamily family, SocketProvider provider)
{
m_rawsock = new Socket(family, SocketType.Raw, ProtocolType.IP);
m_rawsock.Blocking = false;
m_Provider = provider;
m_ReceivedData = new AsyncCallback(OnReceive);
}
示例11: Button_Click
private async void Button_Click(object sender, RoutedEventArgs e)
{
// Timer a = new Timer();
//
//Ping pingSender = new Ping();
////for(int i=0;i<30;i++)
////{
// // MessageBox.Show(adr);
String adr ="172.16.16.10";
IPAddress address = IPAddress.Parse(adr);
// // pingSender.SendAsync(address,250,new PingOptions(5,true));
// pingSender.SendAsync(address, 250);
//pingSender.PingCompleted += (a, b) =>
//{
// if (b.Reply.Status == IPStatus.Success)
// {
// MessageBox.Show(b.ToString());
// }
// else
// {
// MessageBox.Show("failed");
// }
//};
//PingReply reply = await pingSender.SendPingAsync(address);
//if (reply.Status == IPStatus.Success)
//{
// ListBox1.Items.Add(adr);
//}
//else
//{
// ListBox2.Items.Add("Failed "+adr); // MessageBox.Show("falied" + adr);
//}
//}
AddressFamily d=new AddressFamily();
try
{
Ping s=new Ping();
var a= s.SendPingAsync(address).GetAwaiter();
a.OnCompleted(() => MessageBox.Show(a.GetResult().Status.ToString()));
//UdpClient udp=new UdpClient(8080);
// udp.Connect(address,8080);
// var a=new byte[]{1,2};
// await udp.SendAsync(a,44);
// TcpClient tcp = new TcpClient();
//await tcp.ConnectAsync(adr, 8080);
// MessageBox.Show(adr);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
示例12: Reset
public void Reset (Socket socket, IPEndPoint ip)
{
this.addressFamily = socket.AddressFamily;
this.socketType = socket.SocketType;
this.protocolTtype = socket.ProtocolType;
this.RemoteEndPoint = ip;
}
示例13: DnsEndPoint
public DnsEndPoint(string host, int port, AddressFamily addressFamily)
{
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
if (String.IsNullOrEmpty(host))
{
throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(host)));
}
if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
{
throw new ArgumentOutOfRangeException(nameof(port));
}
if (addressFamily != AddressFamily.InterNetwork &&
addressFamily != AddressFamily.InterNetworkV6 &&
addressFamily != AddressFamily.Unspecified)
{
throw new ArgumentException(SR.net_sockets_invalid_optionValue_all, nameof(addressFamily));
}
_host = host;
_port = port;
_family = addressFamily;
}
示例14: Address
/// <summary>
/// Initializes a new instance of the <see cref="Address"/> class.
/// </summary>
/// <param name="addressFamily">The IP address family</param>
/// <param name="address">The IP address</param>
/// <param name="port">The port</param>
public Address(AddressFamily addressFamily, string address, int port)
{
_isEnhanced = true;
AddressFamily = addressFamily;
IpAddress = address;
IpPort = port;
}
示例15: IsProtocolSupported
private static bool IsProtocolSupported(AddressFamily af)
{
int family;
switch (af)
{
case AddressFamily.InterNetwork:
family = Interop.libc.AF_INET;
break;
case AddressFamily.InterNetworkV6:
family = Interop.libc.AF_INET6;
break;
default:
Debug.Fail("Invalid address family: " + af.ToString());
throw new ArgumentException("af");
}
int socket = -1;
try
{
socket = Interop.libc.socket(family, Interop.libc.SOCK_DGRAM, 0);
if (socket == -1)
{
return Interop.Sys.GetLastError() != Interop.Error.EAFNOSUPPORT;
}
return true;
}
finally
{
if (socket != -1)
{
Interop.Sys.Close(socket);
}
}
}