使用Udp连接接收数据报

发布于 2024-07-16 05:05:58 字数 348 浏览 7 评论 0原文

为了通过 UDP 连接接收数据报,我创建了一个 UDPClient 类型的对象。

 receivedNotificationSock = new UdpClient();

然而,一旦完成并使用接收方法:

 receivedHostNameBuffer=receivedNotificationSock.Receive(ref receivedNotificationIP);

我收到一个异常,说我必须调用绑定方法。 但是UDPClient类中没有bind方法。 如果可能的话,请给我提供代码,告诉我应该采取什么措施来克服这个异常。

In order to receive datagrams through an UDP connection I have created an object of type UDPClient.

 receivedNotificationSock = new UdpClient();

However once done and on using the receive method:

 receivedHostNameBuffer=receivedNotificationSock.Receive(ref receivedNotificationIP);

I am getting an exception saying that I must call the bind method.
But there is no bind method in the UDPClient class.
Could You guys please provide me with the code if possible as to what should be done to overcome this exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花随流水 2024-07-23 05:05:58

我想你需要更多地了解套接字。

所有套接字都有一个端口号。 首先,创建一个套接字 - 它本身几乎没有用处。 它只是漂浮在那里。 但是然后你绑定它 - 为它分配一个端口号。 现在它很有用 - 现在您可以在其上发送和接收数据。

请记住,所有 UDP 通信均由源 IP 和端口以及目标 IP 和端口的四元数据集定义。 新创建的套接字没有 IP 地址或端口; 绑定给它一个IP地址和端口。

不幸的是,我不是 C# 程序员,所以我无法正确回答你的问题。 但至少您知道为什么它很重要。

You need I think to know some more about sockets.

All sockets possess a port number. First, you create a socket - which is almost useless on its own. It just floats there. But then you bind it - you assign it a port number. Now it's useful - now you can send and receive data on it.

Remember, all UDP communications are defined by the quad data set of the IP and port of the source and the IP and port of the destination. A freshly created socket doesn't have an IP address or port; binding gives it an IP address and port.

Unfortunately, I'm not a C# programmer, so I can't properly answer your question. But at least you know why it's important.

彼岸花似海 2024-07-23 05:05:58

将端口号传递到 UDP 客户端的构造函数中。

 receivedNotificationSock = new UdpClient(21000);

您可能需要更改防火墙设置以允许绑定,尽管当您首次在开发计算机上运行此操作时通常会打开一个弹出窗口。

Pass the port number into the constructor of your UDP client.

 receivedNotificationSock = new UdpClient(21000);

You may need to change firewall settings to allow the bind, though a popup window normally opens when you first run this on your dev machine.

死开点丶别碍眼 2024-07-23 05:05:58

对于套接字编程,您需要知道需要在客户端和服务器端执行的系统调用的顺序。

如果您正在编写客户端

  1. 您可以通过socket调用打开一个套接字。

  2. 然后,您可以通过 connect 调用连接到服务器端口

  3. 连接成功后

  4. 然后使用 sendsendtowrite 将请求发送到服务器< /p>

  5. 这会导致接收数据您可以使用 receiveread 读取,

在服务器端

  1. 您创建一个套接字

  2. 将其绑定到端口

  3. 开始侦听套接字上的传入使用 listen 来自不同客户端的连接。
    还有一种使用 select 系统调用来侦听连接的非阻塞方式。

  4. 一旦建立连接,您基本上就可以读取请求并开始处理它。

下面是一个 C# 示例,可能对您有用。

http://www.developerfusion.com/article/分段阅读_第 3918 章

For Socket proramming you need to know the sequence of syscalls you need to do on client side and on the server side.

If you are writting a client :

  1. you open a socket with a socket call.

  2. you then connect to the server port with a connect call

  3. once connect is successful

  4. then you send the request to the server using either a send or sendto or a write

  5. which results in reception of data that you can read using a receive or read

On Server Side

  1. you create a socket

  2. bind it to a port

  3. start listening on the socket for incoming connections from various clients using a listen.
    There is a non blocking way of listening for connections as well with a select syscall.

  4. Once the you establish a connection you can essentially read the request and start processing it.

Here's an example in C# that may be useful to you.

http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文