yuexb
asked on
udp socket broadcasting
hi, all,
im trying to write a small application that listens to a device and respond thru udp socket. neither the device nor the program knows the ip address of each other. but i think udp socket can do broadcasting. my program receives the packet, but the responding packet it sends out can't be received (or maybe the packet can't be sent out). does anyone have any idea on this problem? here's my code:
Private Sub Form_Load()
bcWinsock.Protocol = sckUDPProtocol
bcWinsock.RemotePort = 1111
bcWinsock.LocalPort = 1007
bcWinsock.Bind
End Sub
Private Sub bcWinsock_DataArrival(ByVa l bytesTotal As Long)
Dim helloInfo As String
bcWinsock.GetData helloInfo
statusText.Text = statusText.Text + helloInfo
'feedback
bcWinsock.SendData "message received"
End Sub
im trying to write a small application that listens to a device and respond thru udp socket. neither the device nor the program knows the ip address of each other. but i think udp socket can do broadcasting. my program receives the packet, but the responding packet it sends out can't be received (or maybe the packet can't be sent out). does anyone have any idea on this problem? here's my code:
Private Sub Form_Load()
bcWinsock.Protocol = sckUDPProtocol
bcWinsock.RemotePort = 1111
bcWinsock.LocalPort = 1007
bcWinsock.Bind
End Sub
Private Sub bcWinsock_DataArrival(ByVa
Dim helloInfo As String
bcWinsock.GetData helloInfo
statusText.Text = statusText.Text + helloInfo
'feedback
bcWinsock.SendData "message received"
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
''''''''''''''''''''''''''
'Admin Side
''''''''''''''''''''''''''
Add 1 Winsock Control named Winsock1
Set Winsock control to UDP protocal
Add 1 Command Button named Command1
Add the following code to the Form
''''''''''''''''''''''''''
Private Sub Command1_Click()
Dim sTr As String
sTr = "Broadcast using UDP"
Winsock1.SendData sTr
End Sub
Private Sub Form_Load()
With Winsock1
.RemoteHost = "255.255.255.255" ' Broadcast IP
.RemotePort = 2001 ' Port to connect to.
.Bind 2002 ' Bind to the local port.
End With
End Sub
'Server Side
''''''''''''''''''''''''''
Add 1 Winsock Control named Winsock1
Set Winsock control to UDP protocal
Add the following code to the Form
''''''''''''''''''''''''''
Private Sub Form_Load()
Winsock1.RemotePort = 2002
Winsock1.RemoteHost = "255.255.255.255"
Winsock1.Bind 2001
End Sub
Private Sub Winsock1_DataArrival(ByVal
Dim sTr As String
Winsock1.GetData sTr
MsgBox sTr, vbInformation, "UDP Broadcasting"
End Sub
'Thats it that will broadcast using UDP without knowing the IP address.
'Please remember than when using UDP it is only for LAN, UDP packets will not reach pass the router.