Onsens  1.0
This is C++ game about bitwise logic.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sf::TcpListener Class Reference

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

#include <TcpListener.hpp>

+ Inheritance diagram for sf::TcpListener:
+ Collaboration diagram for sf::TcpListener:

Public Member Functions

 TcpListener ()
 Default constructor. More...
 
unsigned short getLocalPort () const
 Get the port to which the socket is bound locally. More...
 
Status listen (unsigned short port, const IpAddress &address=IpAddress::Any)
 Start listening for incoming connection attempts. More...
 
void close ()
 Stop listening and close the socket. More...
 
Status accept (TcpSocket &socket)
 Accept a new connection. More...
 
- Public Member Functions inherited from sf::Socket
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...
 

Additional Inherited Members

- Public Types inherited from sf::Socket
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...
 
- Protected Types inherited from sf::Socket
enum  Type { Tcp , Udp }
 Types of protocols that the socket can use. More...
 
- Protected Member Functions inherited from sf::Socket
 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...
 

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);
}
}
@ Done
The socket has sent / received the data.
Definition: Socket.hpp:55
Socket that listens to new TCP connections.
Definition: TcpListener.hpp:45
Status listen(unsigned short port, const IpAddress &address=IpAddress::Any)
Start listening for incoming connection attempts.
Status accept(TcpSocket &socket)
Accept a new connection.
Specialized socket using the TCP protocol.
Definition: TcpSocket.hpp:47
IpAddress getRemoteAddress() const
Get the address of the connected peer.
See also
sf::TcpSocket, sf::Socket

Definition at line 44 of file TcpListener.hpp.

Constructor & Destructor Documentation

◆ TcpListener()

sf::TcpListener::TcpListener ( )

Default constructor.

Member Function Documentation

◆ accept()

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

◆ close()

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

◆ getLocalPort()

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

◆ listen()

Status sf::TcpListener::listen ( unsigned short  port,
const IpAddress address = IpAddress::Any 
)

Start listening for incoming connection attempts.

This function makes the socket start listening on the specified port, waiting for incoming connection attempts.

If the socket is already listening on a port when this function is called, it will stop listening on the old port before starting to listen on the new port.

Parameters
portPort to listen on for incoming connection attempts
addressAddress of the interface to listen on
Returns
Status code
See also
accept, close

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