读书人

[第一次发帖][讨论][求教][散分][高分

发布时间: 2012-05-03 14:06:56 作者: rapoo

[第一次发帖][讨论][求教][散分][高分]MSDN中C#的Udp示例中的疑惑
vs.net2003
示例
[Visual Basic, C#, C++] 下面的示例将无连接数据文报发送到指定的远程主机。偏移量、大小和 SocketFlags 将被传递给 SendTo 方法。
[Visual Basic, C#, C++] 注意 此示例显示如何使用 SendTo 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[C#]
IPHostEntry lipa = Dns.Resolve( "host.contoso.com ");
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);

Socket s = new Socket(lep.Address.AddressFamily,
SocketType.Dgram,
ProtocolType.Udp);

byte[] msg = Encoding.ASCII.GetBytes( "This is a test ");

try{
// Sends datagram to the IpEndPoint specified. This call blocks.
s.SendTo(msg, 0, msg.Length, SocketFlags.None, lep);


// Creates an IpEndPoint to capture the identity of the sending host.
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)sender;

// Creates a byte buffer to receive the message.
byte[] buffer = new byte[1024];

// Receives datagram from a remote host. This call blocks.
s.ReceiveFrom(buffer, 0, 100, SocketFlags.None, ref tempRemoteEP);

// Displays the information received to the screen.
Console.WriteLine( " I received the following message : " +
Encoding.ASCII.GetString(buffer));

}
catch(Exception e){
Console.WriteLine( "Exception : " + e.ToString());
}


或者UdpClient的例子
示例
[Visual Basic, C#] 下面的示例使用主机名 www.contoso.com 在端口 11000 上建立 UdpClient 连接。将很短的字符串消息发送到两个单独的远程主机。Receive 方法在接收消息前阻止执行。使用传递给 Receive 的 IPEndPoint 可以显示响应主机的标识。



[C#]
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient();
try{
udpClient.Connect( "www.contoso.com ", 11000);

// Sends a message to the host to which you have connected.
Byte[] sendBytes = Encoding.ASCII.GetBytes( "Is anybody there? ");

udpClient.Send(sendBytes, sendBytes.Length);

// Sends a message to a different host using optional hostname and port parameters.
UdpClient udpClientB = new UdpClient();
udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName ", 11000);

//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);

// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine( "This is the message you received " +
returnData.ToString());
Console.WriteLine( "This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +


RemoteIpEndPoint.Port.ToString());

udpClient.Close();
udpClientB.Close();

}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}


直接将代码复制到程序中
并将 "host.contoso.com " 换成IPAddress.Parse( "127.0.0.1 ")
(这么改应该没错吧……)
但在调试时却接收不到消息
面对MSDN上面的示例居然调不通
顿时感觉有点晕……
在继续研究了N久之后
发现如果把用来Receive的Socket绑定接收端口
问题就解决了……

发现MSDN上面对Socket的概述如下
如果当前使用的是面向连接的协议(如 TCP),则服务器可以使用 Listen 方法侦听连接。Accept 方法处理任何传入的连接请求,并返回可用于与远程主机进行数据通信的 Socket。可以使用此返回的 Socket 来调用 Send 或 Receive 方法。如果要指定本地 IP 地址和端口号,请在调用 Listen 方法之前先调用 Bind 方法。如果不调用 Bind,基础服务提供程序将为您分配这些值。此后,可以使用 LocalEndPoint 属性来标识分配给 Socket 的 IP 地址和端口号。如果想连接到侦听主机,请调用 Connect 方法。若要进行数据通信,请调用 Send 或 Receive 方法。
如果当前使用的是无连接协议(如 UDP),则根本不需要侦听连接。调用 ReceiveFrom 方法可接受任何传入的数据报。使用 SendTo 方法可将数据报发送到远程主机。

他确实没有说UDP不需要绑定,但我确实被误导了……

呵呵,说了那么一大堆,仿佛没有提到问题
我的问题就是
1、我上面描述的那一大堆……究竟是例子有问题,还是我理解有问题
2、请各位前辈谈一下使用UDP的方法
3、如果想实现向服务器发送命令,服务器解析并执行,传送的数据应如何定义比较好

欢迎大家讨论~
谢谢大家~

[解决办法]
UDP原本就太多的不稳定因素了
[解决办法]
JF
[解决办法]
应该差不多吧
[解决办法]
和例子无关,看看udp协议吧http://blog.csdn.net/goodboy1881/archive/2006/05/09/713856.aspx
[解决办法]
楼主应该了解一下udp协议
[解决办法]
up 一下

读书人网 >C#

热点推荐