Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Socket by
UDPSocket.h@10:c228f2097b40, 2012-07-31 (annotated)
- Committer:
- nkhorman
- Date:
- Tue Jul 31 04:01:42 2012 +0000
- Revision:
- 10:c228f2097b40
- Parent:
- 6:cd2e5559786d
- Child:
- 8:9cf9c2d45264
Add shutdown() method to Socket.; Declare Socket as public for TCPSocketServer.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| donatien | 0:1f77255a22f5 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
| donatien | 0:1f77255a22f5 | 2 | * |
| donatien | 0:1f77255a22f5 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| donatien | 0:1f77255a22f5 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| donatien | 0:1f77255a22f5 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| donatien | 0:1f77255a22f5 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| donatien | 0:1f77255a22f5 | 7 | * furnished to do so, subject to the following conditions: |
| donatien | 0:1f77255a22f5 | 8 | * |
| donatien | 0:1f77255a22f5 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
| donatien | 0:1f77255a22f5 | 10 | * substantial portions of the Software. |
| donatien | 0:1f77255a22f5 | 11 | * |
| donatien | 0:1f77255a22f5 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| donatien | 0:1f77255a22f5 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| donatien | 0:1f77255a22f5 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| donatien | 0:1f77255a22f5 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| donatien | 0:1f77255a22f5 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| donatien | 0:1f77255a22f5 | 17 | */ |
| donatien | 0:1f77255a22f5 | 18 | |
| donatien | 0:1f77255a22f5 | 19 | #ifndef UDPSOCKET_H |
| donatien | 0:1f77255a22f5 | 20 | #define UDPSOCKET_H |
| donatien | 0:1f77255a22f5 | 21 | |
| emilmont | 3:e6474399e057 | 22 | #include "Socket/Socket.h" |
| emilmont | 5:300e7ad2dc1d | 23 | #include "Socket/UDPPacket.h" |
| donatien | 0:1f77255a22f5 | 24 | #include <cstdint> |
| donatien | 0:1f77255a22f5 | 25 | |
| donatien | 0:1f77255a22f5 | 26 | /** |
| donatien | 0:1f77255a22f5 | 27 | This is a C++ abstraction for UDP networking sockets. |
| donatien | 0:1f77255a22f5 | 28 | */ |
| emilmont | 3:e6474399e057 | 29 | class UDPSocket : public Socket { |
| emilmont | 5:300e7ad2dc1d | 30 | |
| emilmont | 5:300e7ad2dc1d | 31 | public: |
| donatien | 0:1f77255a22f5 | 32 | /** Instantiate a UDP Socket. |
| donatien | 0:1f77255a22f5 | 33 | */ |
| donatien | 0:1f77255a22f5 | 34 | UDPSocket(); |
| donatien | 0:1f77255a22f5 | 35 | |
| donatien | 0:1f77255a22f5 | 36 | ~UDPSocket(); |
| emilmont | 3:e6474399e057 | 37 | |
| emilmont | 5:300e7ad2dc1d | 38 | int init(void); |
| emilmont | 5:300e7ad2dc1d | 39 | |
| emilmont | 3:e6474399e057 | 40 | /** Bind a UDP Server Socket to a specific port |
| donatien | 0:1f77255a22f5 | 41 | For a listening socket, bind the socket to the following port. |
| donatien | 0:1f77255a22f5 | 42 | \param port The port to listen for incoming connections on, using 0 here will select a random port. |
| donatien | 0:1f77255a22f5 | 43 | \return 0 on success, -1 on failure. |
| donatien | 0:1f77255a22f5 | 44 | */ |
| donatien | 0:1f77255a22f5 | 45 | int bind(int port); |
| emilmont | 5:300e7ad2dc1d | 46 | |
| emilmont | 5:300e7ad2dc1d | 47 | /** Send a packet to the remote host. |
| emilmont | 5:300e7ad2dc1d | 48 | \param packet UDP Packet to be sent to the remote host |
| donatien | 0:1f77255a22f5 | 49 | \param timeout The maximum amount of time in ms to wait while trying to send the buffer. |
| donatien | 0:1f77255a22f5 | 50 | \return the number of written bytes on success (>=0) or -1 on failure |
| donatien | 0:1f77255a22f5 | 51 | */ |
| emilmont | 6:cd2e5559786d | 52 | int sendTo(UDPPacket& packet, int timeout_ms=0); |
| emilmont | 5:300e7ad2dc1d | 53 | |
| donatien | 0:1f77255a22f5 | 54 | /** Receive data from a remote host. |
| emilmont | 5:300e7ad2dc1d | 55 | If a message is too long to fit in the supplied packet, excess bytes are |
| emilmont | 5:300e7ad2dc1d | 56 | discarded. |
| emilmont | 5:300e7ad2dc1d | 57 | \param packet UDP Packet received from the remote host |
| donatien | 0:1f77255a22f5 | 58 | \param timeout The maximum amount of time in ms to wait while trying to receive data. |
| donatien | 0:1f77255a22f5 | 59 | \return the number of received bytes on success (>=0) or -1 on failure |
| donatien | 0:1f77255a22f5 | 60 | */ |
| emilmont | 6:cd2e5559786d | 61 | int receiveFrom(UDPPacket& packet, int timeout_ms=0); |
| donatien | 0:1f77255a22f5 | 62 | }; |
| donatien | 0:1f77255a22f5 | 63 | |
| donatien | 0:1f77255a22f5 | 64 | #endif |
