MCPcopy Create free account
hub / github.com/SFML/SFML / getRemotePort

Method getRemotePort

src/SFML/Network/TcpSocket.cpp:813–836  ·  view source on GitHub ↗

///////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

811
812////////////////////////////////////////////////////////////
813unsigned short TcpSocket::getRemotePort() const
814{
815 if (getNativeHandle() != priv::SocketImpl::invalidSocket())
816 {
817 // Retrieve information about the remote end of the socket
818 sockaddr_in6 addressV6{};
819 priv::SocketImpl::AddrLength size = sizeof(addressV6);
820 if (getpeername(getNativeHandle(), reinterpret_cast<sockaddr*>(&addressV6), &size) != -1)
821 {
822 if (addressV6.sin6_family == PF_INET6)
823 return ntohs(addressV6.sin6_port);
824
825 if (addressV6.sin6_family == PF_INET)
826 {
827 sockaddr_in addressV4{};
828 std::memcpy(&addressV4, &addressV6, sizeof(addressV4));
829 return ntohs(addressV4.sin_port);
830 }
831 }
832 }
833
834 // We failed to retrieve the port
835 return 0;
836}
837
838
839////////////////////////////////////////////////////////////

Callers 2

TcpSocket.test.cppFile · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected