Movatterモバイル変換


[0]ホーム

URL:


GitHub

Sockets

Sockets.SocketsModule

Support for sockets. ProvidesIPAddr and subtypes,TCPSocket, andUDPSocket.

Sockets.connectMethod
connect([host], port::Integer) -> TCPSocket

Connect to the hosthost on portport.

Sockets.connectMethod
connect(path::AbstractString) -> PipeEndpoint

Connect to the named pipe / UNIX domain socket atpath.

Note

Path length on Unix is limited to somewhere between 92 and 108 bytes (cf.man unix).

Sockets.listenMethod
listen([addr, ]port::Integer; backlog::Integer=BACKLOG_DEFAULT) -> TCPServer

Listen on port on the address specified byaddr. By default this listens onlocalhost only. To listen on all interfaces passIPv4(0) orIPv6(0) as appropriate.backlog determines how many connections can be pending (not having calledaccept) before the server will begin to reject them. The default value ofbacklog is 511.

Sockets.listenMethod
listen(path::AbstractString) -> PipeServer

Create and listen on a named pipe / UNIX domain socket.

Note

Path length on Unix is limited to somewhere between 92 and 108 bytes (cf.man unix).

Sockets.getaddrinfoFunction
getaddrinfo(host::AbstractString, IPAddr) -> IPAddr

Gets the first IP address of thehost of the specifiedIPAddr type. Uses the operating system's underlying getaddrinfo implementation, which may do a DNS lookup.

Examples

julia> getaddrinfo("localhost", IPv6)ip"::1"julia> getaddrinfo("localhost", IPv4)ip"127.0.0.1"
getaddrinfo(host::AbstractString) -> IPAddr

Gets the first available IP address ofhost, which may be either anIPv4 orIPv6 address. Uses the operating system's underlying getaddrinfo implementation, which may do a DNS lookup.

Sockets.getipaddrFunction
getipaddr() -> IPAddr

Get an IP address of the local machine, preferring IPv4 over IPv6. Throws if no addresses are available.

getipaddr(addr_type::Type{T}) where T<:IPAddr -> T

Get an IP address of the local machine of the specified type. Throws if no addresses of the specified type are available.

This function is a backwards-compatibility wrapper aroundgetipaddrs. New applications should usegetipaddrs instead.

Examples

julia> getipaddr()ip"192.168.1.28"julia> getipaddr(IPv6)ip"fe80::9731:35af:e1c5:6e49"

See alsogetipaddrs.

Sockets.getipaddrsFunction
getipaddrs(addr_type::Type{T}=IPAddr; loopback::Bool=false) where T<:IPAddr -> Vector{T}

Get the IP addresses of the local machine.

Setting the optionaladdr_type parameter toIPv4 orIPv6 causes only addresses of that type to be returned.

Theloopback keyword argument dictates whether loopback addresses (e.g.ip"127.0.0.1",ip"::1") are included.

Julia 1.2

This function is available as of Julia 1.2.

Examples

julia> getipaddrs()5-element Array{IPAddr,1}: ip"198.51.100.17" ip"203.0.113.2" ip"2001:db8:8:4:445e:5fff:fe5d:5500" ip"2001:db8:8:4:c164:402e:7e3c:3668" ip"fe80::445e:5fff:fe5d:5500"julia> getipaddrs(IPv6)3-element Array{IPv6,1}: ip"2001:db8:8:4:445e:5fff:fe5d:5500" ip"2001:db8:8:4:c164:402e:7e3c:3668" ip"fe80::445e:5fff:fe5d:5500"

See alsoislinklocaladdr.

Sockets.islinklocaladdrFunction
islinklocaladdr(addr::IPAddr)

Tests if an IP address is a link-local address. Link-local addresses are not guaranteed to be unique beyond their network segment, therefore routers do not forward them. Link-local addresses are from the address blocks169.254.0.0/16 orfe80::/10.

Examples

filter(!islinklocaladdr, getipaddrs())
Sockets.getalladdrinfoFunction
getalladdrinfo(host::AbstractString) -> Vector{IPAddr}

Gets all of the IP addresses of thehost. Uses the operating system's underlyinggetaddrinfo implementation, which may do a DNS lookup.

Examples

julia> getalladdrinfo("google.com")2-element Array{IPAddr,1}: ip"172.217.6.174" ip"2607:f8b0:4000:804::200e"
Sockets.DNSErrorType
DNSError

The type of exception thrown when an error occurs in DNS lookup. Thehost field indicates the host URL string. Thecode field indicates the error code based on libuv.

Sockets.getnameinfoFunction
getnameinfo(host::IPAddr) -> String

Performs a reverse-lookup for IP address to return a hostname and service using the operating system's underlyinggetnameinfo implementation.

Examples

julia> getnameinfo(IPv4("8.8.8.8"))"google-public-dns-a.google.com"
Sockets.getsocknameFunction
getsockname(sock::Union{TCPServer, TCPSocket}) -> (IPAddr, UInt16)

Get the IP address and port that the given socket is bound to.

Sockets.getpeernameFunction
getpeername(sock::TCPSocket) -> (IPAddr, UInt16)

Get the IP address and port of the remote endpoint that the given socket is connected to. Valid only for connected TCP sockets.

Sockets.IPAddrType
IPAddr

Abstract supertype for IP addresses.IPv4 andIPv6 are subtypes of this.

Sockets.IPv4Type
IPv4(host::Integer) -> IPv4

Return an IPv4 object from IP addresshost formatted as anInteger.

Examples

julia> IPv4(3223256218)ip"192.30.252.154"
IPv4(str::AbstractString) -> IPv4

Parse an IPv4 address string into anIPv4 object.

Examples

julia> IPv4("127.0.0.1")ip"127.0.0.1"
Sockets.IPv6Type
IPv6(host::Integer) -> IPv6

Return an IPv6 object from IP addresshost formatted as anInteger.

Examples

julia> IPv6(3223256218)ip"::c01e:fc9a"
IPv6(str::AbstractString) -> IPv6

Parse an IPv6 address string into anIPv6 object.

Examples

julia> IPv6("::1")ip"::1"
Sockets.@ip_strMacro
@ip_str str -> IPAddr

Parsestr as an IP address.

Examples

julia> ip"127.0.0.1"ip"127.0.0.1"julia> @ip_str "2001:db8:0:0:0:0:2:1"ip"2001:db8::2:1"
Sockets.TCPSocketType
TCPSocket(; delay=true)

Open a TCP socket using libuv. Ifdelay is true, libuv delays creation of the socket's file descriptor till the firstbind call.TCPSocket has various fields to denote the state of the socket as well as its send/receive buffers.

Sockets.UDPSocketType
UDPSocket()

Open a UDP socket using libuv.UDPSocket has various fields to denote the state of the socket.

Sockets.acceptFunction
accept(server[, client])

Accepts a connection on the given server and returns a connection to the client. An uninitialized client stream may be provided, in which case it will be used instead of creating a new stream.

Sockets.listenanyFunction
listenany([host::IPAddr,] port_hint; backlog::Integer=BACKLOG_DEFAULT) -> (UInt16, TCPServer)

Create aTCPServer on any port, using hint as a starting point. Returns a tuple of the actual port that the server was created on and the server itself. The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow.

Base.bindFunction
bind(socket::Union{TCPServer, UDPSocket, TCPSocket}, host::IPAddr, port::Integer; ipv6only=false, reuseaddr=false, kws...)

Bindsocket to the givenhost:port. Note that0.0.0.0 will listen on all devices.

  • Theipv6only parameter disables dual stack mode. Ifipv6only=true, only an IPv6 stack is created.
  • Ifreuseaddr=true, multiple threads or processes can bind to the same address without error if they all setreuseaddr=true, but only the last to bind will receive any traffic.
bind(chnl::Channel, task::Task)

Associate the lifetime ofchnl with a task.Channelchnl is automatically closed when the task terminates. Any uncaught exception in the task is propagated to all waiters onchnl.

Thechnl object can be explicitly closed independent of task termination. Terminating tasks have no effect on already closedChannel objects.

When a channel is bound to multiple tasks, the first task to terminate will close the channel. When multiple channels are bound to the same task, termination of the task will close all of the bound channels.

Examples

julia> c = Channel(0);julia> task = @async foreach(i->put!(c, i), 1:4);julia> bind(c,task);julia> for i in c           @show i       end;i = 1i = 2i = 3i = 4julia> isopen(c)false
julia> c = Channel(0);julia> task = @async (put!(c, 1); error("foo"));julia> bind(c, task);julia> take!(c)1julia> put!(c, 1);ERROR: TaskFailedExceptionStacktrace:[...]    nested task error: foo[...]
source
Sockets.sendFunction
send(socket::UDPSocket, host::IPAddr, port::Integer, msg)

Sendmsg oversocket tohost:port.

Sockets.recvFunction
recv(socket::UDPSocket)

Read a UDP packet from the specified socket, and return the bytes received. This call blocks.

Sockets.recvfromFunction
recvfrom(socket::UDPSocket) -> (host_port, data)

Read a UDP packet from the specified socket, returning a tuple of(host_port, data), wherehost_port will be an InetAddr{IPv4} or InetAddr{IPv6}, as appropriate.

Julia 1.3

Prior to Julia version 1.3, the first returned value was an address (IPAddr). In version 1.3 it was changed to anInetAddr.

Sockets.setoptFunction
setopt(sock::UDPSocket; multicast_loop=nothing, multicast_ttl=nothing, enable_broadcast=nothing, ttl=nothing)

Set UDP socket options.

  • multicast_loop: loopback for multicast packets (default:true).
  • multicast_ttl: TTL for multicast packets (default:nothing).
  • enable_broadcast: flag must be set totrue if socket will be used for broadcast messages, or else the UDP system will return an access error (default:false).
  • ttl: Time-to-live of packets sent on the socket (default:nothing).
Sockets.nagleFunction
nagle(socket::Union{TCPServer, TCPSocket}, enable::Bool)

Nagle's algorithm batches multiple small TCP packets into larger ones. This can improve throughput but worsen latency. Nagle's algorithm is enabled by default. This function sets whether Nagle's algorithm is active on a given TCP server or socket. The opposite option is calledTCP_NODELAY in other languages.

Julia 1.3

This function requires Julia 1.3 or later.

Sockets.quickackFunction
quickack(socket::Union{TCPServer, TCPSocket}, enable::Bool)

On Linux systems, the TCP_QUICKACK is disabled or enabled onsocket.

Settings


This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.


[8]ページ先頭

©2009-2025 Movatter.jp