gpt4 book ai didi

c# - 如何在 C# 中调用和使用 WSAAsyncSelect()?

转载 作者:行者123 更新时间:2023-11-30 21:25:24 24 4
gpt4 key购买 nike

我在设置赏金时编辑了我的问题。我想从 WinAPI 调用/DllImport WSAAsyncSelect() 并像在 Delphi/C++ 中一样使用它

例如-德尔福

//Async CallBack handler Declaration
procedure MessageHandler(var Msg:Tmessage);Message WM_WINSOCK_ASYNC_MSG;

//Where i setup the Async
dwError := WSAAsyncSelect(Sock, form1.handle, WM_WINSOCK_ASYNC_MSG, FD_CLOSE or FD_READ);

//Async Callback Handler
procedure Tform1.MessageHandler(var Msg:Tmessage);
begin
case WSAGetSelectEvent(MSG.LParam) of //LParam is FD_READ/FR_CLOSE/FD_WRITE
FD_READ: OnSocketRead(MSG.WParam); //WPARAM is the Socket itself.
FD_CLOSE: OnSocketClose(MSG.WParam);
end;
end;

提前致谢!

最佳答案

我成功了!终于!!!

WSAAsyncSelect() 结构

    [DllImport("wsock32.dll")]
public static extern int WSAAsyncSelect(
int socket,
int hWnd,
int wMsg,
int lEvent
);

WS2 类

public class WS2
{
public static Socket sock;
public static byte[] data = new byte[8096];
public static int server = 0;
public static bool forced = true;

public static void Close()
{
//Extern.closesocket(sock.Handle.ToInt32());
//Extern.WSACleanup();

sock.Shutdown(SocketShutdown.Both);
sock.Close();

if (forced)
{
Connect();
}
}
public static void ConnectTo(string ip,int port)
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ip, port);

int handle = 0;
var form1 = Form.ActiveForm as FormMain;
if (form1 != null)
handle = form1.GetHandle;
if (handle == 0)
{
FormMain.PerformActionOnMainForm(form => form.memo.Text += "An error occured: Error code WS_01_ASYNC_HANDLE");
return;
}
Extern.WSAAsyncSelect(sock.Handle.ToInt32(), handle, Values.MESSAGE_ASYNC, Values.FD_READ | Values.FD_CLOSE);
}

public static void Connect()
{
//Get IP && port
string ip = GetIPFromHost("gwgt1.joymax.com");
if (ip == "")
{
ip = GetIPFromHost("gwgt2.joymax.com");
if (ip == "")
{
}
server +=2;
}
else
server +=1;

int port = 15779;

//
ConnectTo(ip, port);
}

public static void Receive()
{
int size = sock.Receive(data);
if (size == 0)
{
FormMain.PerformActionOnMainForm(form => form.memo.Text += "An error occured: Error Code WS_02_RECV_BEGN");
}

Main.Handle(data, size);
}

public static string GetIPFromHost(string HostName)
{
IPHostEntry ip;
try
{
ip = Dns.GetHostEntry(HostName);
}
catch (Exception)
{
return "";
}
return ip.AddressList[0].ToString();
}
}

Form 类中的 WndProc。

    protected override void WndProc(ref Message m)
{
if (m.Msg == Values.MESSAGE_ASYNC)
{

switch (m.LParam.ToInt32())
{
case Values.FD_READ:
WS2.Receive();
break;
case Values.FD_WRITE: break;
case Values.FD_CLOSE:
WS2.Close();
break;
default: break;
}

}
else
{
base.WndProc(ref m);
}
}

获取句柄:

    public int GetHandle
{
get
{
if (this.InvokeRequired)
{
return (int)this.Invoke((GetHandleDelegate)delegate
{
return this.Handle.ToInt32();
});
}
return this.Handle.ToInt32();
}
}
private delegate int GetHandleDelegate();

关于c# - 如何在 C# 中调用和使用 WSAAsyncSelect()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/686355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com