Not a member yet? Register for your free account!


You last visited: Today at 00:07

  • Did you know? elitepvpers has its own image host, epvpimg.com.

 

[Advanced] Winsock in C#

This is a discussion on [Advanced] Winsock in C# within the CO2 Programming forum part of the CO2 Main - Discussions / Questions category; Requires ws2_32.dll, and "using System.Runtime.InteropServices" Code: // fuck creating my own socketerror enum LOL using SocketError = System.Net.Sockets.SocketError; Code: // ...

Reply
 
Thread Tools
Old 08-16-2008, 18:55   #1
ぼくは凶悪な
 
InfamousNoone's Avatar
 
Join Date: Jan 2008
Posts: 1,806
Received Thanks: 2,483
[Advanced] Winsock in C#


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;
    }
__________________

InfamousNoone is online now  
Thanks
6 Users
Arco. (03-12-2011), Belth (06-13-2010), Epic-Chaos (04-18-2013), Hiyoal (08-17-2008), tariqx111 (05-23-2013), _DreadNought_ (04-16-2013)
Old 08-17-2008, 03:19   #2
/:AutoIt Legend:\
 
Hiyoal's Avatar
 
Join Date: Mar 2007
Posts: 2,445
Received Thanks: 1,061
Nice friggen work there Inf
Ima learn from this

Hiyoal
Hiyoal is offline  
Old 08-18-2008, 18:10   #3
CO2 Moderator
 
leavemealone's Avatar
 
Join Date: May 2006
Posts: 2,165
Received Thanks: 8,537
Damn, you really went all out xD
leavemealone is offline  
Old 08-19-2008, 09:25   #4
Junior Member
 
GhostKnight's Avatar
 
Join Date: Aug 2008
Posts: 2
Received Thanks: 0
i donot understand any of this Shits
GhostKnight is offline  
Old 08-19-2008, 10:12   #5
/:AutoIt Legend:\
 
Hiyoal's Avatar
 
Join Date: Mar 2007
Posts: 2,445
Received Thanks: 1,061
Thats why it says "[Advanced]" as it is for advanced C# users in C# Sockets.

Hiyoal
Hiyoal is offline  
Old 04-16-2013, 06:40   #6
Junior Member
 
Join Date: Apr 2013
Posts: 1
Received Thanks: 0
i am new to win socket will you let us know how to use it.
vishalj is offline  
Old 04-19-2013, 00:13   #7
Do you even byte, bro?
 
Super Aids's Avatar
 
Join Date: Dec 2012
Posts: 476
Received Thanks: 166
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. ]
Super Aids is offline  
Old 05-23-2013, 18:12   #8
TariQoO
 
Join Date: Jul 2011
Posts: 85
Received Thanks: 8
when you hook with .net,
sin addr ip is alwasy 16.0.0.0
after using inet_ntoa
tariqx111 is offline  
Reply

Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
[C++] Winsock + Select Flyff_Service C/C++ 13 12-03-2009 17:43
[C++] winsock ws_32.dll hook majidemo C/C++ 5 06-05-2009 18:58
Vb6 : A Simple Winsock Connection ! daniellook .NET Languages 0 04-12-2009 19:30
winsock in vb.net? Real~Death CO2 Main - Discussions / Questions 4 12-29-2007 20:23
vb6 winsock tutorial Maybe It's Maybelline .NET Languages 2 10-24-2006 18:51




All times are GMT +2. The time now is 00:07.


Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.