bind

bind(socket::Union{UDPSocket, TCPSocket}, host::IPAddr, port::Integer; ipv6only=false)

Bind socket to the given host:port. Note that 0.0.0.0 will listen on all devices. ipv6only parameter disables dual stack mode. If it's true, only IPv6 stack is created.

Examples

julia> bind(socket, host, port)

Binds the socket to the specified host:port combination. The host argument should be of type IPAddr, and the port argument should be an integer.

Example usage:

  1. Bind a UDP socket to a specific address and port:

    julia> using Sockets
    
    julia> udp_socket = UDPSocket();
    
    julia> bind(udp_socket, parse(IPAddr, "192.168.0.1"), 8000)

    In this example, a UDP socket is created and bound to the IP address "192.168.0.1" and port 8000.

  2. Bind a TCP socket to all available devices on a specific port:

    julia> using Sockets
    
    julia> tcp_socket = TCPSocket();
    
    julia> bind(tcp_socket, parse(IPAddr, "0.0.0.0"), 8080)

    This code binds a TCP socket to all available devices (IP address "0.0.0.0") on port 8080.

  3. Bind a socket with IPv6-only stack:

    julia> using Sockets
    
    julia> tcp_socket = TCPSocket();
    
    julia> bind(tcp_socket, parse(IPAddr, "::1"), 8888, ipv6only=true)

    This example binds a TCP socket to the IPv6 loopback address "::1" on port 8888, using only the IPv6 stack.

Note: Make sure to import the Sockets module before using the UDPSocket and TCPSocket types.

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.

*Required Field
Details

Checking you are not a robot: