Unsureness有关传递端点Socket.ReceiveFrom() [英] Unsureness about passing EndPoint to Socket.ReceiveFrom()

查看:227
本文介绍了Unsureness有关传递端点Socket.ReceiveFrom()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我做这样的事情:

byte[] buffer = new byte[1024];
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint remote = new IPEndPoint(IPAddress.Parse("12.34.56.78"), 1337);
sock.ReceiveFrom(buffer, ref remote);



请问ReceiveFrom方法只接收端点被传递的数据包?该文件规定如下:

Will the ReceiveFrom method only receive packets from the endpoint that is being passed? The documentation states the following:

通过无连接协议,ReceiveFrom将阅读第一
排队接收到本地网络缓冲数据报。

With connectionless protocols, ReceiveFrom will read the first enqueued datagram received into the local network buffer.

这是否意味着,传递的终点是仅用于存储主机的数据包来自的端点,不影响ReceiveFrom方法的行为呢?如果是这样,为何还需要为裁判来传递,而不是去?

Does that mean that the passed EndPoint is only used for storing the EndPoint of the host the packet has come from and does not affect the ReceiveFrom method's behaviour at all? If so, why does it need to be passed as "ref" instead of "out"?

推荐答案

注意 ReceiveFrom 方法是的 recvfrom的 Winsock函数。这个函数的指针,的sockaddr 结构是可选的,分配/释放对发送方。

Note that ReceiveFrom method is a managed wrapper for recvfrom WinSock function. This function takes a pointer to sockaddr structure that is optional and allocated/deallocated on the caller side.

考虑到这一点,我有几个理论,为什么是端点通过REF ,而不是退出

With that in mind I have a few theories why is the EndPoint passed as ref and not out:


  1. 也许对于一致性与Winsock函数的端点调用方分配的,因此通过通过REF

  2. 也许端点是在被认为是一个可选参数某些时候,却从未实现的(我查了一下,那一定非空)。

  3. 也许对某些协议没有通过端点参数传递的处理方向。甚至将来的协议: - )

  1. Maybe for the consistency with WinSock function the EndPoint is allocated by the caller and therefore passed by ref.
  2. Maybe EndPoint was at some point considered to be an optional parameter, but this was never implemented (I checked, it must be non-null).
  3. Maybe for some protocols there are processing directions passed through the EndPoint parameter. Maybe even future protocols :-)

这篇关于Unsureness有关传递端点Socket.ReceiveFrom()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆