class methods
- ofxUDPManager()
- ~ofxUDPManager()
- Bind()
- BindMcast()
- Close()
- Connect()
- ConnectMcast()
- Create()
- GetListenAddr()
- GetMaxMsgSize()
- GetReceiveBufferSize()
- GetRemoteAddr()
- GetSendBufferSize()
- GetTTL()
- GetTimeoutReceive()
- GetTimeoutSend()
- HasSocket()
- PeekReceive()
- Receive()
- Send()
- SendAll()
- SetEnableBroadcast()
- SetNonBlocking()
- SetReceiveBufferSize()
- SetReuseAddress()
- SetSendBufferSize()
- SetTTL()
- SetTimeoutReceive()
- SetTimeoutSend()
- Setup()
UDP is a network protocol that is faster and less rigid in its packet transmission requirements. Unlike TCP, UDP has no notion of connections nor does it check to see if a packet has been successfuly recieved by the client. A UDP socket can receive datagrams from any server on the network and send datagrams to any host on the network. In addition, datagrams may arrive in any order, never arrive at all, or be duplicated in transit. There are three modes of UDP servers: socket (aka unicast), broadcast, and multicast.
Unicast refers to a unique host-client. This is a one-to one connection between the client and the server
Multicast is the delivery of a message or information to a group of destination computers simultaneously in a single transmission. A packet sent to a unicast or broadcast address is only delivered to the host identified by that address. To the contrary, when packet is send to a multicast address, all interfaces identified by that address receive the data. However, multicast has the drawback that it is not well supported by routers and NAT.
Broadcast allows you to call every host within a subnet. It's like Multicast but doesn't require that your network infrastructure support it.
A very simple unicast server looks like this:
void ofApp::setup()
{
//create the socket and set to send to 127.0.0.1:11999
udpConnection.Create();
udpConnection.Connect("127.0.0.1",11999);
udpConnection.SetNonBlocking(true);
}
void ofApp::keyPressed(int key)
{
string message = "You pressed a key";
udpConnection.Send(message.c_str(), message.length());
}
A very simple unicast client looks like this:
void ofApp::setup()
{
//create the socket and bind to port 11999
udpConnection.Create();
udpConnection.Bind(11999);
udpConnection.SetNonBlocking(true);
}
void ofApp::update()
{
char udpMessage[1000];
udpConnection.Receive(udpMessage,1000);
string message=udpMessage;
}
Notice that these are quite different than the creation of TCP servers and clients which you might be more familiar with.
The basic usage of UDP for socket servers and clients looks like so:
UDP Socket Server (sending): 1) Create() - initialize the server 2) Connect() - connect to an IP and a Port that you'll be sending messages on 3) Send() - send the message
UDP Socket Client (receiving): 1) Create() - intialize the client 2) Bind() - bind the client to a port and listen for any UDP messages on that port 3) Receive() - receive any data broadcast over UDP on the port set up to receive on
The basic usage of UDP for multicast looks like so:
UDP Multicast (sending): 1) Create() - initialize the server 2) ConnectMcast() - connect to an IP and a Port that you'll broadcast on 3) Send() - send a message to any listening clients
UDP Multicast (receiving): 1) Create() - initialize the client 2) BindMcast() - bind to a port 3) Receive() - check to see if any data has been received
Bind(...)
bool ofxUDPManager::Bind(unsigned short usPort)
Bind a port to receive socket/unicast UDP:
udpConnection.Create();
udpConnection.Bind(11999);
udpConnection.SetNonBlocking(true);
BindMcast(...)
bool ofxUDPManager::BindMcast(char *pMcast, unsigned short usPort)
Bind to multicast address to receive data:
udpConnection.Create();
udpConnection.BindMcast("224.0.0.1", 11999);
Close()
bool ofxUDPManager::Close()
Stop listening on a given port. Works with both socket and multicast.
Connect(...)
bool ofxUDPManager::Connect(const char *pHost, unsigned short usPort)
Connect to a socket client to send information.
udpConnection.Create();
udpConnection.Connect("127.0.0.1",11999);
udpConnection.SetNonBlocking(true);
ConnectMcast(...)
bool ofxUDPManager::ConnectMcast(char *pMcast, unsigned short usPort)
Connect to a multicast address.
udpConnection.Create();
udpConnection.Connect("224.0.0.1",11999);
Create()
bool ofxUDPManager::Create()
Create the UDP manager. Must be called before binding to any IP or sockets.
GetReceiveBufferSize()
int ofxUDPManager::GetReceiveBufferSize()
Get the size of the receive buffer. The minimum (doubled) value for this option is 256. The max is determined by your OS.
GetRemoteAddr(...)
bool ofxUDPManager::GetRemoteAddr(string &address, int &port)
Returns the dots and numbers remote address in a string,
GetSendBufferSize()
int ofxUDPManager::GetSendBufferSize()
Get the size of the send buffer. The minimum (doubled) value for this option is 256. The max is determined by your OS.
Receive(...)
int ofxUDPManager::Receive(char *pBuff, const int iSize)
Receives a message to a buffer of size iSize. Receive() returns the number of bytes actually received.
char udpMessage[100000];
udpConnection.Receive(udpMessage,100000);
string message=udpMessage;
Send(...)
int ofxUDPManager::Send(const char *pBuff, const int iSize)
Send a char* of data with length of iSize to all listeners.
string message = "A message";
udpConnection.Send(message.c_str(),message.length());
SendAll(...)
int ofxUDPManager::SendAll(const char *pBuff, const int iSize)
The SendAll() method is useful for extremely large data objects that may need multiple sendto() calls to actually be completely.
SetEnableBroadcast(...)
bool ofxUDPManager::SetEnableBroadcast(bool enableBroadcast)
Broadcast allows sending of packets to a particular network layer. If you're only using a single local network without a large number of attached machines, using broadcast may make more sense than trying to use multicast.
SetNonBlocking(...)
bool ofxUDPManager::SetNonBlocking(bool useNonBlocking)
Determines whether calls to send or receive are allowed to block their thread until they've completed. If your network infrastructure is time critical, then this may be a good choice. Usually though, you should leave it off.
SetReceiveBufferSize(...)
bool ofxUDPManager::SetReceiveBufferSize(int sizeInByte)
Set the size of the receive buffer. The minimum (doubled) value for this option is 256. The max is determined by your OS.
SetSendBufferSize(...)
bool ofxUDPManager::SetSendBufferSize(int sizeInByte)
Set the size of the send buffer. The minimum (doubled) value for this option is 256. The max is determined by your OS.
SetTTL(...)
bool ofxUDPManager::SetTTL(int nTTL)
The TTL (Time To Live) field in the IP header has a double significance in multicast. As always, it controls the live time of the datagram to avoid it being looped forever due to routing errors. Routers decrement the TTL of every datagram as it traverses from one network to another and when its value reaches 0 the packet is dropped.
A list of TTL thresholds and their associated scope follows:
TTL Scope
0 Restricted to the same host. Won't be output by any interface. 1 Restricted to the same subnet. Won't be forwarded by a router. <32 Restricted to the same site, organization or department. <64 Restricted to the same region. <128 Restricted to the same continent. <255 Unrestricted in scope. Global.
SetTimeoutReceive(...)
void ofxUDPManager::SetTimeoutReceive(int timeoutInSeconds)
Set a timeout for any receive operations in seconds.
SetTimeoutSend(...)
void ofxUDPManager::SetTimeoutSend(int timeoutInSeconds)
Set a timeout for any send operations in seconds.
Last updated Tuesday, 19 November 2024 17:23:44 UTC - 2537ee49f6d46d5fe98e408849448314fd1f180e
If you have any doubt about the usage of this module you can ask in the forum.
If you want to contribute better documentation or start documenting this section you can do so here
If you find anything wrong with this docs you can report any error by opening an issue