使用Udp连接接收数据报
为了通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你需要更多地了解套接字。
所有套接字都有一个端口号。 首先,创建一个套接字 - 它本身几乎没有用处。 它只是漂浮在那里。 但是然后你绑定它 - 为它分配一个端口号。 现在它很有用 - 现在您可以在其上发送和接收数据。
请记住,所有 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.
将端口号传递到 UDP 客户端的构造函数中。
您可能需要更改防火墙设置以允许绑定,尽管当您首次在开发计算机上运行此操作时通常会打开一个弹出窗口。
Pass the port number into the constructor of your UDP client.
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.
对于套接字编程,您需要知道需要在客户端和服务器端执行的系统调用的顺序。
如果您正在编写客户端:
您可以通过
socket
调用打开一个套接字。然后,您可以通过
connect
调用连接到服务器端口连接成功后
然后使用
send
或sendto
或write
将请求发送到服务器< /p>receive
或read
读取,在服务器端
您创建一个
套接字
将其绑定到
端口
开始侦听套接字上的传入使用
listen
来自不同客户端的连接。还有一种使用
select
系统调用来侦听连接的非阻塞方式。一旦建立连接,您基本上就可以读取请求并开始处理它。
下面是一个 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 :
you open a socket with a
socket
call.you then connect to the server port with a
connect
callonce connect is successful
then you send the request to the server using either a
send
orsendto
or awrite
which results in reception of data that you can read using a
receive
orread
On Server Side
you create a
socket
bind
it to aport
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.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/