Public Types | Public Member Functions | Protected Types | Protected Member Functions
sf::TcpListener Class Reference

Socket that listens to new TCP connections. More...

#include <TcpListener.hpp>

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

List of all members.

Public Types

enum  Status {
  Done,
  NotReady,
  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

 TcpListener ()
 Default constructor.
unsigned short GetLocalPort () const
 Get the port to which the socket is bound locally.
Status Listen (unsigned short port)
 Start listening for connections.
void Close ()
 Stop listening and close the socket.
Status Accept (TcpSocket &socket)
 Accept a new connection.
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.

Protected Types

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

Protected Member Functions

SocketHandle GetHandle () 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.

Detailed Description

Socket that listens to new TCP connections.

A listener socket is a special type of socket that listens to a given port and waits for connections on that port.

This is all it can do.

When a new connection is received, you must call Accept and the listener returns a new instance of sf::TcpSocket that is properly initialized and can be used to communicate with the new client.

Listener sockets are specific to the TCP protocol, UDP sockets are connectionless and can therefore communicate directly. As a consequence, a listener socket will always return the new connections as sf::TcpSocket instances.

A listener is automatically closed on destruction, like all other types of socket. However if you want to stop listening before the socket is destroyed, you can call its Close() function.

Usage example:

 // Create a listener socket and make it wait for new
 // connections on port 55001
 sf::TcpListener listener;
 listener.Listen(55001);

 // Endless loop that waits for new connections
 while (running)
 {
     sf::TcpSocket client;
     if (listener.Accept(client) == sf::Socket::Done)
     {
         // A new client just connected!
         std::cout << "New connection received from " << client.GetRemoteAddress() << std::endl;
         DoSomethingWith(client);
     }
 }
See also:
sf::TcpSocket, sf::Socket

Definition at line 42 of file TcpListener.hpp.


Member Enumeration Documentation

anonymous enum [inherited]

Some special values used by sockets.

Enumerator:
AnyPort 

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

Definition at line 64 of file Socket.hpp.

enum sf::Socket::Status [inherited]

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.

Disconnected 

The TCP socket has been disconnected.

Error 

An unexpected error happened.

Definition at line 52 of file Socket.hpp.

enum sf::Socket::Type [protected, inherited]

Types of protocols that the socket can use.

Enumerator:
Tcp 

TCP protocol.

Udp 

UDP protocol.

Definition at line 112 of file Socket.hpp.


Constructor & Destructor Documentation

sf::TcpListener::TcpListener ( )

Default constructor.


Member Function Documentation

Status sf::TcpListener::Accept ( TcpSocket socket)

Accept a new connection.

If the socket is in blocking mode, this function will not return until a connection is actually received.

Parameters:
socketSocket that will hold the new connection
Returns:
Status code
See also:
Listen
void sf::TcpListener::Close ( )

Stop listening and close the socket.

This function gracefully stops the listener. If the socket is not listening, this function has no effect.

See also:
Listen

Reimplemented from sf::Socket.

void sf::Socket::Create ( SocketHandle  handle) [protected, inherited]

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
void sf::Socket::Create ( ) [protected, inherited]

Create the internal representation of the socket.

This function can only be accessed by derived classes.

SocketHandle sf::Socket::GetHandle ( ) const [protected, inherited]

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
unsigned short sf::TcpListener::GetLocalPort ( ) const

Get the port to which the socket is bound locally.

If the socket is not listening to a port, this function returns 0.

Returns:
Port to which the socket is bound
See also:
Listen
bool sf::Socket::IsBlocking ( ) const [inherited]

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

Returns:
True if the socket is blocking, false otherwise
See also:
SetBlocking
Status sf::TcpListener::Listen ( unsigned short  port)

Start listening for connections.

This functions makes the socket listen to the specified port, waiting for new connections. If the socket was previously listening to another port, it will be stopped first and bound to the new port.

Parameters:
portPort to listen for new connections
Returns:
Status code
See also:
Accept, Close
void sf::Socket::SetBlocking ( bool  blocking) [inherited]

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: