| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1716 人关注过本帖
标题:[求助]UDP广播接收不到
取消只看楼主 加入收藏
yz870735
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2007-5-21
收藏
 问题点数:0 回复次数:1 
[求助]UDP广播接收不到

高手帮我看一下这代码哪里出问题了 怎么老是广播不到

服务器端:
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



搜索更多相关主题的帖子: UDP 服务器 System Imports 广播 
2007-11-12 10:59
yz870735
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2007-5-21
收藏
得分:0 
帮帮忙看一下  最近给这个问题烦死了` ``

2007-11-12 19:19
快速回复:[求助]UDP广播接收不到
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016599 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved