当前位置:编程学习 > C#/ASP.NET >>

ws2_32.dll getpeername这个api在c#中如何使用?

想通过套接字的标识符获取到ip地址和端口,必须要用这个api。但是研究了一下午始终无法在c#中使用。求大神解答

现在的声明:

 [DllImport("ws2_32.dll")]
        static unsafe extern int getpeername(int s,Hook.sockaddr* p,int namelen);

sockaddr:

 struct sockaddr
    {
        /// <summary>
        /// 网络地址
        /// </summary>
        public int sin_family;//网络地址
        /// <summary>
        /// 端口
        /// </summary>
        public int sin_port;//端口

        /// <summary>
        /// 地址
        /// </summary>
        public int sin_addr;//地址
        /// <summary>
        /// 地址范围
        /// </summary>
        public byte[] sin_zero;//地址范围
    }


这些声明肯定是不对的。写出来当个参考 --------------------编程问答--------------------
using System;
using System.Runtime.InteropServices;

namespace rtmpsuckwinzoz
{
   
        public static class NativeSocketMethode
        {
            public enum AddressFamily
            {
                Unknown,
                InterNetworkv4 = 2,
                Ipx = 4,
                AppleTalk = 17,
                NetBios = 17,
                InterNetworkv6 = 23,
                Irda = 26,
                BlueTooth = 32
            }
            public enum SocketType
            {
                Unknown,
                Stream,
                DGram,
                Raw,
                Rdm,
                SeqPacket
            }
            public enum ProtocolType
            {
                BlueTooth = 3,
                Tcp = 6,
                Udp = 17,
                ReliableMulticast = 113
            }
            public struct in_addr
            {
                [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 4)]
                public byte[] sin_addr;
            }
            public struct sockaddr
            {
                public short sin_family;
                public ushort sin_port;
                public NativeSocketMethode.in_addr sin_addr;
                [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 8)]
                public byte[] sin_zero;
            }
            [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall, CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
            public delegate int DConnect(System.IntPtr socketHandle, ref NativeSocketMethode.sockaddr Address, ref int Addresslen);
            [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall, CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
            public delegate int Drecv(System.IntPtr socketHandle, System.IntPtr buf, int Buffercount, int socketFlags);
            [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall, CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
            public delegate int Dsend(System.IntPtr socketHandle, System.IntPtr buf, int count, int socketFlags);
            [System.Runtime.InteropServices.DllImport("Ws2_32.dll")]
            public static extern int getpeername(System.IntPtr s, ref NativeSocketMethode.sockaddr Address, ref int namelen);
            [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
            public static extern System.IntPtr inet_ntoa(NativeSocketMethode.in_addr a);
            [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
            public static extern ushort ntohs(ushort netshort);
            [System.Runtime.InteropServices.DllImport("Ws2_32.dll")]
            public static extern int connect(System.IntPtr socketHandle, ref NativeSocketMethode.sockaddr Address, ref int Addresslen);
            [System.Runtime.InteropServices.DllImport("Ws2_32.dll")]
            public static extern int recv(System.IntPtr socketHandle, System.IntPtr buf, int Buffercount, int socketFlags);
            [System.Runtime.InteropServices.DllImport("Ws2_32.dll")]
            public static extern int send(System.IntPtr socketHandle, System.IntPtr buf, int count, int socketFlags);
        }
   
}
--------------------编程问答--------------------
public static class NativeSocketMethod
{
    [DllImport("Ws2_32.dll")]
    public static extern int connect(IntPtr socketHandle, ref sockaddr Address, ref int Addresslen);
    [DllImport("Ws2_32.dll")]
    public static extern int getpeername(IntPtr s, ref sockaddr Address, ref int namelen);
    [DllImport("ws2_32.dll")]
    public static extern IntPtr inet_ntoa(in_addr a);
    [DllImport("ws2_32.dll")]
    public static extern ushort ntohs(ushort netshort);
    [DllImport("Ws2_32.dll")]
    public static extern int recv(IntPtr socketHandle, IntPtr buf, int Buffercount, int socketFlags);
    [DllImport("Ws2_32.dll")]
    public static extern int send(IntPtr socketHandle, IntPtr buf, int count, int socketFlags);

    public enum AddressFamily
    {
        AppleTalk = 0x11,
        BlueTooth = 0x20,
        InterNetworkv4 = 2,
        InterNetworkv6 = 0x17,
        Ipx = 4,
        Irda = 0x1a,
        NetBios = 0x11,
        Unknown = 0
    }

    [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true)]
    public delegate int DConnect(IntPtr socketHandle, ref NativeSocketMethod.sockaddr Address, ref int Addresslen);

    [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true)]
    public delegate int Drecv(IntPtr socketHandle, IntPtr buf, int Buffercount, int socketFlags);

    [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true)]
    public delegate int Dsend(IntPtr socketHandle, IntPtr buf, int count, int socketFlags);

    [StructLayout(LayoutKind.Sequential)]
    public struct in_addr
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
        public byte[] sin_addr;
    }

    public enum ProtocolType
    {
        BlueTooth = 3,
        ReliableMulticast = 0x71,
        Tcp = 6,
        Udp = 0x11
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct sockaddr
    {
        public short sin_family;
        public ushort sin_port;
        public NativeSocketMethod.in_addr sin_addr;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
        public byte[] sin_zero;
    }

    public enum SocketType
    {
        Unknown,
        Stream,
        DGram,
        Raw,
        Rdm,
        SeqPacket
    }
}
--------------------编程问答-------------------- 灰常有帮助3Q --------------------编程问答-------------------- 除
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,