test
Diff: TCPSocket.h
- Revision:
- 0:1f77255a22f5
- Child:
- 1:8080965f5d76
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TCPSocket.h Tue Jun 26 13:16:26 2012 +0000 @@ -0,0 +1,98 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef TCPSOCKET_H +#define TCPSOCKET_H + +#include <cstdint> + +#include "bsd_socket.h" + +/** +This is a C++ abstraction for TCP networking sockets. +*/ +class TCPSocket +{ + public: + + /** Instantiate a TCP Socket. + */ + TCPSocket(); + + ~TCPSocket(); + + /** Connect the TCP Socket to the following host. + \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. + \param port The host's port to connect to. + \param timeout The maximum time in ms during which to try to connect. + \return 0 on success, -1 on failure. + */ + int connect(char* host, int port, int timeout = 0); + + /** Bind a socket to a specific port. + 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(). + \param port The port to listen for incoming connections on. + \return 0 on success, -1 on failure. + */ + int bind(int port); + + /** Start listening for incoming connections. + \param max The maximum number of connections that can be accepted. + \return 0 on success, -1 on failure. + */ + int listen(int max); + + /** Accept a new connection. + \param socket A socket instance that will handle the incoming connection. + \param host The reference that will point to the client's IP address. + \param port The reference that will point to the client's port. + \param timeout The maximum time in ms during which to wait for an incoming connection. + \return 0 on success, -1 on failure. + */ + int accept(TCPSocket& socket, char** host, int* port, int timeout = 0); + + /** Send data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \param timeout The maximum amount of time in ms to wait while trying to send the buffer. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send(std::uint8_t* data, int length, int timeout = 0); + + /** Receive data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \param timeout The maximum amount of time in ms to wait while trying to receive data. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive(std::uint8_t* data, int length, int timeout = 0); + + /** Close the socket. + */ + int close(); + + private: + int init(); + + int m_sock; + bool m_closedByRemoteHost; + struct sockaddr_in m_remoteHost; + +}; + +#endif