高手帮我看一下这代码哪里出问题了 怎么老是广播不到
服务器端:
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class frmMain
Inherits System.Windows.Forms.Form
Private Shared GroupAddress As IPAddress = IPAddress.Parse("224.0.0.1") '多路广播地址
Private Shared GroupPort As Integer = 8080 '端口
Private Shared Sub Send(ByVal strMessage As String) '发送一条指定信息
Dim sender As New UdpClient()
Dim groupEP As New IPEndPoint(GroupAddress, GroupPort)
Try
Dim bytes As Byte() = Encoding.ASCII.GetBytes(strMessage)
MsgBox("1")
sender.Send(bytes, bytes.Length, groupEP)
MsgBox("2")
sender.Close()
MsgBox("3")
MsgBox("发送成功")
MsgBox("4")
Catch e As Exception
MsgBox(e.ToString())
End Try
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Send(txtMessage.Text)
End Sub
End Class
客户端:
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class frmMain
Inherits System.Windows.Forms.Form
Private GroupAddress As IPAddress = IPAddress.Parse("224.0.0.1")
Private GroupPort As Integer = 8080
Private Sub StartListen() '开始监听
Dim listener As New System.Net.Sockets.UdpClient()
Dim groupEP As New IPEndPoint(GroupAddress, GroupPort)
Dim i As Integer
Try
listener.JoinMulticastGroup(GroupAddress)
listener.Connect(groupEP)
For i = 1 To 1000
Dim bytes As Byte() = listener.Receive(groupEP)
txtMessage.AppendText(Encoding.ASCII.GetString(bytes, 0, bytes.Length))
Next
listener.Close()
Catch e As Exception
MsgBox(e.ToString())
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
StartListen()
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class