Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 14:49

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Advanced] Winsock in C#

Discussion on [Advanced] Winsock in C# within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
InfamousNoone's Avatar
 
elite*gold: 20
Join Date: Jan 2008
Posts: 2,012
Received Thanks: 2,882
[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 offline  
Thanks
7 Users
Old 08/17/2008, 03:19   #2
 
Hiyoal's Avatar
 
elite*gold: 20
Join Date: Mar 2007
Posts: 2,444
Received Thanks: 1,066
Nice friggen work there Inf
Ima learn from this

Hiyoal
Hiyoal is offline  
Old 08/18/2008, 18:10   #3
 
leavemealone's Avatar
 
elite*gold: 0
Join Date: May 2006
Posts: 2,168
Received Thanks: 8,592
****, you really went all out xD
leavemealone is offline  
Old 08/19/2008, 09:25   #4
 
GhostKnight's Avatar
 
elite*gold: 0
Join Date: Aug 2008
Posts: 2
Received Thanks: 0
i donot understand any of this *****
GhostKnight is offline  
Old 08/19/2008, 10:12   #5
 
Hiyoal's Avatar
 
elite*gold: 20
Join Date: Mar 2007
Posts: 2,444
Received Thanks: 1,066
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
 
elite*gold: 0
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
 
Super Aids's Avatar
 
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 946
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.

Super Aids is offline  
Old 05/23/2013, 18:12   #8
 
elite*gold: 0
Join Date: Jul 2011
Posts: 98
Received Thanks: 12
when you hook with .net,
sin addr ip is alwasy 16.0.0.0
after using inet_ntoa
tariqx111 is offline  
Reply


Similar Threads Similar Threads
[C++] Winsock + Select
12/03/2009 - C/C++ - 13 Replies
I solved this Problem adding a "continue;" at if(FD_ISSET(aSocket, &fdSet)) { AddSocket(accept(aSocket, NULL, NULL)); continue; }
[C++] winsock ws_32.dll hook
06/05/2009 - C/C++ - 5 Replies
i am trying to make a hack for a online game. but i don't know how to hook my program to the process name : khanclient.exe can someone help me? this is my current code in VB2008 the point in this is i want to click a button and send a packet to server from client. i hope you get what i mean. here is a screenshot:
Vb6 : A Simple Winsock Connection !
04/12/2009 - .NET Languages - 0 Replies
I made a little tut for a simple winsock connection In visual basic 2006 ! So here it comes Start with opening vb6 http://img412.imageshack.us/img412/9527/54328939. jpg Now open Standard EXE http://img258.imageshack.us/img258/5764/73925545. jpg
winsock in vb.net?
12/29/2007 - Conquer Online 2 - 4 Replies
anyone know how to get winsock into vb.net?ive been googleing for an answer and cant findone. i downloaded oswinsock and still not sure how to use it?? Ive been using vb6 for a while and am using vb 2005 for the first time and this is pissing me off.i read i should use systemcom.net or somthing? just anyone know how i can use winsock in vb.net or explain what i need to do,before i give up on this .net framework and stick with my trusty vb6 if anyone has a simple source proxy in...
vb6 winsock tutorial
10/24/2006 - .NET Languages - 2 Replies
Ich weiß, vb6 ist aaaaaaaalllt und 2005 ist sooooo besser (kann ich auch gleich zu C# gehen), aber fürs erste will ich das noch probieren. Okay, ich suche ein Winsock tutorial was allerdings direkt HEX packets schickt/verarbeitet und nicht im ASCII format, ich hab zwar ein paar Beispiel sources, allerdings versteh ich nicht so recht wie ich das machen soll. Also ne TCP verbindung.



All times are GMT +2. The time now is 14:49.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.