Jump to content

How to get incoming packet's IP?


E1M1
 Share

Recommended Posts

How do i get ip of sender? when I copied SocketToIP() from tcprecv example then it returned 0.

;;This is the UDP Server
;;Start this first

; Start The UDP Services
;==============================================
UDPStartup()

; Register the cleanup function.
OnAutoItExitRegister("Cleanup")

; Bind to a SOCKET
;==============================================
$socket = UDPBind("127.0.0.1", 65532)
If @error <> 0 Then Exit

While 1
    $data = UDPRecv($socket, 50)
    If $data <> "" Then
        MsgBox(0, "UDP DATA", $data, 1)
    EndIf
    sleep(100)
WEnd

Func Cleanup()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc

edited

Link to comment
Share on other sites

I don't know that you do.

If it can be done, you have to recognize the difference between AutoIt's TCP and UDP socket variables. The UDP socket is an array with the real socket id in $socket[1], so the call to your function would be:

$sIP = SocketToIP($socket[1])

However, UDP is connectionless. You would have to check during the time data was being received on that socket, I think. Unlike TCP, where you can get the IP after accepting the connection even if data is not currently being transferred. Note there is no UDP equivalent of TCPListen() or TCPAccept().

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

How do i get ip of sender? when I copied SocketToIP() from tcprecv example then it returned 0.

;;This is the UDP Server
;;Start this first

; Start The UDP Services
;==============================================
UDPStartup()

; Register the cleanup function.
OnAutoItExitRegister("Cleanup")

; Bind to a SOCKET
;==============================================
$socket = UDPBind("127.0.0.1", 65532)
If @error <> 0 Then Exit

While 1
 $data = UDPRecv($socket, 50)
 If $data <> "" Then
 MsgBox(0, "UDP DATA", $data, 1)
 EndIf
 sleep(100)
WEnd

Func Cleanup()
 UDPCloseSocket($socket)
 UDPShutdown()
EndFunc

You need a fairly recent version of AutoIt, then you set the flag to 2 in UDPRecv. See the help for how the data is returned. Although the sender's ip can be sent in the UDP header it might be set to 0.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Oooh... got me some 'edumacation from Martin! :mellow:

Tested it like this:

While 1
    ; Flag 2 = array
    ; [0] = Data
    ; [1] = Source IP
    ; [2] = Source Port
    $aData = UDPRecv($socket, 1024, 2)
    If @error = 0 And IsArray($aData) And $aData[0] <> "" Then
        MsgBox(0, "UDP DATA Received", "Data = " & $aData[0] & @CRLF & _
        "IP = " & $aData[1] & @CRLF & _
        "Port = " & $aData[2], 3)
    EndIf
    sleep(100)
WEnd

The UDP header doesn't even have a field in it for source IP, so that IP is retrieved from the IPv4 packet header somehow. Pretty cool. Thanks, Martin!

:(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...