From 9bdd6d46ddda34bd3dce72cd839dbc4bfff211ee Mon Sep 17 00:00:00 2001 From: binary1248 Date: Sun, 8 Apr 2018 04:42:56 +0200 Subject: [PATCH] Fixed TcpListener and TcpSocket not behaving as documented when calling listen or connect while the underlying socket object already exists, also adjusted UdpSocket to be consistent with connect and listen behaviour when calling bind while the underlying socket object already exists. Fixes #1346 --- include/SFML/Network/TcpListener.hpp | 14 ++++++++------ include/SFML/Network/TcpSocket.hpp | 3 ++- include/SFML/Network/UdpSocket.hpp | 5 +++++ src/SFML/Network/TcpListener.cpp | 3 +++ src/SFML/Network/TcpSocket.cpp | 3 +++ src/SFML/Network/UdpSocket.cpp | 3 +++ 6 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/SFML/Network/TcpListener.hpp b/include/SFML/Network/TcpListener.hpp index 840a7cde..e0e05d64 100644 --- a/include/SFML/Network/TcpListener.hpp +++ b/include/SFML/Network/TcpListener.hpp @@ -65,14 +65,16 @@ public: unsigned short getLocalPort() const; //////////////////////////////////////////////////////////// - /// \brief Start listening for connections + /// \brief Start listening for incoming connection attempts /// - /// 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. + /// This function makes the socket start listening on the + /// specified port, waiting for incoming connection attempts. /// - /// \param port Port to listen for new connections + /// 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. + /// + /// \param port Port to listen on for incoming connection attempts /// \param address Address of the interface to listen on /// /// \return Status code diff --git a/include/SFML/Network/TcpSocket.hpp b/include/SFML/Network/TcpSocket.hpp index 6c0c8bc7..6c0ab98a 100644 --- a/include/SFML/Network/TcpSocket.hpp +++ b/include/SFML/Network/TcpSocket.hpp @@ -97,7 +97,8 @@ public: /// In blocking mode, this function may take a while, especially /// if the remote peer is not reachable. The last parameter allows /// you to stop trying to connect after a given timeout. - /// If the socket was previously connected, it is first disconnected. + /// If the socket is already connected, the connection is + /// forcibly disconnected before attempting to connect again. /// /// \param remoteAddress Address of the remote peer /// \param remotePort Port of the remote peer diff --git a/include/SFML/Network/UdpSocket.hpp b/include/SFML/Network/UdpSocket.hpp index 20e35b8b..7ce3fe55 100644 --- a/include/SFML/Network/UdpSocket.hpp +++ b/include/SFML/Network/UdpSocket.hpp @@ -82,6 +82,11 @@ public: /// system to automatically pick an available port, and then /// call getLocalPort to retrieve the chosen port. /// + /// Since the socket can only be bound to a single port at + /// any given moment, if it is already bound when this + /// function is called, it will be unbound from the previous + /// port before being bound to the new one. + /// /// \param port Port to bind the socket to /// \param address Address of the interface to bind to /// diff --git a/src/SFML/Network/TcpListener.cpp b/src/SFML/Network/TcpListener.cpp index ce62fe76..399f87d6 100644 --- a/src/SFML/Network/TcpListener.cpp +++ b/src/SFML/Network/TcpListener.cpp @@ -63,6 +63,9 @@ unsigned short TcpListener::getLocalPort() const //////////////////////////////////////////////////////////// Socket::Status TcpListener::listen(unsigned short port, const IpAddress& address) { + // Close the socket if it is already bound + close(); + // Create the internal socket if it doesn't exist create(); diff --git a/src/SFML/Network/TcpSocket.cpp b/src/SFML/Network/TcpSocket.cpp index bb3e65c5..e11f76b2 100644 --- a/src/SFML/Network/TcpSocket.cpp +++ b/src/SFML/Network/TcpSocket.cpp @@ -118,6 +118,9 @@ unsigned short TcpSocket::getRemotePort() const //////////////////////////////////////////////////////////// Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short remotePort, Time timeout) { + // Disconnect the socket if it is already connected + disconnect(); + // Create the internal socket if it doesn't exist create(); diff --git a/src/SFML/Network/UdpSocket.cpp b/src/SFML/Network/UdpSocket.cpp index 99b52fdf..0ff03b04 100644 --- a/src/SFML/Network/UdpSocket.cpp +++ b/src/SFML/Network/UdpSocket.cpp @@ -66,6 +66,9 @@ unsigned short UdpSocket::getLocalPort() const //////////////////////////////////////////////////////////// Socket::Status UdpSocket::bind(unsigned short port, const IpAddress& address) { + // Close the socket if it is already bound + close(); + // Create the internal socket if it doesn't exist create();