2 // System.Net.IPEndPoint.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Net.Sockets;
12 namespace System.Net {
14 public class IPEndPoint : EndPoint {
16 public const int MaxPort = 65535;
17 public const int MinPort = 0;
19 public IPEndPoint (IPAddress address, int port)
22 throw new ArgumentNullException ("Value cannot be null");
28 public IPEndPoint (long iaddr, int port) : this (new IPAddress (iaddr), port)
32 private IPAddress address;
33 public IPAddress Address {
42 public override AddressFamily AddressFamily {
44 return AddressFamily.InterNetwork;
54 // LAMESPEC: no mention of sanity checking
55 // PS: MS controls the range when setting the value
56 if (value < MinPort || value > MaxPort)
57 throw new ArgumentOutOfRangeException ("Invalid port");
63 // bytes 2 and 3 store the port, the rest
65 public override EndPoint Create(SocketAddress sockaddr) {
66 int size=sockaddr.Size;
68 // LAMESPEC: no mention of what to do if
71 // absolute minimum amount needed for
72 // an address family, buffer size,
76 AddressFamily family=(AddressFamily)sockaddr[0];
77 if(family!=AddressFamily.InterNetwork) {
81 int port=(((int)sockaddr[2])<<8) + (int)sockaddr[3];
82 long address=(((long)sockaddr[4])<<24) +
83 (((long)sockaddr[5])<<16) +
84 (((long)sockaddr[6])<<8) +
87 IPEndPoint ipe = new IPEndPoint(address, port);
92 public override SocketAddress Serialize() {
93 // .net produces a 16 byte buffer, even though
94 // only 8 bytes are used. I guess its just a
95 // holdover from struct sockaddr padding.
96 SocketAddress sockaddr = new SocketAddress(AddressFamily.InterNetwork, 16);
98 // bytes 2 and 3 store the port, the rest
100 sockaddr[2]=(byte)((port>>8) & 0xff);
101 sockaddr[3]=(byte)(port & 0xff);
103 sockaddr[4]=(byte)((address.Address >> 24) & 0xff);
104 sockaddr[5]=(byte)((address.Address >> 16) & 0xff);
105 sockaddr[6]=(byte)((address.Address >> 8) & 0xff);
106 sockaddr[7]=(byte)(address.Address & 0xff);
111 public override string ToString() {
112 return(address.ToString() + ":" + port);
115 public override bool Equals (Object obj)
117 if (obj is System.Net.IPEndPoint) {
118 return ( ((IPEndPoint) obj).port == port &&
119 ((IPEndPoint) obj).address == address);
125 public override int GetHashCode ()
127 return address.GetHashCode () + port;