Sign in to follow this  
Followers 0
donv

need help to read and write from UDP socket

2 posts in this topic

I apologize if this has been answered elsewhere. I am attempting to write a simple application to find other devices running our software on a network.

This is essentially a network discovery app which will assist the user so they dont have to type the IP address of another machine on the network running our software.

I would prefer to use broadcast UDP for the discovery and response. I would like to write a single app which will normally 'listen' and if it recieves a 'STATUS' request from another machine on the network, then it will respond using the same port/socket with it's machine information i.e. (the ip address, freindly name, service status, etc.).

I have read and re-read the info on UDP sockets and have read example code which is appropriate.

It isn't obvious (from reading the docs) if you can UDPOpen and UDPRecieve from the same open socket.

If anyone could point me to an example or to documentation so i can understand this it would be great.

Any help would really be appreciated.

Thank you.

-don

Share this post


Link to post
Share on other sites



I apologize if this has been answered elsewhere. I am attempting to write a simple application to find other devices running our software on a network.

This is essentially a network discovery app which will assist the user so they dont have to type the IP address of another machine on the network running our software.

I would prefer to use broadcast UDP for the discovery and response. I would like to write a single app which will normally 'listen' and if it recieves a 'STATUS' request from another machine on the network, then it will respond using the same port/socket with it's machine information i.e. (the ip address, freindly name, service status, etc.).

I have read and re-read the info on UDP sockets and have read example code which is appropriate.

It isn't obvious (from reading the docs) if you can UDPOpen and UDPRecieve from the same open socket.

If anyone could point me to an example or to documentation so i can understand this it would be great.

Any help would really be appreciated.

Thank you.

-don

Wel I agree that it isn't obvious from the help but you could always try it and see if it works. That would probably have been the fastest way to find out. It will work though.

I would have expected that searching would find plenty of examples too.

Modified from the help, do this on one PC

;Script1

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

; Open a "SOCKET"
;==============================================

$ThisPCIP = "192.168.1.1";running this script1
$socket2 = UDPBind($ThisPCIP, 65532)
$ThatPCIP = "192.168.1.11";running script2
If @error <> 0 Then 
 ConsoleWrite("error opening $socket2 , @error = " & @error & @CRLF)
 Exit
 EndIf

 
 $socket = UDPOpen($ThatPCIP, 65532)
If @error <> 0 Then 
 ConsoleWrite("error opening $socket" & @CRLF)
 Exit
 EndIf
 
 
$n=0
While 1
    $data = UDPRecv($socket2, 50)
    If $data <> "" Then
        MsgBox(0, "UDP DATA", $data, 1)
    EndIf
    sleep(100)
    $n = $n + 1
    $status = UDPSend($socket, "Message #" & $n)
    If $status = 0 then 
        MsgBox(0, "ERROR", "Error while sending UDP message: " & @error)
        Exit
    EndIf
WEnd

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPCloseSocket($socket2)
    UDPShutdown()
EndFunc

and do this on another

;Script2

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

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

$thatPCIP = "192.168.1.68";running script1
$socket2 = UDPOpen($thatPCIP,65532)
While 1
    $data = UDPRecv($socket, 50)
    If $data <> "" Then
        MsgBox(0, "UDP DATA", $data, 1)
        UDPSend($socket2,"Got number " & $data)
    EndIf
    sleep(100)
WEnd

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

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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0