mbed socket API

Dependents:   EthernetInterface EthernetInterface_RSF EthernetInterface EthernetInterface ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.cpp Source File

UDPSocket.cpp

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "Socket/UDPSocket.h"
00020 
00021 #include <cstring>
00022 
00023 using std::memset;
00024 
00025 UDPSocket::UDPSocket() {
00026 }
00027 
00028 int UDPSocket::init(void) {
00029     return init_socket(SOCK_DGRAM);
00030 }
00031 
00032 // Server initialization
00033 int UDPSocket::bind(int port) {
00034     if (init_socket(SOCK_DGRAM) < 0)
00035         return -1;
00036     
00037     struct sockaddr_in localHost;
00038     std::memset(&localHost, 0, sizeof(localHost));
00039     
00040     localHost.sin_family = AF_INET;
00041     localHost.sin_port = htons(port);
00042     localHost.sin_addr.s_addr = INADDR_ANY;
00043     
00044     if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
00045         close();
00046         return -1;
00047     }
00048     
00049     return 0;
00050 }
00051 
00052 int UDPSocket::join_multicast_group(const char* address) {
00053     struct ip_mreq mreq;
00054     
00055     // Set up group address 
00056     mreq.imr_multiaddr.s_addr = inet_addr(address);
00057     mreq.imr_interface.s_addr = htonl(INADDR_ANY);
00058     
00059     return set_option(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
00060 }
00061 
00062 int UDPSocket::set_broadcasting(bool broadcast) {
00063     int option = (broadcast) ? (1) : (0);
00064     return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option));
00065 }
00066 
00067 // -1 if unsuccessful, else number of bytes written
00068 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
00069     if (_sock_fd < 0)
00070         return -1;
00071     
00072     if (!_blocking) {
00073         TimeInterval timeout(_timeout);
00074         if (wait_writable(timeout) != 0)
00075             return 0;
00076     }
00077     
00078     return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost));
00079 }
00080 
00081 // -1 if unsuccessful, else number of bytes received
00082 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
00083     if (_sock_fd < 0)
00084         return -1;
00085     
00086     if (!_blocking) {
00087         TimeInterval timeout(_timeout);
00088         if (wait_readable(timeout) != 0)
00089             return 0;
00090     }
00091     remote.reset_address();
00092     socklen_t remoteHostLen = sizeof(remote._remoteHost);
00093     return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
00094 }