UDPSender: Sending UDP messages in C#
Categories: C, Source Code, UDP Communication
Tags:
This post is part of the UDPCommunication project.
This is a class that allows to send UDP messages in C# with an unbelievable semplicity.
To create the class you only need to create it, specifying a port for the udp communication, eg 3125 in this case:
try
{
INetworkSender udpSender = new UDPSender(3125)
}
catch(UDPSenderException e)
{
// error during the socket initialization
}
Sending a message to an host is simple: call the SendMessage method specifying the receiver (with an host name, like “www.coders4fun.com“, or with an ip, like “192.168.10.6“) and a byte array containing the message.
byte[] message = new byte[n] {1, 2, 3, 4, ..., n};
try
{
udpSender.SendMessage("www.coders4fun.com", message)
}
catch(UDPSenderException e)
{
// error during the send
}
The UDPSender class implements an interface named INetworkSender (this can be usefull if you have various sender, in my case I also have a TCPServer that implements this interface too).
public interface INetworkSender
{
void SendMessage(string address, byte[] message);
}
If the class encounter an error, it will generate an UDPSenderException exception, that contains the error description, the reason of the exception (in the innerException), and the UDPSender object that throwed the exception.
Download UDPSender. Downloads: 831
Se sei interessato a questo post, potresti anche provare a leggere:
- No related posts
04 Oct 2007 dzamir

This code does not work via internet.
UsandoIt only worked on the local LAN.