Jump to content

UDP multicast


nend
 Share

Recommended Posts

I made a UDP broadcast between a client and server.
If the server and client are on the same LAN this routine return the IP address from the server without knowing the name or ipaddress from the server.

I'm a newbie on programming TCP/UDP and I think there are better ways to do this so I want to know what you think of this.

Server

#include <Array.au3>

Global $server_name = "TRS"
Global $send_port = 6666
Global $recieve_port = 7777

UDPStartup()

Global $_Socket_Send_Local_IP = UDPBind(@IPAddress1, $send_port)

While 1
    _Loop_Send_Local_IP($server_name)
WEnd

Func _Loop_Send_Local_IP($server_name)
    $data = UDPRecv($_Socket_Send_Local_IP, 50, $UDP_DATA_ARRAY)
    If IsArray($data) Then
        If $data[0] <> "" Then
            If $data[0] = $server_name Then
                $socket_send = UDPOpen($data[1], $recieve_port)
                $status = UDPSend($socket_send, $server_name)
                UDPCloseSocket($socket_send)
            EndIf
        EndIf
    EndIf
EndFunc

 UDPShutdown()
 Exit

 

Client

#include <Array.au3>

Global $server_name = "TRS"
Global $send_port = 6666
Global $recieve_port = 7777

UDPStartup()

$sIPADDRESS = _Get_client_ip($server_name)
ConsoleWrite("Ip address server is " & $sIPADDRESS & @CRLF)

Func _Get_client_ip($sendstring)
    Local $data, $count, $count_total
    UDPStartup()

    Local $socket_send = UDPOpen("255.255.255.255", $send_port, 1); Multicast
    Local $socket_recieve = UDPBind(@IPAddress1, $recieve_port)

    If UDPSend($socket_send, $sendstring) = 0 then
        SetError(1);
    Else
        While 1
            If $count_total > 20 Then
                SetError(2)
                ExitLoop
            EndIf
            If $count > 5 Then
                If UDPSend($socket_send, $sendstring) = 0 then
                    SetError(1);
                    ExitLoop
                EndIf
                $count = 0
            EndIf
            $data = UDPRecv($socket_recieve, 50, $UDP_DATA_ARRAY)
            If IsArray($data) Then
                If $data[0] = $server_name Then
                    ExitLoop
                EndIf
            EndIf
            $count = $count + 1
            $count_total = $count_total + 1
            Sleep(100)
        WEnd
    EndIf

    UDPCloseSocket($socket_send)
    UDPCloseSocket($socket_recieve)
    UDPShutdown()

    Return $data[1]
EndFunc

UDPShutdown()
Exit

 

Edited by nend
Link to comment
Share on other sites

  • 6 months later...

Hello, your code works great!

But I have a problem, is it possible to run the client code without the server but wont return an error?

this is what it returns when I dont use the server.

Quote

Return $data[1]
Return $data^ ERROR

I hope I am not confusing.

Thank you so much!

Link to comment
Share on other sites

Hello, your code works great!

But I have a problem, is it possible to run the client code without the server but wont return an error?

this is what it returns when I dont use the server.

Quote

Return $data[1]
Return $data^ ERROR

I hope I am not confusing.

Thank you so much!

Link to comment
Share on other sites

@Nyl- Just a small fix for the client.
 

#include <Array.au3>

Global $server_name = "TRS"
Global $send_port = 6666
Global $recieve_port = 7777

UDPStartup()

$sIPADDRESS = _Get_client_ip($server_name)
If @error Then
    ConsoleWrite("Error" & @CRLF)
Else
    ConsoleWrite("Ip address server is " & $sIPADDRESS & @CRLF)
EndIf

Func _Get_client_ip($sendstring)
    Local $data, $count, $count_total
    UDPStartup()

    Local $socket_send = UDPOpen("255.255.255.255", $send_port, 1); Multicast
    Local $socket_recieve = UDPBind(@IPAddress1, $recieve_port)

    If UDPSend($socket_send, $sendstring) = 0 then
        SetError(1)
    Else
        While 1
            If $count_total > 20 Then
                SetError(2)
                ExitLoop
            EndIf
            If $count > 5 Then
                If UDPSend($socket_send, $sendstring) = 0 then
                    SetError(1);
                    ExitLoop
                EndIf
                $count = 0
            EndIf
            $data = UDPRecv($socket_recieve, 50, $UDP_DATA_ARRAY)
            If IsArray($data) Then
                If $data[0] = $server_name Then
                    ExitLoop
                EndIf
            EndIf
            $count = $count + 1
            $count_total = $count_total + 1
            Sleep(100)
        WEnd
    EndIf

    UDPCloseSocket($socket_send)
    UDPCloseSocket($socket_recieve)
    UDPShutdown()

    If UBound($data) = 0 Then
        Return SetError(2)
    Else
        Return $data[1]
    EndIf
EndFunc

UDPShutdown()
Exit

 

Edited by nend
Link to comment
Share on other sites

13 hours ago, nend said:

@Nyl- Just a small fix for the client.
 

#include <Array.au3>

Global $server_name = "TRS"
Global $send_port = 6666
Global $recieve_port = 7777

UDPStartup()

$sIPADDRESS = _Get_client_ip($server_name)
If @error Then
    ConsoleWrite("Error" & @CRLF)
Else
    ConsoleWrite("Ip address server is " & $sIPADDRESS & @CRLF)
EndIf

Func _Get_client_ip($sendstring)
    Local $data, $count, $count_total
    UDPStartup()

    Local $socket_send = UDPOpen("255.255.255.255", $send_port, 1); Multicast
    Local $socket_recieve = UDPBind(@IPAddress1, $recieve_port)

    If UDPSend($socket_send, $sendstring) = 0 then
        SetError(1)
    Else
        While 1
            If $count_total > 20 Then
                SetError(2)
                ExitLoop
            EndIf
            If $count > 5 Then
                If UDPSend($socket_send, $sendstring) = 0 then
                    SetError(1);
                    ExitLoop
                EndIf
                $count = 0
            EndIf
            $data = UDPRecv($socket_recieve, 50, $UDP_DATA_ARRAY)
            If IsArray($data) Then
                If $data[0] = $server_name Then
                    ExitLoop
                EndIf
            EndIf
            $count = $count + 1
            $count_total = $count_total + 1
            Sleep(100)
        WEnd
    EndIf

    UDPCloseSocket($socket_send)
    UDPCloseSocket($socket_recieve)
    UDPShutdown()

    If UBound($data) = 0 Then
        Return SetError(2)
    Else
        Return $data[1]
    EndIf
EndFunc

UDPShutdown()
Exit

 

You are a blessing sir. I will try this later and reply with the results. Thank you again.

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...