Tuesday, September 19, 2006

Does your network application support IPv6?

One of the ways to find out about this is to add an IPv6 address to you computer, and make the application use it. If you observe no crashes and connectivity is fine, then you're okay and there is no need to read further :8-).

What can you do to be IPv6 "compatible"? At first start from here.

If your application is managed one and you use sockets for network I/O then the only thing you should remeber is to check IpAddress.AddressFamily property.

In code this can look like this (error checking is removed for simplicity's sake)

public void Connect(string host, int port)


{

IPHostEntry ipHostEntry = Dns.GetHostEntry(host);

//and now we're creating socket with appropriate address family

IPEndPoint ipEP = new IPEndPoint(ipHostEntry.AddressList[0], port);

Socket socket = new Socket(ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

socket.Connect(ipEP);

}



Many developers are creating sockets assuming that there will always be IPv4. Generally this works as IPv6 addresses are not common these days. But times are changing and we have to be prepared...

Wednesday, September 06, 2006

Finance news from Ukraine

Once again I had to use Feedburner to convert feed fomat. The trick is the same as described here. This time it was http://news.finance.ua/ua/rss. I'm using IE7 RC1 and it doesn't recognize format of this feed.

So, here is new feed http://feeds.feedburner.com/Financeua in the RSS 2.0 format.

.NET: tricky enums with custom attributes

Sometimes, when we want to serialize data type we use attributes to give additional description for type itself and its fields.

Imagine that the type we want to serialize has enum field (SampleEnum)


public class ExtendedInfoAttribute : Attribute
{
string description;

public string Description
{
get { return description; }
set { description = value; }
}

}



public enum SampleEnum
{
[ExtendedInfo(Description="First value")]
EnumValueOne,
[ExtendedInfo(Description = "Second value")]
EnumValueTwo
}



ExtendedInfo attribute provides additional info about enum fields. When serializing this attribute value can be used to provide some additional info about enum fields.


So, what's so special about getting these attribute values? Well, nothing special if you known where to look for :8-)

//errors checking is omitted for clarity
SampleEnum sEnum = SampleEnum.EnumValueTwo;

Type type = sEnum.GetType();
FieldInfo fieldInfo = type.GetField(Enum.GetName(type, sEnum));
ExtendedInfoAttribute[] attrs = (ExtendedInfoAttribute[])fieldInfo.GetCustomAttributes(
typeof(ExtendedInfoAttribute), false);
Console.WriteLine(attrs[0].Description);

Sunday, September 03, 2006