SFML 3.0.1
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Friends | List of all members
sf::Socket Class Reference
Network module

Base class for all the socket types. More...

`#include <SFML/Network/Socket.hpp>`

Inheritance diagram for sf::Socket:

## Public Types
enum class | Status { Done , NotReady , Partial , Disconnected , Error }
| Status codes that may be returned by socket functions. More...

## Public Member Functions
virtual | ~Socket ()
| Destructor.
| Socket (const Socket &)=delete
| Deleted copy constructor.
Socket & | operator= (const Socket &)=delete
| Deleted copy assignment.
| Socket (Socket &&socket) noexcept
| Move constructor.
Socket & | operator= (Socket &&socket) noexcept
| Move assignment.
void | setBlocking (bool blocking)
| Set the blocking state of the socket.
bool | isBlocking () const
| Tell whether the socket is in blocking or non-blocking mode.

## Static Public Attributes
static constexpr unsigned short | AnyPort {0}
| Some special values used by sockets.

## Protected Types
enum class | Type { Tcp , Udp }
| Types of protocols that the socket can use. More...

## Protected Member Functions
| Socket (Type type)
| Default constructor.
SocketHandle | getNativeHandle () const
| Return the internal handle of the socket.
void | create ()
| Create the internal representation of the socket.
void | create (SocketHandle handle)
| Create the internal representation of the socket from a socket handle.
void | close ()
| Close the socket gracefully.

## Friends
class | SocketSelector

## Detailed Description

Base class for all the socket types.

This class mainly defines internal stuff to be used by derived classes.

The only public features that it defines, and which is therefore common to all the socket classes, is the blocking state. All sockets can be set as blocking or non-blocking.

In blocking mode, socket functions will hang until the operation completes, which means that the entire program (well, in fact the current thread if you use multiple ones) will be stuck waiting for your socket operation to complete.

In non-blocking mode, all the socket functions will return immediately. If the socket is not ready to complete the requested operation, the function simply returns the proper status code (Socket::Status::NotReady).

The default mode, which is blocking, is the one that is generally used, in combination with threads or selectors. The non-blocking mode is rather used in real-time applications that run an endless loop that can poll the socket often enough, and cannot afford blocking this loop.

See also
  sf::TcpListener, sf::TcpSocket, sf::UdpSocket

Definition at line 41 of file Socket.hpp.

## Member Enumeration Documentation

## ◆ Status

enum class sf::Socket::Status
| strong

Status codes that may be returned by socket functions.

Enumerator
Done | The socket has sent / received the data.
NotReady | The socket is not ready to send / receive data yet.
Partial | The socket sent a part of the data.
Disconnected | The TCP socket has been disconnected.
Error | An unexpected error happened.

Definition at line 48 of file Socket.hpp.

## ◆ Type

enum class sf::Socket::Type
| strong protected

Types of protocols that the socket can use.

Enumerator
Tcp | TCP protocol.
Udp | UDP protocol.

Definition at line 128 of file Socket.hpp.

## Constructor & Destructor Documentation

## ◆ ~Socket()

virtual sf::Socket::~Socket | ( |  | ) |
| virtual

Destructor.

## ◆ Socket() [1/3]

sf::Socket::Socket | ( | const Socket & |  | ) |
| delete

Deleted copy constructor.

## ◆ Socket() [2/3]

sf::Socket::Socket | ( | Socket && | socket | ) |
| noexcept

Move constructor.

## ◆ Socket() [3/3]

sf::Socket::Socket | ( | Type | type | ) |
| explicit protected

Default constructor.

This constructor can only be accessed by derived classes.

Parameters

  type | Type of the socket (TCP or UDP)

## Member Function Documentation

## ◆ close()

void sf::Socket::close | ( |  | ) |
| protected

Close the socket gracefully.

This function can only be accessed by derived classes.

## ◆ create() [1/2]

void sf::Socket::create | ( |  | ) |
| protected

Create the internal representation of the socket.

This function can only be accessed by derived classes.

## ◆ create() [2/2]

void sf::Socket::create | ( | SocketHandle | handle | ) |
| protected

Create the internal representation of the socket from a socket handle.

This function can only be accessed by derived classes.

Parameters

  handle | OS-specific handle of the socket to wrap

## ◆ getNativeHandle()

SocketHandle sf::Socket::getNativeHandle | ( |  | ) | const
| nodiscard protected

Return the internal handle of the socket.

The returned handle may be invalid if the socket was not created yet (or already destroyed). This function can only be accessed by derived classes.

Returns
  The internal (OS-specific) handle of the socket

## ◆ isBlocking()

bool sf::Socket::isBlocking | ( |  | ) | const
| nodiscard

Tell whether the socket is in blocking or non-blocking mode.

Returns
  true if the socket is blocking, false otherwise

See also
  setBlocking

## ◆ operator=() [1/2]

Socket & sf::Socket::operator= | ( | const Socket & |  | ) |
| delete

Deleted copy assignment.

## ◆ operator=() [2/2]

Socket & sf::Socket::operator= | ( | Socket && | socket | ) |
| noexcept

Move assignment.

## ◆ setBlocking()

void sf::Socket::setBlocking | ( | bool | blocking | ) |

Set the blocking state of the socket.

In blocking mode, calls will not return until they have completed their task. For example, a call to Receive in blocking mode won't return until some data was actually received. In non-blocking mode, calls will always return immediately, using the return code to signal whether there was data available or not. By default, all sockets are blocking.

Parameters

  blocking | true to set the socket as blocking, false for non-blocking

See also
  isBlocking

## Friends And Related Symbol Documentation

## ◆ SocketSelector

friend class SocketSelector
| friend

Definition at line 184 of file Socket.hpp.

## Member Data Documentation

## ◆ AnyPort

unsigned short sf::Socket::AnyPort {0}
| static constexpr

Some special values used by sockets.

Special value that tells the system to pick any available port

Definition at line 62 of file Socket.hpp.

The documentation for this class was generated from the following file:

- Socket.hpp