为什么我建立一个UDP Listen 会报错?

Colin-Han 2004-09-10 01:24:16
我想建立一个UDP的监听服务,为什么总是报错?
下面是代码:
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any, 11113));
socket.Listen(10);
最后一句就抛出一个10045的错误。
Operation not supported.
The attempted operation is not supported for the type of object referenced. Usually this occurs when a socket descriptor to a socket that cannot support this operation is trying to accept a connection on a datagram socket.
...全文
968 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Colin-Han 2004-09-10
  • 打赏
  • 举报
回复
Thanks,派分
Colin-Han 2004-09-10
  • 打赏
  • 举报
回复
Async_Send_Receive.ReceiveFrom_Callback 的定义?
daguzhang 2004-09-10
  • 打赏
  • 举报
回复
写错了的点是
Socket s = new Socket(lep.Address.AddressFamily,
SocketType.Dgram,
ProtocolType.Udp);
daguzhang 2004-09-10
  • 打赏
  • 举报
回复
IPHostEntry lipa = Dns.Resolve("host.contoso.com");
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);

Socket s = new Socket(lep.Address.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);

IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)sender;

try{
while(true){
allDone.Reset();
StateObject so2 = new StateObject();
so2.workSocket = s;
Console.WriteLine("Attempting to Receive data from host.contoso.com");

s.BeginReceiveFrom(so2.buffer, 0, StateObject.BUFFER_SIZE,0, ref tempRemoteEP,
new AsyncCallback(Async_Send_Receive.ReceiveFrom_Callback), so2);
allDone.WaitOne();
}
}
catch (Exception e){
Console.WriteLine(e.ToString());
}
Colin-Han 2004-09-10
  • 打赏
  • 举报
回复
如果我要建立一个非阻塞模式的UDP,是否应该使用BeginReceiveFrom方法,又没有相关的例子?
wolve 2004-09-10
  • 打赏
  • 举报
回复
ReceiveFrom就相当于监听,因为它是同步的,如果没有数据传来,且blocking标志为true.它会一直等.当然,你可以使用异步,一般UDP的I/O操作都是异步的.
daguzhang 2004-09-10
  • 打赏
  • 举报
回复
http://tech.ccidnet.com/pub/article/c1137_a73028_p1.html
dgvictor 2004-09-10
  • 打赏
  • 举报
回复 1
udp socket 没有listen 一说,tcp socket 才有。。
daguzhang 2004-09-10
  • 打赏
  • 举报
回复
1。你不需要立一个UPD的监听服务
2。
我怎么知道有数据到这个端口上了?
s.ReceiveFrom(buffer, 0, 100, SocketFlags.None, ref tempRemoteEP);
tempRemoteEP就表示远程的endpoint


daguzhang 2004-09-10
  • 打赏
  • 举报
回复
看列子:
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());
}
Colin-Han 2004-09-10
  • 打赏
  • 举报
回复
如果我想建立一个UPD的监听服务,怎么做?我怎么知道有数据到这个端口上了?
daguzhang 2004-09-10
  • 打赏
  • 举报
回复
如果当前使用的是无连接协议(如 UDP),则根本不需要侦听连接。调用 ReceiveFrom 方法可接受任何传入的数据报。使用 SendTo 方法可将数据报发送到远程主机。
Colin-Han 2004-09-10
  • 打赏
  • 举报
回复
当时通过防火墙,可以知道,执行完后,他会把这个端口占掉。
daguzhang 2004-09-10
  • 打赏
  • 举报
回复
udp是无连接的

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧