Documentation de SFML 2.5.1

Attention: cette page se réfère à une ancienne version de SFML. Cliquez ici pour passer à la dernière version.
sf::Socket Class Reference

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

#include <Socket.hpp>

Inheritance diagram for sf::Socket:
sf::NonCopyable sf::TcpListener sf::TcpSocket sf::UdpSocket

Public Types

enum  Status {
  Done, NotReady, Partial, Disconnected,
  Error
}
 Status codes that may be returned by socket functions. More...
 
enum  { AnyPort = 0 }
 Some special values used by sockets. More...
 

Public Member Functions

virtual ~Socket ()
 Destructor. More...
 
void setBlocking (bool blocking)
 Set the blocking state of the socket. More...
 
bool isBlocking () const
 Tell whether the socket is in blocking or non-blocking mode. More...
 

Protected Types

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

Protected Member Functions

 Socket (Type type)
 Default constructor. More...
 
SocketHandle getHandle () const
 Return the internal handle of the socket. More...
 
void create ()
 Create the internal representation of the socket. More...
 
void create (SocketHandle handle)
 Create the internal representation of the socket from a socket handle. More...
 
void close ()
 Close the socket gracefully. More...
 

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::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 45 of file Socket.hpp.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum

Some special values used by sockets.

Enumerator
AnyPort 

Special value that tells the system to pick any available port.

Definition at line 66 of file Socket.hpp.

◆ Status

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 53 of file Socket.hpp.

◆ Type

enum sf::Socket::Type
protected

Types of protocols that the socket can use.

Enumerator
Tcp 

TCP protocol.

Udp 

UDP protocol.

Definition at line 114 of file Socket.hpp.

Constructor & Destructor Documentation

◆ ~Socket()

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

Destructor.

◆ Socket()

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

Default constructor.

This constructor can only be accessed by derived classes.

Parameters
typeType 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
handleOS-specific handle of the socket to wrap

◆ getHandle()

SocketHandle sf::Socket::getHandle ( ) const
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

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

Returns
True if the socket is blocking, false otherwise
See also
setBlocking

◆ 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
blockingTrue to set the socket as blocking, false for non-blocking
See also
isBlocking

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