.. index:: pair: class; EE::Network::TcpSocket .. _doxid-class_e_e_1_1_network_1_1_tcp_socket: class EE::Network::TcpSocket ============================ .. toctree:: :hidden: struct_EE_Network_TcpSocket_PendingPacket.rst Overview ~~~~~~~~ Specialized socket using the TCP protocol. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include class TcpSocket: public :ref:`EE::Network::Socket` { public: // typedefs typedef std::function :target:`ReadFn`; // structs struct :ref:`PendingPacket`; // construction virtual :target:`~TcpSocket`(); // methods static TcpSocket* :target:`New`(); unsigned short :ref:`getLocalPort`() const; :ref:`IpAddress` :ref:`getRemoteAddress`() const; unsigned short :ref:`getRemotePort`() const; virtual :ref:`Status` :ref:`connect`(const :ref:`IpAddress`& remoteAddress, unsigned short remotePort, :ref:`Time` timeout = Time::Zero); virtual void :ref:`disconnect`(); virtual :ref:`Status` :ref:`send`(const void* data, std::size_t size); virtual :ref:`Status` :ref:`send`(const void* data, std::size_t size, std::size_t& sent); virtual :ref:`Status` :ref:`receive`(void* data, std::size_t size, std::size_t& received); virtual :ref:`Status` :ref:`send`(:ref:`Packet`& packet); virtual :ref:`Status` :ref:`receive`(:ref:`Packet`& packet); void :ref:`setSendTimeout`(:ref:`SocketHandle` sock, const :ref:`Time`& timeout); void :ref:`setReceiveTimeout`(:ref:`SocketHandle` sock, const :ref:`Time`& timeout); void :ref:`startAsyncRead`(:ref:`ReadFn` readFn = nullptr); }; Inherited Members ----------------- .. ref-code-block:: cpp :class: doxyrest-overview-inherited-code-block public: // enums enum { :ref:`AnyPort` = 0, }; enum :ref:`Status`; // methods void :ref:`setBlocking`(bool blocking); bool :ref:`isBlocking`() const; .. _details-class_e_e_1_1_network_1_1_tcp_socket: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ Specialized socket using the TCP protocol. TCP is a connected protocol, which means that a TCP socket can only communicate with the host it is connected to. It can't send or receive anything if it is not connected. The TCP protocol is reliable but adds a slight overhead. It ensures that your data will always be received in order and without errors (no data corrupted, lost or duplicated). When a socket is connected to a remote host, you can retrieve informations about this host with the GetRemoteAddress and GetRemotePort functions. You can also get the local port to which the socket is bound (which is automatically chosen when the socket is connected), with the GetLocalPort function. Sending and receiving data can use either the low-level or the high-level functions. The low-level functions process a raw sequence of bytes, and cannot ensure that one call to Send will exactly match one call to Receive at the other end of the socket. The high-level interface uses packets (see :ref:`Packet `), which are easier to use and provide more safety regarding the data that is exchanged. You can look at the :ref:`Packet ` class to get more details about how they work. The socket is automatically disconnected when it is destroyed, but if you want to explicitely close the connection while the socket instance is still alive, you can call disconnect. Usage example: .. ref-code-block:: cpp // ----- The client ----- // Create a socket and connect it to 192.168.1.50 on port 55001 TcpSocket socket; socket.connect("192.168.1.50", 55001); // Send a message to the connected host std::string message = "Hi, I am a client"; socket.send(message.c_str(), message.size() + 1); // Receive an answer from the server char buffer[1024]; std::size_t received = 0; socket.receive(buffer, sizeof(buffer), received); std::cout << "The server said: " << buffer << std::endl; // ----- The server ----- // Create a listener to wait for incoming connections on port 55001 TcpListener listener; listener.listen(55001); // Wait for a connection TcpSocket socket; listener.accept(socket); std::cout << "New client connected: " << socket.getRemoteAddress() << std::endl; // Receive a message from the client char buffer[1024]; std::size_t received = 0; socket.receive(buffer, sizeof(buffer), received); std::cout << "The client said: " << buffer << std::endl; // Send an answer std::string message = "Welcome, client"; socket.send(message.c_str(), message.size() + 1); .. rubric:: See also: :ref:`EE::Network::Socket `, :ref:`EE::Network::UdpSocket `, :ref:`EE::Network::Packet ` Methods ------- .. index:: pair: function; getLocalPort .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1ab753ddc4bca52b78aeedeb70a210a42e: .. ref-code-block:: cpp :class: doxyrest-title-code-block unsigned short getLocalPort() const Get the port to which the socket is bound locally If the socket is not connected, this function returns 0. .. rubric:: Returns: Port to which the socket is bound .. rubric:: See also: Connect, GetRemotePort .. index:: pair: function; getRemoteAddress .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1afdb9824cce5028c46227a07931731979: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`IpAddress` getRemoteAddress() const Get the address of the connected peer It the socket is not connected, this function returns :ref:`IpAddress::None `. .. rubric:: Returns: Address of the remote peer .. rubric:: See also: GetRemotePort .. index:: pair: function; getRemotePort .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1ad64abfcfca997f297d106261d3f62ac1: .. ref-code-block:: cpp :class: doxyrest-title-code-block unsigned short getRemotePort() const Get the port of the connected peer to which the socket is connected If the socket is not connected, this function returns 0. .. rubric:: Returns: Remote port to which the socket is connected .. rubric:: See also: GetRemoteAddress .. index:: pair: function; connect .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1a95d7d98e4beb6ba344b6e4c4562975a7: .. ref-code-block:: cpp :class: doxyrest-title-code-block virtual :ref:`Status` connect(const :ref:`IpAddress`& remoteAddress, unsigned short remotePort, :ref:`Time` timeout = Time::Zero) Connect the socket to a remote peer In blocking mode, this function may take a while, especially if the remote peer is not reachable. The last parameter allows you to stop trying to connect after a given timeout. If the socket was previously connected, it is first disconnected. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - remoteAddress - Address of the remote peer * - remotePort - Port of the remote peer * - timeout - Optional maximum time to wait .. rubric:: Returns: Status code .. rubric:: See also: Disconnect .. index:: pair: function; disconnect .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1af161c8ba6c1fde4fd2fab9730a4a8325: .. ref-code-block:: cpp :class: doxyrest-title-code-block virtual void disconnect() Disconnect the socket from its remote peer This function gracefully closes the connection. If the socket is not connected, this function has no effect. .. rubric:: See also: Connect .. index:: pair: function; send .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1a9e450a4b00ebe7cb76ca07898e65cc87: .. ref-code-block:: cpp :class: doxyrest-title-code-block virtual :ref:`Status` send(const void* data, std::size_t size) Send raw data to the remote peer To be able to handle partial sends over non-blocking sockets, use the :ref:`send(const void\*, std::size_t, std::size_t&) ` overload instead. This function will fail if the socket is not connected. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - data - Pointer to the sequence of bytes to send * - size - Number of bytes to send .. rubric:: Returns: Status code .. rubric:: See also: Receive .. index:: pair: function; send .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1a0baabd1cf3ce4de131f19222e1b33b30: .. ref-code-block:: cpp :class: doxyrest-title-code-block virtual :ref:`Status` send(const void* data, std::size_t size, std::size_t& sent) Send raw data to the remote peer This function will fail if the socket is not connected. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - data - Pointer to the sequence of bytes to send * - size - Number of bytes to send * - sent - The number of bytes sent will be written here .. rubric:: Returns: Status code .. rubric:: See also: :ref:`receive ` .. index:: pair: function; receive .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1a1a0fe2da4596b016a0c2820dddf96790: .. ref-code-block:: cpp :class: doxyrest-title-code-block virtual :ref:`Status` receive(void* data, std::size_t size, std::size_t& received) Receive raw data from the remote peer In blocking mode, this function will wait until some bytes are actually received. This function will fail if the socket is not connected. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - data - Pointer to the array to fill with the received bytes * - size - Maximum number of bytes that can be received * - received - This variable is filled with the actual number of bytes received .. rubric:: Returns: Status code .. rubric:: See also: Send .. index:: pair: function; send .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1ac02d474bf09a7794f68f648a4db5ea15: .. ref-code-block:: cpp :class: doxyrest-title-code-block virtual :ref:`Status` send(:ref:`Packet`& packet) Send a formatted packet of data to the remote peer. In non-blocking mode, if this function returns sf::Socket::Partial, you *must* retry sending the same unmodified packet before sending anything else in order to guarantee the packet arrives at the remote peer uncorrupted. This function will fail if the socket is not connected. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - packet - :ref:`Packet ` to send .. rubric:: Returns: Status code .. rubric:: See also: Receive .. index:: pair: function; receive .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1a48a795313a35c49f7963ab3bc9586f74: .. ref-code-block:: cpp :class: doxyrest-title-code-block virtual :ref:`Status` receive(:ref:`Packet`& packet) Receive a formatted packet of data from the remote peer In blocking mode, this function will wait until the whole packet has been received. This function will fail if the socket is not connected. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - packet - :ref:`Packet ` to fill with the received data .. rubric:: Returns: Status code .. rubric:: See also: Send .. index:: pair: function; setSendTimeout .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1a23659a930fe214942dc1e01e9abdaeeb: .. ref-code-block:: cpp :class: doxyrest-title-code-block void setSendTimeout(:ref:`SocketHandle` sock, const :ref:`Time`& timeout) Set the send timeout. Only callable after connect ( after the socket has been initialized ). .. index:: pair: function; setReceiveTimeout .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1ab36cf242ac2bb1baad3601fbbdf993e4: .. ref-code-block:: cpp :class: doxyrest-title-code-block void setReceiveTimeout(:ref:`SocketHandle` sock, const :ref:`Time`& timeout) Set the receive timeout Only callable after connect ( after the socket has been initialized ). .. index:: pair: function; startAsyncRead .. _doxid-class_e_e_1_1_network_1_1_tcp_socket_1a8224923b4755c5dbc6f4ddf70b7f616c: .. ref-code-block:: cpp :class: doxyrest-title-code-block void startAsyncRead(:ref:`ReadFn` readFn = nullptr) Starts a new thread to receive all stdout and stderr data.