通过ws2_32.dll发送UDP数据后,如何接收数据呢?
场景:Client(绑定指定端口)-----UDP----->Server
问题:如果使用ws2_32.dll来接收数据,Client如何接收Server响应的数据?哪位大虾帮忙做一个接收的函数。下面的代码是从AutoIT英文网上看到的。
ps:不能使用UDPSend,原因是UDPSend后,本地是随机端口,Server会向随机端口发包,导致脚本无法监听到数据。UDPStartup()
$hWs2_32 = DllOpen("ws2_32.dll")
$hSocket = _WinsockCreateSocket(2, 2, 17)
_WinsockBind($hSocket, "192.168.0.241", 88); The source address and port for the Socket. The IP must be your LAN IP. I've found that 127.0.0.1 does not work.
_WinsockConnect($hSocket, "192.168.0.64", 5060); The destination IP and port.
_WinsockSend($hSocket, "SEND MSG!")
Func _WinsockCreateSocket($Address_Family, $Socket_Type, $Protocol)
$iRet = DllCall($hWs2_32, "int", "socket", "int", $Address_Family, "int", $Socket_Type, "int", $Protocol)
Return $iRet
EndFunc
Func _WinsockBind($hSocket, $IP, $Port)
$hAddr = _SocketAddr($IP, $Port)
$iRet = DllCall($hWs2_32, "int", "bind", "uint", $hSocket, "ptr", DllStructGetPtr($hAddr), "int", DllStructGetSize($hAddr))
Return $iRet
EndFunc
Func _WinsockConnect($hSocket, $IP, $Port)
$hAddr = _SocketAddr($IP, $Port)
$iRet = DllCall($hWs2_32, "int", "connect", "uint", $hSocket, "ptr", DllStructGetPtr($hAddr), "int", DllStructGetSize($hAddr))
EndFunc
Func _WinsockSend($hSocket, $data)
$hBuf = DllStructCreate("byte[" & BinaryLen($data) & "]")
DllStructSetData($hBuf, 1, $data)
$iRet = DllCall($hWs2_32, "int", "send", "uint", $hSocket, "ptr", DllStructGetPtr($hBuf), "int", DllStructGetSize($hBuf), "int", 0)
If IsArray($iRet) Then _ArrayDisplay($iRet)
EndFunc
Func _SocketAddr($IP, $Port, $Address_Family = 2)
$stAddress = DllStructCreate("short; ushort; uint; char")
DllStructSetData($stAddress, 1, $Address_Family)
$iRet = DllCall($hWs2_32, "ushort", "htons", "ushort", $Port)
DllStructSetData($stAddress, 2, $iRet)
$iRet = DllCall($hWs2_32, "uint", "inet_addr", "str", $IP)
DllStructSetData($stAddress, 3, $iRet)
Return $stAddress
EndFunc ;==>_SocketAddr
Func _WSAGetLastError()
$iRet = DllCall($hWs2_32, "int", "WSAGetLastError")
Return $iRet
EndFunc ;==>_WSAGetLastError
页:
[1]