Jump to content

tcp broadcast address listen


Recommended Posts

I was looking at the following thread,

http://www.autoitscript.com/forum/index.ph...opic=29772&

and noticed that the connection was made to a broacast address.

I am trying to make a program that sends a few packets of info to the broadcast and those packets are picked up by all computers on the network that are currently listening. i tried changing the tcp chat example in the help file to the broadcast address on both the client and the server and i just get some error. is there an easy way to do communication using the broadcast?

Link to comment
Share on other sites

I was looking at the following thread,

http://www.autoitscript.com/forum/index.ph...opic=29772&

and noticed that the connection was made to a broacast address.

I am trying to make a program that sends a few packets of info to the broadcast and those packets are picked up by all computers on the network that are currently listening. i tried changing the tcp chat example in the help file to the broadcast address on both the client and the server and i just get some error. is there an easy way to do communication using the broadcast?

I think that to broadcast you need to use UDP. UDP sends a datagram (like sending a letter) which can be addressed to one ip or may (like junkmail). TCP establishes a connection between two ip address, like making a phone call. You cannot broadcast beyond a local area network because routers block broadcast packets. A chat program should work fine using UDP on a local network. If you want to use TCP for some reason then you could broadcat a UDP message, examine the replies and decide which one to communicate with using TCP.

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

I think that to broadcast you need to use UDP. UDP sends a datagram (like sending a letter) which can be addressed to one ip or may (like junkmail). TCP establishes a connection between two ip address, like making a phone call. You cannot broadcast beyond a local area network because routers block broadcast packets. A chat program should work fine using UDP on a local network. If you want to use TCP for some reason then you could broadcat a UDP message, examine the replies and decide which one to communicate with using TCP.

ok i will try to change the chat program to udp for now and see if i can get this working, thanks martin!

Link to comment
Share on other sites

I still cant get this working

This is the broadcast code

$IPAddress = "192.168.1.255"; This is the broadcast address !


UDPStartUp()

$connection = UDPOpen($IPAddress, 65532)
$res = UDPSend($connection,"superdata")
MsgBox(0, "", $res)

UDPCloseSocket($connection)
UDPShutdown()

This is the recieve code

UDPStartup()
$socket = UDPBind("192.168.1.255", 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 OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc
Link to comment
Share on other sites

I still cant get this working

This is the broadcast code

$IPAddress = "192.168.1.255"; This is the broadcast address !
UDPStartUp()

$connection = UDPOpen($IPAddress, 65532)
$res = UDPSend($connection,"superdata")
MsgBox(0, "", $res)

UDPCloseSocket($connection)
UDPShutdown()

This is the recieve code

UDPStartup()
$socket = UDPBind("192.168.1.255", 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 OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc

Have you tested the server to see if it has the port open - when I ran your code on the server it closed, but when I changed the IP to $socket = UDPBind("127.0.0.1", 65532) it stayed open and I checked for the port and it was opened.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Have you tested the server to see if it has the port open - when I ran your code on the server it closed, but when I changed the IP to $socket = UDPBind("127.0.0.1", 65532) it stayed open and I checked for the port and it was opened.

The only issue that i see is that if i run the udpbind to 127.0.0.1 then i will not recieve broadcast packets but only local ones. how can i make the receive work for all braodcast packets on the network? in the end i will have the recieve (server) running on 10 computers and the broadcaster (the client) running on only one system.

Link to comment
Share on other sites

Is your LAN range 192.168.1.0 with an IP mask of 255.255.0.0?

Maybe you are using the wrong IP address.

What is the LAN IP address of one of your computers and what is the IP mask?

#)

Link to comment
Share on other sites

Try this - make sure you notice that there is server code and client code

#cs
;server

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

; Start The UDP Services
;==============================================
UDPStartup()
If @error <> 0 Then
    MsgBox(0, "UDP Startup ERROR", @error, 1)
    Exit
EndIf

; Bind to a SOCKET
;==============================================
$socket = UDPBind(@IPAddress1, 65532)
If @error <> 0 Then
    MsgBox(0, "UDP BIND ERROR", @error, 1)
    Exit
EndIf

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

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

#ce



;#cs
;client
    
;;This is the UDP Client
;;Start the server first

; Start The UDP Services
;==============================================
UDPStartup()
If @error <> 0 Then
    MsgBox(0, "UDP Startup ERROR", @error, 1)
    Exit
EndIf

; Open a "SOCKET"
;==============================================
$socket = UDPOpen("192.168.0.255", 65532)
$text = ''
For $i = 0 to UBound($socket) -1
$text = $text & @LF & $socket[$i] & @LF
Next
MsgBox(0,"UDPOpen",$text)
If @error <> 0 Then
    MsgBox(0, "UDP Open ERROR", @error, 1)
    Exit
EndIf

;#cs
$n=0
While 1
    Sleep(2000)
    $n = $n + 1
    $status = UDPSend($socket, "Message # " & $n)
    ;MsgBox(0,'$status',$status)
    If $status = 0 then 
        MsgBox(0, "ERROR", "Error while sending UDP message: " & @error)
        Exit
    EndIf
WEnd
;#ce

;UDPSend($socket, "your message")

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc
    
;#ce

edit fixed code

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

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