udp_socket |
Datagram communication using os_udp_socket
is often called connectionless, because the sender is not connected to the
receiver with a reliability protocol that guarantees message delivery and
connection failure notification.
In UDP communication, the sender has no automatic way to determine whether or not the receiver actually read the message. Because the sender and receiver are not actually connected, there is no notification if the communication fails. However, UDP communication is often faster, especially when a sender must communicate with multiple receivers and reliability is not a concern
The os_udp_socket
interface provides the send_to() and receive_from()
functions for sending and receiving messages. send_to()
takes the address of the destination socket as its first parameter. receive_from()
takes a reference to an os_socket_address as its
first parameter and sets this reference to the sender's address upon receiving a
message.
The following example demonstrates
datagram socket communications between processes. The first program, socket2s.cpp
, is the server. The server process constructs an os_udp_socket
bound to port 3005 of the local host. This process then waits for an incoming
datagram from the client and echoes it to the socket that sent it. The second
program, socket2c.cpp , is the client. The client
process connects to the server process and sends it a datagram.
// Example socket2 server.
#include <iostream>
#include <ospace/network.h>
void
main()
{
os_network_toolkit initialize;
// Create UDP socket at well known port on local host.
os_udp_socket socket( os_socket_address( 3005 ) );
char string[ 100 ];
os_socket_address address;
int bytes = socket.receive_from( address, string, 100 );
string[ bytes ] = '\0';
cout << "Server received: " << string;
cout<< " from port " << address.port() << endl;
cout << "Server sends: " << string << " to port ";
cout << address.port() << endl;
socket.send_to( address, string, bytes ); // Echo back reply.
}
Server received: hello from port 62885
Server sends: hello to port 62885
// Example socket2 client.
#include <iostream>
#include <ospace/network.h>
void
main()
{
os_network_toolkit initialize;
int server_port = 3005; // Port 3005 on local host.
os_socket_address server_address( server_port );
os_udp_socket socket; // Create UDP socket on system assigned port.
cout << "Client port = " << socket.socket_address().port() << endl;
cout << "Client sends: hello" << " to port " << server_port << endl;
socket.send_to( server_address, "hello", 5 );
char string[ 100 ];
int bytes = socket.read( string, 100 ); // Receive reply.
string[ bytes ] = '\0';
cout << "Client received: " << string << endl << endl;
}
Client port = 62885
Client sends: hello to port 3005
Client received: hello
The maximum size datagram that can be sent varies with each system. The concept of end-of-input does not exist for UDP sockets, because the receiver is never connected to a single data source.
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.