sendto
Transmit a message to another socket.
The sendto call may be used only when the socket is in a connected state (so that the intended
recipient is known). The following call
send(sockfd, buf, len, flags);
is equivalent to
sendto(sockfd, buf, len, flags, NULL, 0);
If sendto() is used on a connection-mode (SOCK.STREAM, SOCK.SEQPACKET) socket, the arguments
dest_addr and addrlen are asserted to be null and 0 respectively, and asserted
that the socket was actually connected.
Otherwise, the address of the target is given by dest_addr with addrlen specifying its size.
If the message is too long to pass atomically through the underlying protocol,
SendError.MessageTooBig is returned, and the message is not transmitted.
There is no indication of failure to deliver.
When the message does not fit into the send buffer of the socket, sendto normally blocks,
unless the socket has been placed in nonblocking I/O mode. In nonblocking mode it would fail
with SendError.WouldBlock. The select call may be used to determine when it is
possible to send more data.
Function parameters
Parameters
Type definitions in this namespace
Types
Obtains errno from the return value of a system function call.
Functions
- errno
- Obtains errno from the return value of a system function call.
- close
- Closes the file descriptor.
- fchmod
- Changes the mode of the file referred to by the file descriptor.
- fchmodat
- Changes the `mode` of `path` relative to the directory referred to by
- fchown
- Changes the owner and group of the file referred to by the file descriptor.
- getrandom
- Obtain a series of random bytes.
- abort
- Causes abnormal process termination.
- exit
- Exits all threads of the program with the specified status code.
- read
- Returns the number of bytes that were read, which can be less than
- readv
- Number of bytes read is returned.
- pread
- Number of bytes read is returned.
- ftruncate
- Length must be positive when treated as an i64.
- preadv
- Number of bytes read is returned.
- write
- Write to a file descriptor.
- writev
- Write multiple buffers to a file descriptor.
- pwrite
- Write to a file descriptor, with a position offset.
- pwritev
- Write multiple buffers to a file descriptor, with a position offset.
- open
- Open and possibly create a file.
- openZ
- Open and possibly create a file.
- openat
- Open and possibly create a file.
- openatWasi
- Open and possibly create a file in WASI.
- openatZ
- Open and possibly create a file.
- execveZ
- This function ignores PATH environment variable.
- execvpeZ_expandArg0
- Like `execvpeZ` except if `arg0_expand` is `.expand`, then `argv` is mutable,
- execvpeZ
- This function also uses the PATH environment variable to get the full path to the executable.
- getenv
- Get an environment variable.
- getenvZ
- Get an environment variable with a null-terminated name.
- getcwd
- The result is a slice of out_buffer, indexed from 0.
- symlink
- Creates a symbolic link named `sym_link_path` which contains the string `target_path`.
- symlinkZ
- This is the same as `symlink` except the parameters are null-terminated pointers.
- symlinkat
- Similar to `symlink`, however, creates a symbolic link named `sym_link_path` which contains the string
- symlinkatWasi
- WASI-only.
- symlinkatZ
- The same as `symlinkat` except the parameters are null-terminated pointers.
- linkZ
- On WASI, both paths should be encoded as valid UTF-8.
- link
- On WASI, both paths should be encoded as valid UTF-8.
- linkatZ
- On WASI, both paths should be encoded as valid UTF-8.
- linkat
- On WASI, both paths should be encoded as valid UTF-8.
- unlink
- Delete a name and possibly the file it refers to.
- unlinkZ
- Same as `unlink` except the parameter is null terminated.
- unlinkW
- Windows-only.
- unlinkat
- Delete a file name and possibly the file it refers to, based on an open directory handle.
- unlinkatWasi
- WASI-only.
- unlinkatZ
- Same as `unlinkat` but `file_path` is a null-terminated string.
- unlinkatW
- Same as `unlinkat` but `sub_path_w` is WTF16LE, NT prefixed.
- rename
- Change the name or location of a file.
- renameZ
- Same as `rename` except the parameters are null-terminated.
- renameW
- Same as `rename` except the parameters are null-terminated and WTF16LE encoded.
- renameat
- Change the name or location of a file based on an open directory handle.
- renameatZ
- Same as `renameat` except the parameters are null-terminated.
- renameatW
- Same as `renameat` but Windows-only and the path parameters are
- mkdirat
- On Windows, `sub_dir_path` should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
- mkdiratZ
- Same as `mkdirat` except the parameters are null-terminated.
- mkdiratW
- Windows-only.
- mkdir
- Create a directory.
- mkdirZ
- Same as `mkdir` but the parameter is null-terminated.
- mkdirW
- Windows-only.
- rmdir
- Deletes an empty directory.
- rmdirZ
- Same as `rmdir` except the parameter is null-terminated.
- rmdirW
- Windows-only.
- chdir
- Changes the current working directory of the calling process.
- chdirZ
- Same as `chdir` except the parameter is null-terminated.
- chdirW
- Windows-only.
- readlink
- Read value of a symbolic link.
- readlinkW
- Windows-only.
- readlinkZ
- Same as `readlink` except `file_path` is null-terminated.
- readlinkat
- Similar to `readlink` except reads value of a symbolink link **relative** to `dirfd` directory handle.
- readlinkatWasi
- WASI-only.
- readlinkatW
- Windows-only.
- readlinkatZ
- Same as `readlinkat` except `file_path` is null-terminated.
- isatty
- Test whether a file descriptor refers to a terminal.
- shutdown
- Shutdown socket send/receive operations
- bind
- addr is `*const T` where T is one of the sockaddr
- accept
- Accept a connection on a socket.
- epoll_wait
- Waits for an I/O event on an epoll file descriptor.
- connect
- Initiate a connection on a socket.
- waitpid
- Use this version of the `waitpid` wrapper if you spawned your child process using explicit
- fstat
- Return information about a file descriptor.
- fstatat
- Similar to `fstat`, but returns stat of a resource pointed to by `pathname`
- fstatatZ
- Same as `fstatat` but `pathname` is null-terminated.
- inotify_init1
- initialize an inotify instance
- inotify_add_watch
- add a watch to an initialized inotify instance
- inotify_add_watchZ
- Same as `inotify_add_watch` except pathname is null-terminated.
- inotify_rm_watch
- remove an existing watch from an inotify instance
- mmap
- Map files or devices into memory.
- munmap
- Deletes the mappings for the specified address range, causing
- access
- check user's permissions for a file
- accessZ
- Same as `access` except `path` is null-terminated.
- faccessat
- Check user's permissions for a file, based on an open directory handle.
- faccessatZ
- Same as `faccessat` except the path parameter is null-terminated.
- faccessatW
- Same as `faccessat` except asserts the target is Windows and the path parameter
- pipe
- Creates a unidirectional data channel that can be used for interprocess communication.
- lseek_SET
- Repositions read/write file offset relative to the beginning.
- lseek_CUR
- Repositions read/write file offset relative to the current offset.
- lseek_END
- Repositions read/write file offset relative to the end.
- lseek_CUR_get
- Returns the read/write file offset relative to the beginning.
- flock
- Depending on the operating system `flock` may or may not interact with
- realpath
- Return the canonicalized absolute pathname.
- realpathZ
- Same as `realpath` except `pathname` is null-terminated.
- realpathW
- Same as `realpath` except `pathname` is WTF16LE-encoded.
- nanosleep
- Spurious wakeups are possible and no precision of timing is guaranteed.
- sigfillset
- Return a filled sigset_t.
- sigemptyset
- Return an empty sigset_t.
- sigaction
- Examine and change a signal action.
- sigprocmask
- Sets the thread signal mask.
- sendto
- Transmit a message to another socket.
- send
- Transmit a message to another socket.
- copy_file_range
- Transfer data between file descriptors at specified offsets.
- recvfrom
- If `sockfd` is opened in non blocking mode, the function will
- setsockopt
- Set a socket's options.
- tcgetpgrp
- Returns the process group ID for the TTY associated with the given handle.
- tcsetpgrp
- Sets the controlling process group ID for given TTY.
- sync
- Write all pending file contents and metadata modifications to all filesystems.
- syncfs
- Write all pending file contents and metadata modifications to the filesystem which contains the specified file.
- fsync
- Write all pending file contents and metadata modifications for the specified file descriptor to the underlying filesystem.
- fdatasync
- Write all pending file contents for the specified file descriptor to the underlying filesystem, but not necessarily the metadata.
- mincore
- Determine whether pages are resident in memory.
- madvise
- Give advice about use of memory.
- unexpectedErrno
- Call this when you made a syscall or something that sets errno
- toPosixPath
- Used to convert a slice to a null terminated slice on the stack.
Error sets in this namespace
Error Sets
= system.AF
Values
- AF
- = system.AF
- AF_SUN
- = system.AF_SUN
- AI
- = system.AI
- ARCH
- = system.ARCH
- AT
- = system.AT
- AT_SUN
- = system.AT_SUN
- CLOCK
- = system.CLOCK
- CPU_COUNT
- = system.CPU_COUNT
- CTL
- = system.CTL
- DT
- = system.DT
- E
- = system.E
- Elf_Symndx
- = system.Elf_Symndx
- F
- = system.F
- FD_CLOEXEC
- = system.FD_CLOEXEC
- Flock
- = system.Flock
- HOST_NAME_MAX
- = system.HOST_NAME_MAX
- HW
- = system.HW
- IFNAMESIZE
- = system.IFNAMESIZE
- IOV_MAX
- = system.IOV_MAX
- IPPROTO
- = system.IPPROTO
- KERN
- = system.KERN
- Kevent
- = system.Kevent
- MADV
- = system.MADV
- MAP
- = system.MAP
- MAX_ADDR_LEN
- = system.MAX_ADDR_LEN
- MFD
- = system.MFD
- MREMAP
- = system.MREMAP
- MSF
- = system.MSF
- MSG
- = system.MSG
- NAME_MAX
- = system.NAME_MAX
- NSIG
- = system.NSIG
- O
- = system.O
- PATH_MAX
- = system.PATH_MAX
- POLL
- = system.POLL
- POSIX_FADV
- = system.POSIX_FADV
- PR
- = system.PR
- PROT
- = system.PROT
- REG
- = system.REG
- RLIM
- = system.RLIM
- RR
- = system.RR
- S
- = system.S
- SA
- = system.SA
- SC
- = system.SC
- SEEK
- = system.SEEK
- SHUT
- = system.SHUT
- SIG
- = system.SIG
- SIOCGIFINDEX
- = system.SIOCGIFINDEX
- SO
- = system.SO
- SOCK
- = system.SOCK
- SOL
- = system.SOL
- IFF
- = system.IFF
- STDERR_FILENO
- = system.STDERR_FILENO
- STDIN_FILENO
- = system.STDIN_FILENO
- STDOUT_FILENO
- = system.STDOUT_FILENO
- SYS
- = system.SYS
- Sigaction
- = system.Sigaction
- Stat
- = system.Stat
- T
- = system.T
- TCP
- = system.TCP
- VDSO
- = system.VDSO
- W
- = system.W
- _SC
- = system._SC
- addrinfo
- = system.addrinfo
- blkcnt_t
- = system.blkcnt_t
- blksize_t
- = system.blksize_t
- clock_t
- = system.clock_t
- clockid_t
- = system.clockid_t
- timerfd_clockid_t
- = system.timerfd_clockid_t
- cpu_set_t
- = system.cpu_set_t
- dev_t
- = system.dev_t
- dl_phdr_info
- = system.dl_phdr_info
- fd_t
- = system.fd_t
- file_obj
- = system.file_obj
- gid_t
- = system.gid_t
- ifreq
- = system.ifreq
- ino_t
- = system.ino_t
- mcontext_t
- = system.mcontext_t
- mode_t
- = system.mode_t
- msghdr
- = system.msghdr
- msghdr_const
- = system.msghdr_const
- nfds_t
- = system.nfds_t
- nlink_t
- = system.nlink_t
- off_t
- = system.off_t
- pid_t
- = system.pid_t
- pollfd
- = system.pollfd
- port_event
- = system.port_event
- port_notify
- = system.port_notify
- port_t
- = system.port_t
- rlim_t
- = system.rlim_t
- rlimit
- = system.rlimit
- rlimit_resource
- = system.rlimit_resource
- rusage
- = system.rusage
- sa_family_t
- = system.sa_family_t
- siginfo_t
- = system.siginfo_t
- sigset_t
- = system.sigset_t
- sigrtmin
- = system.sigrtmin
- sigrtmax
- = system.sigrtmax
- sockaddr
- = system.sockaddr
- socklen_t
- = system.socklen_t
- stack_t
- = system.stack_t
- time_t
- = system.time_t
- timespec
- = system.timespec
- timestamp_t
- = system.timestamp_t
- timeval
- = system.timeval
- timezone
- = system.timezone
- ucontext_t
- = system.ucontext_t
- uid_t
- = system.uid_t
- user_desc
- = system.user_desc
- utsname
- = system.utsname
- termios
- = system.termios
- CSIZE
- = system.CSIZE
- NCCS
- = system.NCCS
- cc_t
- = system.cc_t
- V
- = system.V
- speed_t
- = system.speed_t
- tc_iflag_t
- = system.tc_iflag_t
- tc_oflag_t
- = system.tc_oflag_t
- tc_cflag_t
- = system.tc_cflag_t
- tc_lflag_t
- = system.tc_lflag_t
- F_OK
- = system.F_OK
- R_OK
- = system.R_OK
- W_OK
- = system.W_OK
- X_OK
- = system.X_OK
- unexpected_error_tracing
- Whether or not `error.Unexpected` will print its value and a stack trace.
Source
Implementation
pub fn sendto(
/// The file descriptor of the sending socket.
sockfd: socket_t,
/// Message to send.
buf: []const u8,
flags: u32,
dest_addr: ?*const sockaddr,
addrlen: socklen_t,
) SendToError!usize {
if (native_os == .windows) {
switch (windows.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen)) {
windows.ws2_32.SOCKET_ERROR => switch (windows.ws2_32.WSAGetLastError()) {
.WSAEACCES => return error.AccessDenied,
.WSAEADDRNOTAVAIL => return error.AddressNotAvailable,
.WSAECONNRESET => return error.ConnectionResetByPeer,
.WSAEMSGSIZE => return error.MessageTooBig,
.WSAENOBUFS => return error.SystemResources,
.WSAENOTSOCK => return error.FileDescriptorNotASocket,
.WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported,
.WSAEDESTADDRREQ => unreachable, // A destination address is required.
.WSAEFAULT => unreachable, // The lpBuffers, lpTo, lpOverlapped, lpNumberOfBytesSent, or lpCompletionRoutine parameters are not part of the user address space, or the lpTo parameter is too small.
.WSAEHOSTUNREACH => return error.NetworkUnreachable,
// TODO: WSAEINPROGRESS, WSAEINTR
.WSAEINVAL => unreachable,
.WSAENETDOWN => return error.NetworkSubsystemFailed,
.WSAENETRESET => return error.ConnectionResetByPeer,
.WSAENETUNREACH => return error.NetworkUnreachable,
.WSAENOTCONN => return error.SocketNotConnected,
.WSAESHUTDOWN => unreachable, // The socket has been shut down; it is not possible to WSASendTo on a socket after shutdown has been invoked with how set to SD_SEND or SD_BOTH.
.WSAEWOULDBLOCK => return error.WouldBlock,
.WSANOTINITIALISED => unreachable, // A successful WSAStartup call must occur before using this function.
else => |err| return windows.unexpectedWSAError(err),
},
else => |rc| return @intCast(rc),
}
}
while (true) {
const rc = system.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen);
switch (errno(rc)) {
.SUCCESS => return @intCast(rc),
.ACCES => return error.AccessDenied,
.AGAIN => return error.WouldBlock,
.ALREADY => return error.FastOpenAlreadyInProgress,
.BADF => unreachable, // always a race condition
.CONNREFUSED => return error.ConnectionRefused,
.CONNRESET => return error.ConnectionResetByPeer,
.DESTADDRREQ => unreachable, // The socket is not connection-mode, and no peer address is set.
.FAULT => unreachable, // An invalid user space address was specified for an argument.
.INTR => continue,
.INVAL => return error.UnreachableAddress,
.ISCONN => unreachable, // connection-mode socket was connected already but a recipient was specified
.MSGSIZE => return error.MessageTooBig,
.NOBUFS => return error.SystemResources,
.NOMEM => return error.SystemResources,
.NOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket.
.OPNOTSUPP => unreachable, // Some bit in the flags argument is inappropriate for the socket type.
.PIPE => return error.BrokenPipe,
.AFNOSUPPORT => return error.AddressFamilyNotSupported,
.LOOP => return error.SymLinkLoop,
.NAMETOOLONG => return error.NameTooLong,
.NOENT => return error.FileNotFound,
.NOTDIR => return error.NotDir,
.HOSTUNREACH => return error.NetworkUnreachable,
.NETUNREACH => return error.NetworkUnreachable,
.NOTCONN => return error.SocketNotConnected,
.NETDOWN => return error.NetworkSubsystemFailed,
else => |err| return unexpectedErrno(err),
}
}
}