mbed socket API

Dependents:   EthernetInterface EthernetInterface_RSF EthernetInterface EthernetInterface ... more

Deprecated

This is an mbed 2 sockets library. For mbed 5, network sockets have been revised to better support additional network stacks and thread safety here.

Committer:
emilmont
Date:
Mon Jul 23 11:52:50 2012 +0000
Revision:
2:b227d242f3c7
Parent:
1:8080965f5d76
tidyup

Who changed what in which revision?

UserRevisionLine numberNew 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 TCPSOCKET_H
donatien 0:1f77255a22f5 20 #define TCPSOCKET_H
donatien 0:1f77255a22f5 21
emilmont 2:b227d242f3c7 22 #include "Socket/socket.h"
donatien 0:1f77255a22f5 23
donatien 0:1f77255a22f5 24 /**
donatien 0:1f77255a22f5 25 This is a C++ abstraction for TCP networking sockets.
donatien 0:1f77255a22f5 26 */
donatien 0:1f77255a22f5 27 class TCPSocket
donatien 0:1f77255a22f5 28 {
donatien 0:1f77255a22f5 29 public:
donatien 0:1f77255a22f5 30
donatien 0:1f77255a22f5 31 /** Instantiate a TCP Socket.
donatien 0:1f77255a22f5 32 */
donatien 0:1f77255a22f5 33 TCPSocket();
donatien 0:1f77255a22f5 34
donatien 0:1f77255a22f5 35 ~TCPSocket();
donatien 0:1f77255a22f5 36
donatien 0:1f77255a22f5 37 /** Connect the TCP Socket to the following host.
donatien 0:1f77255a22f5 38 \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS.
donatien 0:1f77255a22f5 39 \param port The host's port to connect to.
donatien 0:1f77255a22f5 40 \param timeout The maximum time in ms during which to try to connect.
donatien 0:1f77255a22f5 41 \return 0 on success, -1 on failure.
donatien 0:1f77255a22f5 42 */
donatien 0:1f77255a22f5 43 int connect(char* host, int port, int timeout = 0);
donatien 0:1f77255a22f5 44
donatien 0:1f77255a22f5 45 /** Bind a socket to a specific port.
donatien 0:1f77255a22f5 46 For a listening socket, bind the socket to the following port. The socket will start listening for incoming connections on this port on the call to listen().
donatien 0:1f77255a22f5 47 \param port The port to listen for incoming connections on.
donatien 0:1f77255a22f5 48 \return 0 on success, -1 on failure.
donatien 0:1f77255a22f5 49 */
donatien 0:1f77255a22f5 50 int bind(int port);
donatien 0:1f77255a22f5 51
donatien 0:1f77255a22f5 52 /** Start listening for incoming connections.
donatien 0:1f77255a22f5 53 \param max The maximum number of connections that can be accepted.
donatien 0:1f77255a22f5 54 \return 0 on success, -1 on failure.
donatien 0:1f77255a22f5 55 */
donatien 0:1f77255a22f5 56 int listen(int max);
donatien 0:1f77255a22f5 57
donatien 0:1f77255a22f5 58 /** Accept a new connection.
donatien 0:1f77255a22f5 59 \param socket A socket instance that will handle the incoming connection.
donatien 0:1f77255a22f5 60 \param host The reference that will point to the client's IP address.
donatien 0:1f77255a22f5 61 \param port The reference that will point to the client's port.
donatien 0:1f77255a22f5 62 \param timeout The maximum time in ms during which to wait for an incoming connection.
donatien 0:1f77255a22f5 63 \return 0 on success, -1 on failure.
donatien 0:1f77255a22f5 64 */
donatien 0:1f77255a22f5 65 int accept(TCPSocket& socket, char** host, int* port, int timeout = 0);
donatien 0:1f77255a22f5 66
donatien 0:1f77255a22f5 67 /** Send data to the remote host.
donatien 0:1f77255a22f5 68 \param data The buffer to send to the host.
donatien 0:1f77255a22f5 69 \param length The length of the buffer to send.
donatien 0:1f77255a22f5 70 \param timeout The maximum amount of time in ms to wait while trying to send the buffer.
donatien 0:1f77255a22f5 71 \return the number of written bytes on success (>=0) or -1 on failure
donatien 0:1f77255a22f5 72 */
donatien 1:8080965f5d76 73 int send(char* data, int length, int timeout = 0);
donatien 0:1f77255a22f5 74
donatien 0:1f77255a22f5 75 /** Receive data from the remote host.
donatien 0:1f77255a22f5 76 \param data The buffer in which to store the data received from the host.
donatien 0:1f77255a22f5 77 \param length The maximum length of the buffer.
donatien 0:1f77255a22f5 78 \param timeout The maximum amount of time in ms to wait while trying to receive data.
donatien 0:1f77255a22f5 79 \return the number of received bytes on success (>=0) or -1 on failure
donatien 0:1f77255a22f5 80 */
donatien 1:8080965f5d76 81 int receive(char* data, int length, int timeout = 0);
donatien 0:1f77255a22f5 82
donatien 0:1f77255a22f5 83 /** Close the socket.
donatien 0:1f77255a22f5 84 */
donatien 0:1f77255a22f5 85 int close();
donatien 0:1f77255a22f5 86
donatien 0:1f77255a22f5 87 private:
donatien 0:1f77255a22f5 88 int init();
donatien 0:1f77255a22f5 89
donatien 0:1f77255a22f5 90 int m_sock;
donatien 0:1f77255a22f5 91 bool m_closedByRemoteHost;
donatien 0:1f77255a22f5 92 struct sockaddr_in m_remoteHost;
donatien 0:1f77255a22f5 93
donatien 0:1f77255a22f5 94 };
donatien 0:1f77255a22f5 95
donatien 0:1f77255a22f5 96 #endif