这是我的代码,我没有成功:
Unmanaged.WsaBuf buffer =
Unmanaged.WsaBuf.Create("Blah Blah");
IntPtr bufferPointer = Marshal.AllocHGlobal(Marshal.SizeOf(buffer));
Marshal.StructureToPtr(buffer, bufferPointer, true);
IntPtr o;
SocketError error = Unmanaged.WSA_Send(
socket,
bufferPointer,
1,
out o,
SocketFlags.None,
IntPtr.Zero,
IntPtr.Zero);
这是方法声明:
[DllImport("Ws2_32.dll", SetLastError = true, EntryPoint = "WSASend")]
public static extern SocketError WSA_Send(IntPtr socket, IntPtr buffer, int len, out IntPtr numberOfBytesSent, SocketFlags flags, IntPtr overlapped, IntPtr completionRoutine);
这是我的WSABUF声明:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WsaBuf
{
public ulong Len;
public IntPtr Buf;
public static WsaBuf Create(string str)
{
byte[] connectBuf = Encoding.ASCII.GetBytes(str);
GCHandle pinnedArray = GCHandle.Alloc(connectBuf, GCHandleType.Pinned);
return new WsaBuf { Buf = pinnedArray.AddrOfPinnedObject(), Len = (ulong)connectBuf.Length };
}
public void Free()
{
if (!Buf.Equals(IntPtr.Zero))
{
GCHandle.FromIntPtr(Buf).Free();
}
}
}
我在另一边没有收到任何东西。有什么想法吗?
答案 0 :(得分:1)
u_long
是unsigned long
,它映射到uint
,因为它是Microsoft Visual C编译器上的4字节数量。 Len
应为uint
。可能WSASend
只查看前4个字节,发现它们是0
,这是一个零长度的缓冲区。
GCHandle.FromIntPtr
。查看文档以了解它的真正功能。这个问题无关紧要,所以我不会详细说明。