getsockname
getsockname(sock::Union{TCPServer, TCPSocket}) -> (IPAddr,UInt16)
Get the IP address and the port that the given TCP socket is connected to (or bound to, in the case of TCPServer).
Examples
getsockname(sock::Union{TCPServer, TCPSocket}) -> (IPAddr, UInt16)
This function returns the IP address and port number to which the given TCP socket is connected (or bound to, in the case of TCPServer).
Example 1: Get the local IP address and port of a TCP socket
using Sockets
server = listen(8080)
ip, port = getsockname(server)
println("Server is listening on $ip:$port")
close(server)
In this example, we create a TCP server socket using listen on port 8080. We then use getsockname to retrieve the IP address and port number to which the server socket is bound. Finally, we print the IP address and port.
Example 2: Get the IP address and port of a TCP socket
using Sockets
client = connect("localhost", 8080)
ip, port = getsockname(client)
println("Connected to $ip:$port")
close(client)
In this example, we create a TCP client socket using connect to connect to a server running on localhost at port 8080. We use getsockname to retrieve the IP address and port number to which the client socket is connected. Finally, we print the IP address and port.
Note: These examples assume that the Sockets module has been imported.
See Also
accept, bind, :@spawn, connect, fetch, getaddrinfo, gethostname, getipaddr, getsockname, init_worker, IPv4, IPv6, isready, issocket, kill, listen, recv, recvfrom, remotecall, remotecall_fetch, remotecall_wait, RemoteRef, send, setopt,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.