|
|
|||
File indexing completed on 2026-06-02 08:58:14
0001 //////////////////////////////////////////////////////////// 0002 // 0003 // SFML - Simple and Fast Multimedia Library 0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org) 0005 // 0006 // This software is provided 'as-is', without any express or implied warranty. 0007 // In no event will the authors be held liable for any damages arising from the use of this software. 0008 // 0009 // Permission is granted to anyone to use this software for any purpose, 0010 // including commercial applications, and to alter it and redistribute it freely, 0011 // subject to the following restrictions: 0012 // 0013 // 1. The origin of this software must not be misrepresented; 0014 // you must not claim that you wrote the original software. 0015 // If you use this software in a product, an acknowledgment 0016 // in the product documentation would be appreciated but is not required. 0017 // 0018 // 2. Altered source versions must be plainly marked as such, 0019 // and must not be misrepresented as being the original software. 0020 // 0021 // 3. This notice may not be removed or altered from any source distribution. 0022 // 0023 //////////////////////////////////////////////////////////// 0024 0025 #ifndef SFML_TCPSOCKET_HPP 0026 #define SFML_TCPSOCKET_HPP 0027 0028 //////////////////////////////////////////////////////////// 0029 // Headers 0030 //////////////////////////////////////////////////////////// 0031 #include <SFML/Network/Export.hpp> 0032 #include <SFML/Network/Socket.hpp> 0033 #include <SFML/System/Time.hpp> 0034 0035 0036 namespace sf 0037 { 0038 class TcpListener; 0039 class IpAddress; 0040 class Packet; 0041 0042 //////////////////////////////////////////////////////////// 0043 /// \brief Specialized socket using the TCP protocol 0044 /// 0045 //////////////////////////////////////////////////////////// 0046 class SFML_NETWORK_API TcpSocket : public Socket 0047 { 0048 public: 0049 0050 //////////////////////////////////////////////////////////// 0051 /// \brief Default constructor 0052 /// 0053 //////////////////////////////////////////////////////////// 0054 TcpSocket(); 0055 0056 //////////////////////////////////////////////////////////// 0057 /// \brief Get the port to which the socket is bound locally 0058 /// 0059 /// If the socket is not connected, this function returns 0. 0060 /// 0061 /// \return Port to which the socket is bound 0062 /// 0063 /// \see connect, getRemotePort 0064 /// 0065 //////////////////////////////////////////////////////////// 0066 unsigned short getLocalPort() const; 0067 0068 //////////////////////////////////////////////////////////// 0069 /// \brief Get the address of the connected peer 0070 /// 0071 /// If the socket is not connected, this function returns 0072 /// sf::IpAddress::None. 0073 /// 0074 /// \return Address of the remote peer 0075 /// 0076 /// \see getRemotePort 0077 /// 0078 //////////////////////////////////////////////////////////// 0079 IpAddress getRemoteAddress() const; 0080 0081 //////////////////////////////////////////////////////////// 0082 /// \brief Get the port of the connected peer to which 0083 /// the socket is connected 0084 /// 0085 /// If the socket is not connected, this function returns 0. 0086 /// 0087 /// \return Remote port to which the socket is connected 0088 /// 0089 /// \see getRemoteAddress 0090 /// 0091 //////////////////////////////////////////////////////////// 0092 unsigned short getRemotePort() const; 0093 0094 //////////////////////////////////////////////////////////// 0095 /// \brief Connect the socket to a remote peer 0096 /// 0097 /// In blocking mode, this function may take a while, especially 0098 /// if the remote peer is not reachable. The last parameter allows 0099 /// you to stop trying to connect after a given timeout. 0100 /// If the socket is already connected, the connection is 0101 /// forcibly disconnected before attempting to connect again. 0102 /// 0103 /// \param remoteAddress Address of the remote peer 0104 /// \param remotePort Port of the remote peer 0105 /// \param timeout Optional maximum time to wait 0106 /// 0107 /// \return Status code 0108 /// 0109 /// \see disconnect 0110 /// 0111 //////////////////////////////////////////////////////////// 0112 Status connect(const IpAddress& remoteAddress, unsigned short remotePort, Time timeout = Time::Zero); 0113 0114 //////////////////////////////////////////////////////////// 0115 /// \brief Disconnect the socket from its remote peer 0116 /// 0117 /// This function gracefully closes the connection. If the 0118 /// socket is not connected, this function has no effect. 0119 /// 0120 /// \see connect 0121 /// 0122 //////////////////////////////////////////////////////////// 0123 void disconnect(); 0124 0125 //////////////////////////////////////////////////////////// 0126 /// \brief Send raw data to the remote peer 0127 /// 0128 /// To be able to handle partial sends over non-blocking 0129 /// sockets, use the send(const void*, std::size_t, std::size_t&) 0130 /// overload instead. 0131 /// This function will fail if the socket is not connected. 0132 /// 0133 /// \param data Pointer to the sequence of bytes to send 0134 /// \param size Number of bytes to send 0135 /// 0136 /// \return Status code 0137 /// 0138 /// \see receive 0139 /// 0140 //////////////////////////////////////////////////////////// 0141 Status send(const void* data, std::size_t size); 0142 0143 //////////////////////////////////////////////////////////// 0144 /// \brief Send raw data to the remote peer 0145 /// 0146 /// This function will fail if the socket is not connected. 0147 /// 0148 /// \param data Pointer to the sequence of bytes to send 0149 /// \param size Number of bytes to send 0150 /// \param sent The number of bytes sent will be written here 0151 /// 0152 /// \return Status code 0153 /// 0154 /// \see receive 0155 /// 0156 //////////////////////////////////////////////////////////// 0157 Status send(const void* data, std::size_t size, std::size_t& sent); 0158 0159 //////////////////////////////////////////////////////////// 0160 /// \brief Receive raw data from the remote peer 0161 /// 0162 /// In blocking mode, this function will wait until some 0163 /// bytes are actually received. 0164 /// This function will fail if the socket is not connected. 0165 /// 0166 /// \param data Pointer to the array to fill with the received bytes 0167 /// \param size Maximum number of bytes that can be received 0168 /// \param received This variable is filled with the actual number of bytes received 0169 /// 0170 /// \return Status code 0171 /// 0172 /// \see send 0173 /// 0174 //////////////////////////////////////////////////////////// 0175 Status receive(void* data, std::size_t size, std::size_t& received); 0176 0177 //////////////////////////////////////////////////////////// 0178 /// \brief Send a formatted packet of data to the remote peer 0179 /// 0180 /// In non-blocking mode, if this function returns sf::Socket::Partial, 0181 /// you \em must retry sending the same unmodified packet before sending 0182 /// anything else in order to guarantee the packet arrives at the remote 0183 /// peer uncorrupted. 0184 /// This function will fail if the socket is not connected. 0185 /// 0186 /// \param packet Packet to send 0187 /// 0188 /// \return Status code 0189 /// 0190 /// \see receive 0191 /// 0192 //////////////////////////////////////////////////////////// 0193 Status send(Packet& packet); 0194 0195 //////////////////////////////////////////////////////////// 0196 /// \brief Receive a formatted packet of data from the remote peer 0197 /// 0198 /// In blocking mode, this function will wait until the whole packet 0199 /// has been received. 0200 /// This function will fail if the socket is not connected. 0201 /// 0202 /// \param packet Packet to fill with the received data 0203 /// 0204 /// \return Status code 0205 /// 0206 /// \see send 0207 /// 0208 //////////////////////////////////////////////////////////// 0209 Status receive(Packet& packet); 0210 0211 private: 0212 0213 friend class TcpListener; 0214 0215 //////////////////////////////////////////////////////////// 0216 /// \brief Structure holding the data of a pending packet 0217 /// 0218 //////////////////////////////////////////////////////////// 0219 struct PendingPacket 0220 { 0221 PendingPacket(); 0222 0223 Uint32 Size; //!< Data of packet size 0224 std::size_t SizeReceived; //!< Number of size bytes received so far 0225 std::vector<char> Data; //!< Data of the packet 0226 }; 0227 0228 //////////////////////////////////////////////////////////// 0229 // Member data 0230 //////////////////////////////////////////////////////////// 0231 PendingPacket m_pendingPacket; //!< Temporary data of the packet currently being received 0232 }; 0233 0234 } // namespace sf 0235 0236 0237 #endif // SFML_TCPSOCKET_HPP 0238 0239 0240 //////////////////////////////////////////////////////////// 0241 /// \class sf::TcpSocket 0242 /// \ingroup network 0243 /// 0244 /// TCP is a connected protocol, which means that a TCP 0245 /// socket can only communicate with the host it is connected 0246 /// to. It can't send or receive anything if it is not connected. 0247 /// 0248 /// The TCP protocol is reliable but adds a slight overhead. 0249 /// It ensures that your data will always be received in order 0250 /// and without errors (no data corrupted, lost or duplicated). 0251 /// 0252 /// When a socket is connected to a remote host, you can 0253 /// retrieve informations about this host with the 0254 /// getRemoteAddress and getRemotePort functions. You can 0255 /// also get the local port to which the socket is bound 0256 /// (which is automatically chosen when the socket is connected), 0257 /// with the getLocalPort function. 0258 /// 0259 /// Sending and receiving data can use either the low-level 0260 /// or the high-level functions. The low-level functions 0261 /// process a raw sequence of bytes, and cannot ensure that 0262 /// one call to Send will exactly match one call to Receive 0263 /// at the other end of the socket. 0264 /// 0265 /// The high-level interface uses packets (see sf::Packet), 0266 /// which are easier to use and provide more safety regarding 0267 /// the data that is exchanged. You can look at the sf::Packet 0268 /// class to get more details about how they work. 0269 /// 0270 /// The socket is automatically disconnected when it is destroyed, 0271 /// but if you want to explicitly close the connection while 0272 /// the socket instance is still alive, you can call disconnect. 0273 /// 0274 /// Usage example: 0275 /// \code 0276 /// // ----- The client ----- 0277 /// 0278 /// // Create a socket and connect it to 192.168.1.50 on port 55001 0279 /// sf::TcpSocket socket; 0280 /// socket.connect("192.168.1.50", 55001); 0281 /// 0282 /// // Send a message to the connected host 0283 /// std::string message = "Hi, I am a client"; 0284 /// socket.send(message.c_str(), message.size() + 1); 0285 /// 0286 /// // Receive an answer from the server 0287 /// char buffer[1024]; 0288 /// std::size_t received = 0; 0289 /// socket.receive(buffer, sizeof(buffer), received); 0290 /// std::cout << "The server said: " << buffer << std::endl; 0291 /// 0292 /// // ----- The server ----- 0293 /// 0294 /// // Create a listener to wait for incoming connections on port 55001 0295 /// sf::TcpListener listener; 0296 /// listener.listen(55001); 0297 /// 0298 /// // Wait for a connection 0299 /// sf::TcpSocket socket; 0300 /// listener.accept(socket); 0301 /// std::cout << "New client connected: " << socket.getRemoteAddress() << std::endl; 0302 /// 0303 /// // Receive a message from the client 0304 /// char buffer[1024]; 0305 /// std::size_t received = 0; 0306 /// socket.receive(buffer, sizeof(buffer), received); 0307 /// std::cout << "The client said: " << buffer << std::endl; 0308 /// 0309 /// // Send an answer 0310 /// std::string message = "Welcome, client"; 0311 /// socket.send(message.c_str(), message.size() + 1); 0312 /// \endcode 0313 /// 0314 /// \see sf::Socket, sf::UdpSocket, sf::Packet 0315 /// 0316 ////////////////////////////////////////////////////////////
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|