]>
Dogcows Code - chaz/yoink/blob - udp_sockets.cpp
92f909d4c4dae11d25ce5d95d6a46130d24cdd4e
1 ////////////////////////////////////////////////////////////////////////////////
3 // Author: Daniel Milton adapted by Andy Rushton
4 // Copyright: (c) Daniel Milton, Andy Rushton 2009
5 // License: BSD License, see ../docs/license.html
7 ////////////////////////////////////////////////////////////////////////////////
9 #include "udp_sockets.hpp"
11 ////////////////////////////////////////////////////////////////////////////////
16 ////////////////////////////////////////////////////////////////////////////////
18 ////////////////////////////////////////////////////////////////////////////////
20 // create an uninitialised socket
21 UDP_client::UDP_client(void) : IP_socket(UDP
)
25 // Send/Receive datagram packets to/from the given address/remote port on the local port.
26 // Enables default send to remote address/port
27 // - remote_address: IP name or number of remote host
28 // - remote_port: port number of remote host
29 // - local_port: port number to receive on - 0 to get an ephemeral port.
30 UDP_client::UDP_client(const std::string
& remote_address
, unsigned short remote_port
, unsigned short local_port
) :
33 initialise(remote_address
, remote_port
, local_port
);
36 // Send/Receive datagram packets to/from the given address/remote port on the given local port
37 // Enables default send to remote address/port
38 // - remote_address: IP address of remote host - pre-looked-up using ip_lookup
39 // - remote_port: port number of remote host
40 // - local_port: port number to receive on - 0 to get an ephemeral port.
41 UDP_client::UDP_client(unsigned long remote_address
, unsigned short remote_port
, unsigned short local_port
) :
44 initialise(remote_address
, remote_port
, local_port
);
47 // Send/Receive datagram packets to/from the given address/remote port on the local port.
48 // Enables default send to remote address/port
49 // - remote_address: IP name or number of remote host
50 // - remote_port: port number of remote host
51 // - local_port: port number to receive on - 0 to get an ephemeral port.
52 // - returns a success flag
53 bool UDP_client::initialise(const std::string
& address
, unsigned short remote_port
, unsigned short local_port
)
55 // lookup the address and convert it into an IP number
56 unsigned long remote_address
= IP_socket::ip_lookup(address
);
57 if (!remote_address
) return false;
58 return initialise(remote_address
, remote_port
, local_port
);
61 // Send/Receive datagram packets to/from the given address/remote port on the given local port
62 // Enables default send to remote address/port
63 // - remote_address: IP address of remote host - pre-looked-up using ip_lookup
64 // - remote_port: port number of remote host
65 // - local_port: port number to receive on - 0 to get an ephemeral port.
66 // - returns a success flag
67 bool UDP_client::initialise(unsigned long remote_address
, unsigned short remote_port
, unsigned short local_port
)
69 if (!IP_socket::bind(remote_address
, local_port
)) return false;
70 return IP_socket::connect(remote_address
, remote_port
);
73 // send to the remote address/port setup in initialise, from the local port also setup in initialise.
74 // send data through the socket as a single datagram
75 // - packet: string containing data to be sent - if data is successfully sent it is removed
76 // - returns success flag
77 bool UDP_client::send(std::string
& packet
)
79 return IP_socket::send_packet(packet
);
83 // - packet: string to receive data from datagram - if data is successfully sent it is appended
84 // - returns success flag - i.e. packet successfully received
85 bool UDP_client::receive(std::string
& packet
)
87 return IP_socket::receive_packet(packet
);
90 ////////////////////////////////////////////////////////////////////////////////
92 ////////////////////////////////////////////////////////////////////////////////
94 // create an uninitialised socket
95 UDP_server::UDP_server(void) : IP_socket(UDP
)
100 // Receive datagram packets from any address on provided local receiving port.
101 // No default send possible.
102 // - local_port: port number to receive on - 0 to get an ephemeral port.
103 UDP_server::UDP_server(unsigned short local_port
) : IP_socket(UDP
)
105 initialise(local_port
);
108 // Initialise socket.
109 // Receive datagram packets from any address on provided local receiving port.
110 // No default send possible.
111 // - local_port: port number to receive on - 0 to get an ephemeral port.
112 // - returns a success flag
113 bool UDP_server::initialise(unsigned short local_port
)
115 return IP_socket::bind_any(local_port
);
118 // send to the address/port given here, from the local port setup in initialise.
119 // send data through the socket as a single datagram
120 // - packet: string containing data to be sent - if data is successfully sent it is removed
121 // - remote_address: IP name (stlplus.sourceforge.net) or dotted number (216.34.181.96)
122 // - remote_port: port number of remote host
123 // - returns success flag
124 bool UDP_server::send(std::string
& packet
, const std::string
& remote_address
, unsigned short remote_port
)
126 unsigned long ip_address
= ip_lookup(remote_address
);
127 if (ip_address
== 0) return false;
128 return send(packet
, ip_address
, remote_port
);
131 // send to the address/port given here, from the local port setup in initialise.
132 // send data through the socket as a single datagram
133 // - packet: string containing data to be sent - if data is successfully sent it is removed
134 // - remote_address: pre-looked-up IP address of remote host
135 // - remote_port: port number of remote host
136 // - returns success flag
137 bool UDP_server::send(std::string
& packet
, unsigned long remote_address
, unsigned short remote_port
)
139 return IP_socket::send_packet(packet
, remote_address
, remote_port
);
143 // - packet: string to receive data from datagram - if data is successfully sent it is appended
144 // - remote_address: the address of the client that sent the packet, can then be used to reply
145 // - remote_port: the port of the client that sent the packet, can then be used to reply
146 // - returns success flag - i.e. packet successfully received
147 bool UDP_server::receive(std::string
& packet
, unsigned long& remote_address
, unsigned short& remote_port
)
149 return IP_socket::receive_packet(packet
, remote_address
, remote_port
);
152 /////////////////////////////////////////////////////////////////////////////
153 // fire and forget UDP client packet send function
154 ////////////////////////////////////////////////////////////////////////////////
156 bool UDP_send(const std::string
& packet
,
157 const std::string
& remote_address
, unsigned short remote_port
, unsigned short local_port
)
159 UDP_client
client(remote_address
, remote_port
, local_port
);
160 if (!client
.initialised()) return false;
161 std::string packet_copy
= packet
;
162 return client
.send(packet_copy
);
165 /////////////////////////////////////////////////////////////////////////////
167 } // end namespace stlplus
This page took 0.043925 seconds and 3 git commands to generate.