This post is part of the UDPCommunication project.

UDP - User Datagram Protocol

The class in this post allows, in an easy and fast manner, to receive UDP messages. You don’t need to be a socket expert to use the class, cause you only need to write few rows of code to use it. Following, a little example showing how to use the class:

private void InitConnection()
{
	m_udpListener = new UDPListener(m_port, new byte[4] { 0, 0, 0, 0 });
	m_udpListener.MessageReceived += new UDPMessageReceivedDelegate(udpListener_MessageReceived);
}

private void udpListener_MessageReceived(IPEndPoint sender, IPEndPoint receiver, byte[] message)
{
	// insert here the code to read the message received
}

So, you only need to create the UDPListener object and set the event to invoke when the listener receives a message. The MessageReceived method delegate has three parameters: Two IPEndPoint objects containing the information about the sender and the receiver, and a byte array containing the body of the message received. You can use the last parameter to read the content of the message received.
Last note:
The message received method is asyncronous, so you don’t block the UDPListener while you reading a long message.

Download UDPListener. Downloads: UDPListener (423) -


Se sei interessato a questo post, potresti anche provare a leggere:

  • No related posts