WinSock

    • Offizieller Beitrag

    Heyho!

    Da ich leider mein Problem mit dem multicast datagram nicht gelöst bekommen habe, versuche ich nun anders daran zu gehen. Gibt es eine Möglichkeit direkt auf die WinSock API von M$ zu zugreifen?
    Im englischem Forum gab es hin und wieder mal Code schnipsel die wohl auch funktioniert haben sollten, aber ich bekomme unter Win7 nix zu laufen davon.

    Hier mal das Codefragment:

    Spoiler anzeigen
    [autoit]

    Dim $owinsock = ''
    Dim $sbuffer = ''
    Dim $ssource = ''
    Dim $bclose = False

    [/autoit] [autoit][/autoit] [autoit]

    $owinsock = ObjCreate("OSWINSCK.Winsock")
    ConsoleWrite(IsObj($owinsock) & @CRLF)
    ObjEvent($owinsock, "oWinsock_")
    $owinsock.Connect("129.6.15.28", 13)

    [/autoit] [autoit][/autoit] [autoit]

    Func oWinsock_OnDataArrival($total)
    $owinsock.GetData($sbuffer)
    $ssource &= $sbuffer
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    While Not $bclose
    Sleep(1)
    Wend

    [/autoit] [autoit][/autoit] [autoit]

    Func oWinsock_OnClose()
    $owinsock.CloseWinsock()
    MsgBox(262144, '', $ssource)
    $bclose = True
    $owinsock = 0
    EndFunc

    [/autoit]

    Bekomme immer ein Fehler beim erstellen des Objektes. :( ( ==> Variable must be of type "Object".)
    Ich habe unter Win7 eine MSWINSCK.OCX Datei im System Ordner, die ich auch mit regsvr aktiviert habe, bringt leider nichts :(
    Wäre super wenn mir jemand aushelfen könnte :)

    Gruß
    Spider

    • Offizieller Beitrag

    Hey!

    Danke Progandy! Ich versuche nun irgendwie die Connect funktion mit diesem hier zu verbinden:
    http://publib.boulder.ibm.com/infocenter/ise…x2multicast.htm

    Aber ich schaffe es einfach nicht. Hier ist mal was ich bisher habe, aber er hat schon ein Fehler bei dem socket Aufruf. Diesen Fehler gibt es übrigends auch bei der ürsprünglichen Funktion, die du mir geschickt hast. Er gibt mir immer 4294967295 zurück (0xFFFFFFFF).

    Spoiler anzeigen
    [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _TCPConnect
    ; Description ...: Triess to establishes a TCP-connection in a specified time limit
    ; Syntax.........: _TCPConnect($sIPAddr, $iPort, $iTimeOut = -1)
    ; Parameters ....: $sIpAddr - IP address to connect to (IPv4)
    ; $iPort - Port to use
    ; $iTimeOut - Timeout for connection in milliseconds (default: -1)
    ; |Values < 0: default timeout
    ; |Values 0, Keyword Default: use time from Opt("TCPTimeout")
    ; |Values > 0: timeout in milliseconds
    ; Return values .: Success - Socket to use with TCP-functions
    ; Failure - -1, sets @error
    ; |1 - $sIpAddr incorrect
    ; |2 - could not get port
    ; |3 - could not create socket
    ; |4 - could not connect
    ; |5 - could not get WSAError
    ; |and errors from WSAGetLastError
    ; Author ........: ProgAndy
    ; Modified.......:
    ; Remarks .......:
    ; Related .......: TCPConnect, TCPCloseSocket, TCPSend, TCPRecv
    ; Link ..........:
    ; Example .......:
    ; ===============================================================================================================================
    UDPStartup()
    UDPBind("192.168.0.2",5555)
    ConsoleWrite(@error & @CRLF);kein fehler
    ConsoleWrite("_TCPConnect Return: "&_TCPConnect("192.168.0.2", 5555) & @CRLF);fehler!

    [/autoit] [autoit][/autoit] [autoit]

    Func _TCPConnect($sIPAddr, $iPort, $iTimeOut = -1)

    [/autoit] [autoit][/autoit] [autoit]

    Local $udp = 17

    [/autoit] [autoit][/autoit] [autoit]

    Local $hWs2 = DllOpen("Ws2_32.dll")
    Local $iDllErr, $fError = False, $aRes
    Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 1, "int", $udp);ipv4, sock_dgram, udp
    If @error Then
    $iDllErr = 3
    ElseIf $hSock[0] = 4294967295 Or $hSock[0] = -1 Then
    ConsoleWrite($hSock[0] & @CRLF)
    $fError = True
    Else
    $hSock = $hSock[0]
    $aRes = DllCall($hWs2, "ulong", "inet_addr", "str", $sIPAddr)
    If @error Or $aRes[0] = -1 Or $aRes[0] = 4294967295 Then
    $iDllErr = 1
    Else
    $iPort = DllCall($hWs2, "ushort", "htons", "ushort", $iPort)
    If @error Then
    $iDllErr = 2
    Else
    $iPort = $iPort[0]
    EndIf
    EndIf
    If 0 = $iDllErr Then

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Local $tSockAddr = DllStructCreate("short sin_family;ushort sin_port; ulong sin_addr;char sin_zero[8];")
    DllStructSetData($tSockAddr, 1, $udp)
    DllStructSetData($tSockAddr, 2, $iPort)
    DllStructSetData($tSockAddr, 3, $aRes[0])

    [/autoit] [autoit][/autoit] [autoit]

    If IsKeyword($iTimeOut) Or $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout")

    [/autoit] [autoit][/autoit] [autoit]

    If $iTimeOut > -1 Then DllCall($hWs2, "int", "ioctlsocket", "int", $hSock, "long", 0x8004667e, "uint*", 1)
    ;~ $aRes = DllCall($hWs2, "int", "connect", "int", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr))
    $aRes = DllCall($hWs2, "int", "bind", "int", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr))

    [/autoit] [autoit][/autoit] [autoit]

    Select
    Case @error
    $iDllErr = 4
    Case $aRes[0] <> 0
    $aRes = DllCall($hWs2, "int", "WSAGetLastError")
    If Not @error And $aRes[0] = 10035 Then ContinueCase
    $fError = True
    Case $iTimeOut > -1
    If IsKeyword($iTimeOut) Or $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout")
    Local $t = DllStructCreate("uint;int")
    DllStructSetData($t, 1, 1)
    DllStructSetData($t, 2, $hSock)
    Local $to = DllStructCreate("long;long")
    DllStructSetData($to, 1, Floor($iTimeOut / 1000))
    DllStructSetData($to, 2, Mod($iTimeOut, 1000))
    $aRes = DllCall($hWs2, "int", "select", "int", $hSock, "ptr", DllStructGetPtr($t), "ptr", DllStructGetPtr($t), "ptr", 0, "ptr", DllStructGetPtr($to))
    If Not @error And $aRes[0] = 0 Then
    $aRes = DllCall($hWs2, "int", "WSAGetLastError")
    If Not @error And $aRes[0] = 0 Then
    $iDllErr = 10060
    Else
    $fError = True
    EndIf
    Else
    DllCall($hWs2, "int", "ioctlsocket", "int", $hSock, "long", 0x8004667e, "uint*", 0)
    EndIf
    EndSelect

    [/autoit] [autoit][/autoit] [autoit]

    Local $tIPmreq = DllStructCreate("ulong imr_multiaddr;ulong imr_interface")

    [/autoit] [autoit][/autoit] [autoit]

    DllStructSetData($tIPmreq,1,"225.1.1.1")
    DllStructSetData($tIPmreq,1,@IPAddress1)

    [/autoit] [autoit][/autoit] [autoit]

    DllCall($hWs2,"uint","setsockopt","int",$hSock,"int",0,"int",0,"ptr",DllStructGetPtr($tIPmreq),"int",DllStructGetSize($tIPmreq))

    [/autoit] [autoit][/autoit] [autoit]

    EndIf
    EndIf
    If $iDllErr Then
    TCPCloseSocket($hSock)
    $hSock = -1
    ElseIf $fError Then
    $iDllErr = DllCall($hWs2, "int", "WSAGetLastError")
    If Not @error Then $iDllErr = $iDllErr[0]
    If $iDllErr = 0 Then $iDllErr = 5
    TCPCloseSocket($hSock)
    $hSock = -1
    EndIf
    DllClose($hWs2)
    Return SetError($iDllErr, 0, $hSock)
    EndFunc ;==>_TCPConnect

    [/autoit]

    Wäre super wenn du da mal drüber schauen könntest :)

    Gruß
    Spider

  • Hi, Ich würde an deiner Stelle nicht die komplette Funktion verwenden, sondern nur die DLLCalls die du brauchst rausschneiden. Dann wird die Fehlersuche einfacher, da hier viel COde abei ist, den du gar nicht brauchst.

  • Wenn du SOCK_DGRAM willst, muss der zweite Wert 2 sein:

    [autoit]

    Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 2, "int", 17);ipv4, sock_dgram, udp

    [/autoit]
    • Offizieller Beitrag

    Jep, ich weiß, sorry, habe es nochmal mit allen Möglichkeiten getestet und es dann mit falschen Werten hier rein gepostet.
    Problem ist eben, das er über genau diesen DLLCall nicht herrüber kommt. Er gibt mir immer ein falschen socket raus!
    Was gibt er denn bei dir aus, wenn du den Befehl ausführst? Und bei welchem OS?

    Gruß
    Spider

  • Ich hab hier Win7 x64 mit AutoIt x64 und x86. Es gibt jedes Mal ein Socket (UDPStartup oder TCPStartup nicht vergessen)

    [autoit]

    #AutoIt3Wrapper_UseX64=n
    TCPStartup()
    Local $hWs2 = DllOpen("Ws2_32.dll")

    [/autoit][autoit][/autoit][autoit]

    Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 2, "int", 17);ipv4, sock_dgram, udp
    $hSock = $hSock[0]
    ConsoleWrite("Socket: " & Hex($hSock) & @CRLF)

    [/autoit][autoit][/autoit][autoit]

    TCPCloseSocket($hSock)
    ConsoleWrite("Close socket error: " & @error & @CRLF)
    TCPShutdown()

    [/autoit]
    • Offizieller Beitrag

    Heyho,

    Stimmt. Ich habe nochmal etwas weiter geforscht und habe nun aus der TCP udf event driven (die ich btw auch schon seit Jahren nutze ;) ) die ___A... funktionen extrahiert, nun klappt zumindest das.
    Leider stehe ich nun vor nem neuen Problem, natürlich.. Ich bekomme es nicht hin, dass die Dll mich in die datagram gruppe hinzufügt., das Problem liegt bei dem DLL Aufruf in Zeile 67. Er gibt mir immer als Fehler 10042 zurück (WSAENOPROTOOPT: Bad protocol option.)

    Es kann sein, dass es an den falschen globalen Variablen liegt, ich kann mir eigentlich kaum vorstellen, dass IPPROTO_IP = 0 ist. Aber im Internet findet man nur wenig darüber, irgendwo habe ich aber "#define IPPROTO_IP 0" gelesen.. Gibt es ne Möglichkeit diese Variablen irgendwo her alle zu bekommen? Und was ist die INADDR_ANY IP? Ist das 255.255.255.255?

    Hier mal der Code, wäre super wenn sich das nochmal jemand angucken kann, progandy, dir aber schonmal vielen Dank :)

    Spoiler anzeigen
    [autoit]

    Dim $SOL_SOCKET = 0xffff
    Dim $SO_REUSEADDR = 4
    Dim $SOCK_DGRAM = 2
    Dim $AF_INET = 2
    Dim $IPPROTO_UDP = 17
    Dim $IPPROTO_IP = 0
    Dim $IP_ADD_MEMBERSHIP = 5

    [/autoit] [autoit][/autoit] [autoit]

    Dim $hWs2_32 = DllOpen("Ws2_32.dll")

    [/autoit] [autoit][/autoit] [autoit]

    UDPStartup()

    [/autoit] [autoit][/autoit] [autoit]

    $sTCP = _ConnectGroup("225.1.1.1", 5555)
    ConsoleWrite("sTCP: " & $sTCP & " - error: " & @error & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    Func _ConnectGroup($sIPAddr, $iPort, $iTimeOut = -1)
    Local $INADDR_ANY = "255.255.255.255";Ist das wahr?

    [/autoit] [autoit][/autoit] [autoit]

    Local $iDllErr, $fError = False, $aRes
    Local $hSock = ___ASocket($AF_INET, $SOCK_DGRAM, $IPPROTO_UDP)
    If @error Then
    $iDllErr = 3
    ElseIf $hSock = 4294967295 Or $hSock = -1 Then
    $fError = True
    Else
    ConsoleWrite("Socket: "&$hSock & @CRLF)
    If 0 = $iDllErr Then

    [/autoit] [autoit][/autoit] [autoit]

    Local $tIPmreq = DllStructCreate("ulong imr_multiaddr;ulong imr_interface")
    ;~ DllStructSetData($tIPmreq, 1, _inet_addr("225.1.1.1",$hWs2_32))
    ;~ DllStructSetData($tIPmreq, 2, @IPAddress1)
    $hDll = DllCall($hWs2_32, "uint", "setsockopt", "int", $hSock, "int", $SOL_SOCKET, "int", $SO_REUSEADDR, "ptr", DllStructGetPtr($tIPmreq), "int", DllStructGetSize($tIPmreq))
    ConsoleWrite($hDll[0] & @CRLF)
    $tSockAddr = ___SockAddr("127.0.0.1", $iPort, $AF_INET)
    $aRes = DllCall($hWs2_32, "int", "bind", "uint", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr))
    Select
    Case @error
    $iDllErr = 4
    Case $aRes[0] <> 0
    ConsoleWrite("!" & $aRes[0] & @CRLF)
    $aRes = DllCall($hWs2_32, "int", "WSAGetLastError")
    If Not @error And $aRes[0] = 10035 Then ContinueCase
    $fError = True
    Case $iTimeOut > -1
    ConsoleWrite("52353252623" & @CRLF)
    If IsKeyword($iTimeOut) Or $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout")
    Local $t = DllStructCreate("uint;int")
    DllStructSetData($t, 1, 1)
    DllStructSetData($t, 2, $hSock)
    Local $to = DllStructCreate("long;long")
    DllStructSetData($to, 1, Floor($iTimeOut / 1000))
    DllStructSetData($to, 2, Mod($iTimeOut, 1000))
    $aRes = DllCall($hWs2_32, "int", "select", "int", $hSock, "ptr", DllStructGetPtr($t), "ptr", DllStructGetPtr($t), "ptr", 0, "ptr", DllStructGetPtr($to))
    If Not @error And $aRes[0] = 0 Then
    $aRes = DllCall($hWs2_32, "int", "WSAGetLastError")
    If Not @error And $aRes[0] = 0 Then
    $iDllErr = 10060
    Else
    $fError = True
    EndIf
    Else
    DllCall($hWs2_32, "int", "ioctlsocket", "int", $hSock, "long", 0x8004667e, "uint*", 0)
    EndIf
    EndSelect
    DllStructSetData($tIPmreq, 1, _inet_addr("225.1.1.1",$hWs2_32))
    DllStructSetData($tIPmreq, 2, _inet_addr("9.5.1.1",$hWs2_32))
    $hDll = DllCall($hWs2_32, "int", "setsockopt", "int", $hSock, "int", $IPPROTO_IP, "int", $IP_ADD_MEMBERSHIP, "ptr", DllStructGetPtr($tIPmreq), "int", DllStructGetSize($tIPmreq))

    [/autoit] [autoit][/autoit] [autoit]

    ConsoleWrite("setsockopt:" & @CRLF)
    ConsoleWrite(Hex($hDll[0]) & @CRLF)
    ConsoleWrite(___WSAGetLastError() & @CRLF)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    EndIf
    EndIf
    If $iDllErr Then
    ConsoleWrite("!iDllErr: " & $iDllErr & @CRLF)
    TCPCloseSocket($hSock)
    $hSock = -1
    ElseIf $fError Then
    ConsoleWrite("fError: " & $fError & @CRLF)
    $iDllErr = DllCall($hWs2_32, "int", "WSAGetLastError")
    If Not @error Then $iDllErr = $iDllErr[0]
    If $iDllErr = 0 Then $iDllErr = 5

    [/autoit] [autoit][/autoit] [autoit]

    $hSock = -1
    EndIf
    TCPCloseSocket($hSock)
    DllClose($hWs2_32)
    Return SetError($iDllErr, 0, $hSock)
    EndFunc ;==>_ConnectGroup

    [/autoit] [autoit][/autoit] [autoit]

    Func _inet_addr($sIP,$hDll = "")
    $aRet = DllCall($hDll, "uint", "inet_addr", "str", $sIP)
    Return SetError(@error,0,$aRet[0])
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func ___ASocket($iAddressFamily = 2, $iType = 1, $iProtocol = 6)
    Local $hSocket = DllCall($hWs2_32, "uint", "socket", "int", $iAddressFamily, "int", $iType, "int", $iProtocol)
    If @error Then
    SetError(1, @error)
    Return -1
    EndIf
    If $hSocket[0] = -1 Then
    SetError(2, ___WSAGetLastError())
    Return -1
    EndIf
    Return $hSocket[0]
    EndFunc ;==>___ASocket

    [/autoit] [autoit][/autoit] [autoit]

    ; A wrapper function to ease all the pain in creating and filling the sockaddr struct
    Func ___SockAddr($sIP, $iPort, $iAddressFamily = 2)
    Local $iRet
    Local $stAddress

    [/autoit] [autoit][/autoit] [autoit]

    If $hWs2_32 = -1 Then $hWs2_32 = DllOpen("Ws2_32.dll")

    [/autoit] [autoit][/autoit] [autoit]

    $stAddress = DllStructCreate("short; ushort; uint; char[8]")
    If @error Then
    SetError(1, @error)
    Return False
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    DllStructSetData($stAddress, 1, $iAddressFamily)
    $iRet = DllCall($hWs2_32, "ushort", "htons", "ushort", $iPort)
    DllStructSetData($stAddress, 2, $iRet[0])
    $iRet = DllCall($hWs2_32, "uint", "inet_addr", "str", $sIP)
    If $iRet[0] = 0xffffffff Then; INADDR_NONE
    $stAddress = 0; Deallocate
    SetError(2, ___WSAGetLastError())
    Return False
    EndIf
    DllStructSetData($stAddress, 3, $iRet[0])

    [/autoit] [autoit][/autoit] [autoit]

    Return $stAddress
    EndFunc ;==>___SockAddr

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func ___WSAGetLastError()

    [/autoit] [autoit][/autoit] [autoit]

    Local $iRet = DllCall($hWs2_32, "int", "WSAGetLastError")
    If @error Then
    ;ConsoleWrite("+> _WSAGetLastError(): WSAGetLastError() failed. Script line number: " & @ScriptLineNumber & @CRLF)
    SetExtended(1)
    Return 0
    EndIf
    Return $iRet[0]
    EndFunc ;==>___WSAGetLastError

    [/autoit]

    Gruß
    Spider

  • Es kann sein, dass es an den falschen globalen Variablen liegt, ich kann mir eigentlich kaum vorstellen, dass IPPROTO_IP = 0 ist. Aber im Internet findet man nur wenig darüber, irgendwo habe ich aber "#define IPPROTO_IP 0" gelesen.. Gibt es ne Möglichkeit diese Variablen irgendwo her alle zu bekommen? Und was ist die INADDR_ANY IP? Ist das 255.255.255.255?


    Hallo,

    ich habe ehrlich gesagt keine Ahnung vom Thema, IPPROTO_IP ist aber in der Tat 0. INADDR_ANY ist allerdings "0.0.0.0", "255.255.255.255" ist INADDR_BROADCAST. Der für mich einfachste Weg, an die Definitionen zu kommen, ist das Herunterladen von MS Visual C++ 2010 Express. Bis auf eine Registrierung bei MS kostet das nichts, und im enthaltenen SDK (unter "Microsoft SDKs" im Programme-Ordner abgelegt) finden sich alle Windows Headerdateien, die bei den Beschreibungen im MSDN fast immer angegeben sind. In diesem Fall verteilen sich die Definitionen auf die WinSock.h und die WinSock2.h.

    • Offizieller Beitrag

    Hallo,

    Ah, alles klar, danke :)
    Ich habe es jetzt tatsächlich gelöst bekommen, IP_PROTO ist 0 aber ADD_MEMBERSHIP ist 12, nicht 5! Endlich funktioniert es komplett, falls es irgendwann mal aus irgendeinem Grund jemand brauchen sollte:

    Spoiler anzeigen
    [autoit]

    ;======================
    ;Multicast Datagram
    ;By Max Reble
    ;http://www.maxreble.de
    ;======================

    [/autoit] [autoit][/autoit] [autoit]

    Dim $SOL_SOCKET = 0xffff
    Dim $SO_REUSEADDR = 4
    Dim $SOCK_DGRAM = 2
    Dim $AF_INET = 2
    Dim $IPPROTO_UDP = 17
    Dim $IPPROTO_IP = 0
    Dim $IP_ADD_MEMBERSHIP = 12
    Dim $INADDR_NONE = 0xffffffff
    Dim $INADDR_ANY = "0.0.0.0"

    [/autoit] [autoit][/autoit] [autoit]

    Global $hWs2_32 = DllOpen("Ws2_32.dll")

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ConsoleWrite(_GetIPFromDevice() & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetIPFromDevice()
    Local $aRecv, $sZero
    UDPStartup()

    [/autoit] [autoit][/autoit] [autoit]

    Local $bSearchGroup = "Search_Group:0"
    Local $bPopServer = "Pop_Server:"&@IPAddress1
    Do
    $sZero &= Chr(0)
    Until StringLen($sZero) = 64
    $bSearchGroup = Binary($bSearchGroup & StringTrimLeft($sZero,StringLen($bSearchGroup)))
    $bPopServer = Binary($bPopServer & StringTrimLeft($sZero,StringLen($bPopServer)))

    [/autoit] [autoit][/autoit] [autoit]

    Local $aUDP = _ConnectGroup("225.1.1.1", 5555)
    If @error Then Return SetError(1,@error,0*UDPShutdown())

    [/autoit] [autoit][/autoit] [autoit]

    Local $hUPDopen = UDPOpen("225.1.1.1", 5555)
    If @error Then Return SetError(2,@error,0*UDPShutdown())

    [/autoit] [autoit][/autoit] [autoit]

    If UDPSend($hUPDopen, $bSearchGroup) <> BinaryLen($bSearchGroup) Then Return SetError(3,0,0*UDPShutdown())
    If UDPSend($hUPDopen, $bPopServer) <> BinaryLen($bPopServer) Then Return SetError(4,0,0*UDPShutdown())
    UDPCloseSocket($hUPDopen)

    [/autoit] [autoit][/autoit] [autoit]

    Local $hTi = TimerInit()
    While TimerDiff($hTi) < 30000
    $aRecv = UDPRecv($aUDP, 1024,2)
    If @error Then Return SetError(5,@error,0*UDPShutdown())
    If IsArray($aRecv) Then
    ConsoleWrite($aRecv[0] & @CRLF)
    If StringInStr($aRecv[0],"Group_Device") Then ExitLoop
    EndIf
    Sleep(50)
    WEnd
    UDPCloseSocket($aUDP)
    UDPShutdown()
    If Not IsArray($aRecv) Then Return SetError(6,0,0)
    Return $aRecv[1]
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _ConnectGroup($sIPAddr, $iPort)
    Local $iDllErr, $fError = False, $aRes, $iRet, $iLastEr
    Local $tIPmreq, $tSockAddr
    Local $hSock = _WinSock_Socket($AF_INET, $SOCK_DGRAM, $IPPROTO_UDP)
    If @error Then
    $iDllErr = 3
    ElseIf $hSock = $INADDR_NONE Or $hSock = -1 Then
    $fError = True
    Else
    If $iDllErr = 0 Then
    $tIPmreq = _WinSock_Struct_IPmreq()
    If _WinSock_SetSockOpt($hSock,$SOL_SOCKET,$SO_REUSEADDR,$tIPmreq) <> 0 Then
    $fError = True
    Else
    Local $tSockAddr = _WinSock_Struct_SockAddr($INADDR_ANY, $iPort, $AF_INET)
    $iRet = _WinSock_Bind($hSock,$tSockAddr)
    Select
    Case @error
    $iDllErr = 4
    Case $iRet <> 0
    $iLastEr = _WSAGetLastError()
    If $iLastEr = 10035 Then ContinueCase
    $fError = True
    case Else
    $tIPmreq = _WinSock_Struct_IPmreq($sIPAddr,$INADDR_ANY,$tIPmreq)
    If _WinSock_SetSockOpt($hSock,$IPPROTO_IP,$IP_ADD_MEMBERSHIP,$tIPmreq) <> 0 Then $fError = False
    EndSelect
    EndIf
    EndIf
    EndIf
    If $iDllErr Then
    UDPCloseSocket($hSock)
    $hSock = -1
    ElseIf $fError Then
    $iDllErr = _WSAGetLastError()
    If $iDllErr = 0 Then $iDllErr = 5
    $hSock = -1
    Else
    Local $udparr[4] = [45454545, $hSock, $INADDR_ANY, 5555]
    EndIf
    Return SetError($iDllErr, 0, $udparr)
    EndFunc ;==>_ConnectGroup

    [/autoit] [autoit][/autoit] [autoit]

    Func _WinSock_InetAddr($sIP)
    $aRet = DllCall($hWs2_32, "uint", "inet_addr", "str", $sIP)
    If @error Then Return SetError(@error,0,-1)
    Return $aRet[0]
    EndFunc ;==>_inet_addr

    [/autoit] [autoit][/autoit] [autoit]

    Func _WinSock_SetSockOpt($hSock,$iLevel,$iOptName,$tIPmreq)
    $aRes = DllCall($hWs2_32, "int", "setsockopt", "int", $hSock, "int", $iLevel, "int", $iOptName, "ptr", DllStructGetPtr($tIPmreq), "int", DllStructGetSize($tIPmreq))
    If @error Or $aRes[0] <> 0 Then Return SetError(1,0,-1)
    Return $aRes[0]
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _WinSock_htons($iHost)
    $aRet = DllCall($hWs2_32, "ushort", "htons", "ushort", $iHost)
    If @error Then Return SetError(@error,0,-1)
    Return $aRet[0]
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _WinSock_Struct_IPmreq($sImr_multiaddr = "",$sImr_interface = "",$tIPmreq = -1);IP,IP
    If $tIPmreq = -1 Then $tIPmreq = DllStructCreate("ulong imr_multiaddr;ulong imr_interface")
    If @error Then Return SetError(1,@error,0)
    $sImr_multiaddr = _WinSock_InetAddr($sImr_multiaddr)
    If @error Or $sImr_multiaddr = $INADDR_NONE Then Return SetError(2,_WSAGetLastError(),False)
    DllStructSetData($tIPmreq,1,$sImr_multiaddr)
    $sImr_interface = _WinSock_InetAddr($sImr_interface)
    If @error Or $sImr_interface = $INADDR_NONE Then Return SetError(3,_WSAGetLastError(),False)
    DllStructSetData($tIPmreq,2,$sImr_interface)
    Return $tIPmreq
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _WinSock_Struct_SockAddr($sIP, $iPort, $iAddressFamily = 2)
    Local $iRet, $tSockAddr
    $tSockAddr = DllStructCreate("short; ushort; uint; char[8]")
    If @error Then Return SetError(1, @error,0)

    [/autoit] [autoit][/autoit] [autoit]

    DllStructSetData($tSockAddr, 1, $iAddressFamily)

    [/autoit] [autoit][/autoit] [autoit]

    $iPort = _WinSock_htons($iPort)
    If @error Then Return SetError(2,@error,0)
    DllStructSetData($tSockAddr, 2, $iPort)

    [/autoit] [autoit][/autoit] [autoit]

    $sIP = _WinSock_InetAddr($sIP)
    If $sIP = $INADDR_NONE Then Return SetError(3, _WSAGetLastError(),False)
    DllStructSetData($tSockAddr, 3, $sIP)

    [/autoit] [autoit][/autoit] [autoit]

    Return $tSockAddr
    EndFunc ;==>___SockAddr

    [/autoit] [autoit][/autoit] [autoit]

    Func _WinSock_Bind($hSock,$tSockAddr)
    Local $aRes = DllCall($hWs2_32, "int", "bind", "uint", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr))
    If @error Then Return SetError(1,0,-1)
    Return $aRes[0]
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _WinSock_Socket($iAddressFamily = 2, $iType = 1, $iProtocol = 6)
    Local $hSocket = DllCall($hWs2_32, "uint", "socket", "int", $iAddressFamily, "int", $iType, "int", $iProtocol)
    If @error Then Return SetError(1, @error,-1)
    If $hSocket[0] = -1 Then Return SetError(2, _WSAGetLastError(),-1)
    Return $hSocket[0]
    EndFunc ;==>___ASocket

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _WSAGetLastError()
    Local $iRet = DllCall($hWs2_32, "int", "WSAGetLastError")
    If @error Then Return 0
    Return $iRet[0]
    EndFunc ;==>___WSAGetLastError

    [/autoit]

    Gruß
    Spider