Socket that listens to new TCP connections.
1 // Create a listener socket and make it wait for new 2 // connections on port 55001 3 auto listener = new TcpListener(); 4 listener.listen(55001); 5 6 // Endless loop that waits for new connections 7 while (running) 8 { 9 auto client = new TcpSocket(); 10 if (listener.accept(client) == Socket.Status.Done) 11 { 12 // A new client just connected! 13 writeln("New connection received from ", client.getRemoteAddress()); 14 doSomethingWith(client); 15 } 16 }
$(TCPSOCKET_LINK), $(SOCKET_LINK)
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 $(TCPSOCKET_LINK) 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 $(TCPSOCKET_LINK) 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.