下面是MSDN中的范例:下面的示例使用 UdpClient 侦听端口 11000 上的多路广播地址组 224.168.100.2 的 UDP 数据文报广播。它接收消息字符串并将消息写入控制台。
[Visual Basic]
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.TextPublic Class UDPMulticastListener
Private Shared GroupAddress As IPAddress = _
IPAddress.Parse("224.168.100.2")
Private Shared GroupPort As Integer = 11000
Private Shared Sub StartListener()
Dim done As Boolean = False
Dim listener As New UdpClient()
Dim groupEP As New IPEndPoint(GroupAddress, GroupPort)
Try
listener.JoinMulticastGroup(GroupAddress)
listener.Connect(groupEP)
While Not done
Console.WriteLine("Waiting for broadcast")
Dim bytes As Byte() = listener.Receive(groupEP)
Console.WriteLine("Received broadcast from {0} :" + _
ControlChars.Cr + " {1}" + ControlChars.Cr, _
groupEP.ToString(), _
Encoding.ASCII.GetString(bytes, 0, bytes.Length))
End While
listener.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'StartListener
' Entry point that delegates to C-style main Private Function.
Public Overloads Shared Sub Main()
System.Environment.ExitCode = _
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Function Main(args() As [String]) As Integer
StartListener()
Return 0
End Function 'Main
End Class 'UDPMulticastListener
[C#]
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;public class UDPMulticastListener { private static readonly IPAddress GroupAddress =
IPAddress.Parse("224.168.100.2");
private const int GroupPort = 11000;
private static void StartListener() {
bool done = false;
UdpClient listener = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort); try {
listener.JoinMulticastGroup(GroupAddress);
listener.Connect(groupEP);
while (!done) {
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive( ref groupEP); Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
} listener.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
} public static int Main(String[] args) {
StartListener(); return 0;
}
}
下面的示例使用 UdpClient 将 UDP 数据文报发送到端口 11000 上的多路广播地址组 224.268.100.2。它发送命令行上指定的消息字符串。
[Visual Basic]
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class UDPMulticastSender
Private Shared GroupAddress As IPAddress = _
IPAddress.Parse("224.168.100.2")
Private Shared GroupPort As Integer = 11000
Private Shared Sub Send(message As [String])
Dim sender As New UdpClient()
Dim groupEP As New IPEndPoint(GroupAddress, GroupPort)
Try
Console.WriteLine("Sending datagram : {0}", message)
Dim bytes As Byte() = Encoding.ASCII.GetBytes(message)
sender.Send(bytes, bytes.Length, groupEP)
sender.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'Send
' Entry point that delegates to C-style main Private Function.
Public Overloads Shared Sub Main()
System.Environment.ExitCode = _
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Function Main(args() As [String]) As Integer
Send(args(0))
Return 0
End Function 'Main
End Class 'UDPMulticastSender
[C#]
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;public class UDPMulticastSender { private static IPAddress GroupAddress =
IPAddress.Parse("224.168.100.2");
private static int GroupPort = 11000;
private static void Send( String message) {
UdpClient sender = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort); try {
Console.WriteLine("Sending datagram : {0}", message);
byte[] bytes = Encoding.ASCII.GetBytes(message); sender.Send(bytes, bytes.Length, groupEP);
sender.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
} public static int Main(String[] args) {
Send(args[0]); return 0;
}
}问题:
1)其中有一句话:使用 UdpClient 侦听端口 11000 上的多路广播地址组 224.168.100.2 的 UDP 数据文报广播。
224.168.100.2 明明是一个IP地址,怎么可以称之为“多路广播地址组”,如果是“组”的话,是不是应该是一组IP地质才对?2)将上面的代码原封不动的复制到VS2003上面,
在代码:listener.JoinMulticastGroup(GroupAddress); 出现了下面的错误:提供了一个无效的参数。请问这个GroupAddress应该是个什么样的参数?如果我的接收Udp数据的计算机是一台确定的机器,不是一组机器,这句加入组的代码:
listener.JoinMulticastGroup(GroupAddress);是不是可以去掉?
谢谢!!
[Visual Basic]
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.TextPublic Class UDPMulticastListener
Private Shared GroupAddress As IPAddress = _
IPAddress.Parse("224.168.100.2")
Private Shared GroupPort As Integer = 11000
Private Shared Sub StartListener()
Dim done As Boolean = False
Dim listener As New UdpClient()
Dim groupEP As New IPEndPoint(GroupAddress, GroupPort)
Try
listener.JoinMulticastGroup(GroupAddress)
listener.Connect(groupEP)
While Not done
Console.WriteLine("Waiting for broadcast")
Dim bytes As Byte() = listener.Receive(groupEP)
Console.WriteLine("Received broadcast from {0} :" + _
ControlChars.Cr + " {1}" + ControlChars.Cr, _
groupEP.ToString(), _
Encoding.ASCII.GetString(bytes, 0, bytes.Length))
End While
listener.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'StartListener
' Entry point that delegates to C-style main Private Function.
Public Overloads Shared Sub Main()
System.Environment.ExitCode = _
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Function Main(args() As [String]) As Integer
StartListener()
Return 0
End Function 'Main
End Class 'UDPMulticastListener
[C#]
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;public class UDPMulticastListener { private static readonly IPAddress GroupAddress =
IPAddress.Parse("224.168.100.2");
private const int GroupPort = 11000;
private static void StartListener() {
bool done = false;
UdpClient listener = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort); try {
listener.JoinMulticastGroup(GroupAddress);
listener.Connect(groupEP);
while (!done) {
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive( ref groupEP); Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
} listener.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
} public static int Main(String[] args) {
StartListener(); return 0;
}
}
下面的示例使用 UdpClient 将 UDP 数据文报发送到端口 11000 上的多路广播地址组 224.268.100.2。它发送命令行上指定的消息字符串。
[Visual Basic]
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class UDPMulticastSender
Private Shared GroupAddress As IPAddress = _
IPAddress.Parse("224.168.100.2")
Private Shared GroupPort As Integer = 11000
Private Shared Sub Send(message As [String])
Dim sender As New UdpClient()
Dim groupEP As New IPEndPoint(GroupAddress, GroupPort)
Try
Console.WriteLine("Sending datagram : {0}", message)
Dim bytes As Byte() = Encoding.ASCII.GetBytes(message)
sender.Send(bytes, bytes.Length, groupEP)
sender.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'Send
' Entry point that delegates to C-style main Private Function.
Public Overloads Shared Sub Main()
System.Environment.ExitCode = _
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Function Main(args() As [String]) As Integer
Send(args(0))
Return 0
End Function 'Main
End Class 'UDPMulticastSender
[C#]
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;public class UDPMulticastSender { private static IPAddress GroupAddress =
IPAddress.Parse("224.168.100.2");
private static int GroupPort = 11000;
private static void Send( String message) {
UdpClient sender = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort); try {
Console.WriteLine("Sending datagram : {0}", message);
byte[] bytes = Encoding.ASCII.GetBytes(message); sender.Send(bytes, bytes.Length, groupEP);
sender.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
} public static int Main(String[] args) {
Send(args[0]); return 0;
}
}问题:
1)其中有一句话:使用 UdpClient 侦听端口 11000 上的多路广播地址组 224.168.100.2 的 UDP 数据文报广播。
224.168.100.2 明明是一个IP地址,怎么可以称之为“多路广播地址组”,如果是“组”的话,是不是应该是一组IP地质才对?2)将上面的代码原封不动的复制到VS2003上面,
在代码:listener.JoinMulticastGroup(GroupAddress); 出现了下面的错误:提供了一个无效的参数。请问这个GroupAddress应该是个什么样的参数?如果我的接收Udp数据的计算机是一台确定的机器,不是一组机器,这句加入组的代码:
listener.JoinMulticastGroup(GroupAddress);是不是可以去掉?
谢谢!!
你把地址和端口改下就行了
private static void SampleOne()
{
UdpClient udpClient = new UdpClient(); IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.1"),4444);
//
try
{
int i =0;
while(true)
{
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there" + i);
udpClient.Send(sendBytes, sendBytes.Length,RemoteIpEndPoint);
Console.WriteLine("发送了" + i + "次" + ",,,内容是 Is anybody there" + i);
i++;
}
}
catch ( Exception e )
{
Console.WriteLine(e.ToString());
}
}
如果你用上面的代码发送数据 那么要保证发送端口和接收端口要一样
另外 我注释掉的一行代码是网络上有多播服务器时用的 224.4.5.6是多播地址
如果你的网络里没有多播 你就用不上这一行了private static void SampleTwo()
{
UdpClient receivingUdpClient = new UdpClient(8888);
// receivingUdpClient.JoinMulticastGroup(IPAddress.Parse("224.4.5.6"));
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 2012); try
{
int i = 0;
while(i<10000)
{
Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine(returnData);
i++;
}
}
catch ( SocketException e )
{
Console.WriteLine(e.ErrorCode);
Console.WriteLine(e.ToString());
} }
To: ljb2000(一天一夜) 你的代码完全可以运行。想给你多加些分,如何操作? thanks!!
的意思是从本机的8888端口接收数据IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 2012);
在MSDN里的解释是"一个 IPEndPoint,它表示从其发送数据的远程主机。"
反正我这里的2012是随便写的,测试的时候好象随便填什么端口都能接收到数据,记不太清了.你可以自己改成其他端口试试