[Advanced] Winsock in C#

08/16/2008 18:55 InfamousNoone#1
Requires ws2_32.dll, and "using System.Runtime.InteropServices"

Code:
    // fuck creating my own socketerror enum LOL
    using SocketError = System.Net.Sockets.SocketError;
Code:
    // Interface to ws2_32.dll
    public unsafe partial class Native
    {
        public const int SOCKET_ERROR = -1;
        public const int INVALID_SOCKET = ~0;

        [DllImport("Ws2_32.dll")]
        public static extern int WSAStartup(ushort Version, out WSAData Data);
        [DllImport("Ws2_32.dll")]
        public static extern SocketError WSAGetLastError();
        [DllImport("Ws2_32.dll")]
        public static extern SOCKET socket(AddressFamily af, SocketType type, ProtocolType protocol);
        [DllImport("Ws2_32.dll")]
        public static extern int send(SOCKET s, byte* buf, int len, int flags);
        [DllImport("Ws2_32.dll")]
        public static extern int recv(SOCKET s, byte* buf, int len, int flags);
        [DllImport("Ws2_32.dll")]
        public static extern SOCKET accept(SOCKET s, void* addr, int addrsize);
        [DllImport("Ws2_32.dll")]
        public static extern int listen(SOCKET s, int backlog);
        [DllImport("Ws2_32.dll", CharSet = CharSet.Ansi)]
        public static extern uint inet_addr(string cp);
        [DllImport("Ws2_32.dll")]
        public static extern ushort htons(ushort hostshort);
        [DllImport("Ws2_32.dll")]
        public static extern int connect(SOCKET s, sockaddr_in* addr, int addrsize);
        [DllImport("Ws2_32.dll")]
        public static extern int closesocket(SOCKET s);
        [DllImport("Ws2_32.dll")]
        public static extern int getpeername(SOCKET s, sockaddr_in* addr, int* addrsize);
        [DllImport("Ws2_32.dll")]
        public static extern int bind(SOCKET s, sockaddr_in* addr, int addrsize);
        [DllImport("Ws2_32.dll")]
        public static extern int select(int ndfs, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, timeval* timeout);
        [DllImport("Ws2_32.dll")]
        public static extern sbyte* inet_ntoa(sockaddr_in.in_addr _in);
    }
Code:
    // Enums from Microsofts website (#defined in C++)
    public enum AddressFamily : int
    {
        Unknown = 0,
        InterNetworkv4 = 2,
        Ipx = 4,
        AppleTalk = 17,
        NetBios = 17,
        InterNetworkv6 = 23,
        Irda = 26,
        BlueTooth = 32
    }
    public enum SocketType : int
    {
        Unknown = 0,
        Stream = 1,
        DGram = 2,
        Raw = 3,
        Rdm = 4,
        SeqPacket = 5
    }
    public enum ProtocolType : int
    {
        BlueTooth = 3,
        Tcp = 6,
        Udp = 17,
        ReliableMulticast = 113
    }
Code:
    // Equivilent to C++s "SOCKET"
    public unsafe struct SOCKET
    {
        private void* handle;
        private SOCKET(int _handle)
        {
            handle = (void*)_handle;
        }
        public static bool operator ==(SOCKET s, int i)
        {
            return ((int)s.handle == i);
        }
        public static bool operator !=(SOCKET s, int i)
        {
            return ((int)s.handle != i);
        }
        public static implicit operator SOCKET(int i)
        {
            return new SOCKET(i);
        }
        public static implicit operator uint(SOCKET s)
        {
            return (uint)s.handle;
        }
        public override bool Equals(object obj)
        {
            return (obj is SOCKET) ? (((SOCKET)obj).handle == this.handle) : base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return (int)handle;
        }
    }
Code:
    // fd_set used in 'select' method
    public unsafe struct fd_set
    {
        public const int FD_SETSIZE = 64;
        public uint fd_count;
        public fixed uint fd_array[FD_SETSIZE];
    }
Code:
    // C# equivilent to C++'s sockaddr_in / SOCKADDR_IN
    [StructLayout(LayoutKind.Sequential, Size=16)]
    public struct sockaddr_in
    {
        public const int Size = 16;

        public short sin_family;
        public ushort sin_port;
        public struct in_addr
        {
            public uint S_addr;
            public struct _S_un_b
            {
                public byte s_b1, s_b2, s_b3, s_b4;
            }
            public _S_un_b S_un_b;
            public struct _S_un_w
            {
                public ushort s_w1, s_w2;
            }
            public _S_un_w S_un_w;
        }
        public in_addr sin_addr;
    }
Code:
    // WSAData structure, used in WSAStarutp
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
    public unsafe struct WSAData
    {
        public ushort Version;
        public ushort HighVersion;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
        public string Description;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]
        public string SystemStatus;
        public ushort MaxSockets;
        public ushort MaxUdpDg;
        sbyte* lpVendorInfo;
    }
08/17/2008 03:19 Hiyoal#2
Nice friggen work there Inf :D
Ima learn from this :o

Hiyoal
08/18/2008 18:10 leavemealone#3
Damn, you really went all out xD
08/19/2008 09:25 GhostKnight#4
i donot understand any of this Shits
08/19/2008 10:12 Hiyoal#5
Thats why it says "[Advanced]" as it is for advanced C# users in C# Sockets.

Hiyoal
04/16/2013 06:40 vishalj#6
i am new to win socket will you let us know how to use it.
04/19/2013 00:13 Super Aids#7
Quote:
Originally Posted by vishalj View Post
i am new to win socket will you let us know how to use it.
It's pretty simple. You learn to program in C# and the understanding of PInvoke and implementing API's using it.

As for winsock.

[Only registered and activated users can see links. Click Here To Register...]
05/23/2013 18:12 tariqx111#8
when you hook with .net,
sin addr ip is alwasy 16.0.0.0
after using inet_ntoa