SFML - Simple and Fast Multimedia Library

SFML 3.1.0
Loading...
Searching...
No Matches
Socket.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2026 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
30#include <SFML/Network/Export.hpp>
31
32#include <SFML/Network/IpAddress.hpp>
33#include <SFML/Network/SocketHandle.hpp>
34
35
36namespace sf
37{
42 class SFML_NETWORK_API Socket
43{
44public:
49 enum class Status
50 {
51 Done,
52 NotReady,
53 Partial,
54 Disconnected,
55 Error
56 };
57
62 // NOLINTNEXTLINE(readability-identifier-naming)
63 static constexpr unsigned short AnyPort{0};
64
69 virtual ~Socket();
70
75 Socket(const Socket&) = delete;
76
81 Socket& operator=(const Socket&) = delete;
82
87 Socket(Socket&& socket) noexcept;
88
93 Socket& operator=(Socket&& socket) noexcept;
94
112 void setBlocking(bool blocking);
113
122 [[nodiscard]] bool isBlocking() const;
123
124protected:
129 enum class Type
130 {
131 Tcp,
132 Udp
133 };
134
143 explicit Socket(Type type);
144
155 [[nodiscard]] SocketHandle getNativeHandle() const;
156
165 void create(IpAddress::Type addressType = IpAddress::Type::IpV4);
166
176 void create(SocketHandle handle);
177
184 void close();
185
186private:
187 friend class SocketSelector;
188
190 // Member data
192 Type m_type;
193 SocketHandle m_socket;
194 bool m_isBlocking{true};
195};
196
197} // namespace sf
198
199
IpAddress.hpp
Export.hpp
SFML_NETWORK_API
#define SFML_NETWORK_API
Definition Network/Export.hpp:42
SocketHandle.hpp
sf::IpAddress::Type
Type
Type of IP address.
Definition IpAddress.hpp:58
sf::IpAddress::Type::IpV4
@ IpV4
IPv4 address.
Definition IpAddress.hpp:59
sf::Socket::setBlocking
void setBlocking(bool blocking)
Set the blocking state of the socket.
sf::Socket::AnyPort
static constexpr unsigned short AnyPort
Some special values used by sockets.
Definition Socket.hpp:63
sf::Socket::operator=
Socket & operator=(const Socket &)=delete
Deleted copy assignment.
sf::Socket::SocketSelector
friend class SocketSelector
Definition Socket.hpp:187
sf::Socket::Socket
Socket(Socket &&socket) noexcept
Move constructor.
sf::Socket::Status
Status
Status codes that may be returned by socket functions.
Definition Socket.hpp:50
sf::Socket::create
void create(IpAddress::Type addressType=IpAddress::Type::IpV4)
Create the internal representation of the socket.
sf::Socket::Type
Type
Types of protocols that the socket can use.
Definition Socket.hpp:130
sf::Socket::getNativeHandle
SocketHandle getNativeHandle() const
Return the internal handle of the socket.
sf::Socket::close
void close()
Close the socket gracefully.
sf::Socket::operator=
Socket & operator=(Socket &&socket) noexcept
Move assignment.
sf::Socket::~Socket
virtual ~Socket()
Destructor.
sf::Socket::Socket
Socket(Type type)
Default constructor.
sf::Socket::Socket
Socket(const Socket &)=delete
Deleted copy constructor.
sf::Socket::isBlocking
bool isBlocking() const
Tell whether the socket is in blocking or non-blocking mode.
sf::Socket::create
void create(SocketHandle handle)
Create the internal representation of the socket from a socket handle.
sf
Definition AudioResource.hpp:36
sf::SocketHandle
int SocketHandle
Definition SocketHandle.hpp:48