Class OcsfmlNetwork.tcp_listener

class tcp_listener : object .. end
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 tcp_socket 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 *)

    let listener = new tcp_listener in
    listener#listen (Port.from_int 55001) 
    
    (* Endless loop that waits for new connections *)
    let rec connection_loop running =
      if running
      then 
        let client = new tcp_socket in
        if  listener#accept(client) = Done
        then begin
          (* A new client just connected! *)
          print_endline ("New connection received from " ^ 
                         client#get_remote_address#to_string) ;
          do_something_with client
        end ;
        connection_loop ...
    in connection_loop true
    

Inherits
method accept : tcp_socket -> socket_status
Accept a new connection.

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

method close : unit
Stop listening and close the socket.

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

method destroy : unit
method get_local_port : Port.t
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

method listen : Port.t -> socket_status
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.
Returns Status code