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

Method getLocalPort

src/SFML/Network/TcpSocket.cpp:734–757  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

732
733////////////////////////////////////////////////////////////
734unsigned short TcpSocket::getLocalPort() const
735{
736 if (getNativeHandle() != priv::SocketImpl::invalidSocket())
737 {
738 // Retrieve information about the local end of the socket
739 sockaddr_in6 addressV6{};
740 priv::SocketImpl::AddrLength size = sizeof(addressV6);
741 if (getsockname(getNativeHandle(), reinterpret_cast<sockaddr*>(&addressV6), &size) != -1)
742 {
743 if (addressV6.sin6_family == PF_INET6)
744 return ntohs(addressV6.sin6_port);
745
746 if (addressV6.sin6_family == PF_INET)
747 {
748 sockaddr_in addressV4{};
749 std::memcpy(&addressV4, &addressV6, sizeof(addressV4));
750 return ntohs(addressV4.sin_port);
751 }
752 }
753 }
754
755 // We failed to retrieve the port
756 return 0;
757}
758
759
760////////////////////////////////////////////////////////////

Callers 5

TcpSocket.test.cppFile · 0.45
UdpSocket.test.cppFile · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected