using System.Runtime.InteropServices;
[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
[System.Runtime.InteropServices.FieldOffset(0)]
[System.Runtime.InteropServices.FieldOffset(0)]
[System.Runtime.InteropServices.FieldOffset(1)]
[System.Runtime.InteropServices.FieldOffset(2)]
[System.Runtime.InteropServices.FieldOffset(3)]
public IpAddress(int address) {
Byte1 = Byte2 = Byte3 = Byte4 = 0;
public override string ToString() {
return (new System.Text.StringBuilder()).Append(Byte1).Append('.')
.Append(Byte2).Append('.')
.Append(Byte3).Append('.')
.Append(Byte4).ToString();
enum Protocol : short {http,https,ftp,sftp,tcp};
[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
[System.Runtime.InteropServices.FieldOffset(0)]
public IpAddress Address;
[System.Runtime.InteropServices.FieldOffset(4)]
[System.Runtime.InteropServices.FieldOffset(6)]
public Protocol AppProtocol;
[System.Runtime.InteropServices.FieldOffset(0)]
public Service(IpAddress addr, ushort port, Protocol prot) {
public Service(long val) {
Address = new IpAddress(0);
AppProtocol = Protocol.http;
return new Service(Payload);
public override string ToString() {
return AppProtocol + "//" + Address + ":" + Port + "/";
public static void Main()
var ip = new IpAddress((new Random()).Next());
Console.Out.WriteLine( "Size (bytes): " + Marshal.SizeOf(ip) +
". Value:\t\t" + ip.intAddress + "\t=\t" + ip);
var s1 = new Service(ip,8080,Protocol.https);
var s2 = new Service(s1.Payload);
s2.AppProtocol = Protocol.ftp;
Console.Out.WriteLine("Size (bytes): " + Marshal.SizeOf(s1) +
". Value: " + s1.Payload + "\t=\t" + s1);
Console.Out.WriteLine("Size (bytes): " + Marshal.SizeOf(s2) +
". Value: " + s2.Payload + "\t=\t" + s2);