1socket(3) Erlang Module Definition socket(3)
2
3
4
6 socket - Socket interface.
7
9 This module provides an API for network socket. Functions are provided
10 to create, delete and manupilate the sockets aswell as sending and re‐
11 civing data on them.
12
13 The intent is that it shall be as "close as possible" to the OS level
14 socket interface. The only significant addition is that some of the
15 functions, e.g. recv/3, has a timeout argument.
16
17 Note:
18 Some functions allow for an asynchronous call. This is achieved by
19 setting the Timeout argument to nowait. For instance, if calling the
20 recv/3 function with Timeout set to nowait (recv(Sock, 0, nowait)) when
21 there is actually nothing to read, it will return with {select, Se‐
22 lectInfo} (SelectInfo contains the SelectRef). When data eventually ar‐
23 rives a 'select' message will be sent to the caller:
24
25 : {'$socket', socket(), select, SelectRef}
26
27 The caller can now make another call to the recv function and now ex‐
28 pect data.
29
30 Note that all other users are locked out until the 'current user' has
31 called the function (recv in this case).
32
33 Another message the user must be prepared for (when making asynchronous
34 calls) is the abort message:
35
36 : {'$socket', socket(), abort, Info}
37
38 This message indicates that the (asynchronous) operation has been
39 aborted. If, for instance, the socket has been closed (by another
40 process), Info will be {SelectRef, closed}.
41
42
43 Note:
44 There is currently no support for Windows.
45
46 Support for IPv6 has been implemented but not tested.
47
48 SCTP has only been partly implemented (and not tested).
49
50
52 domain() = local | inet | inet6
53
54 type() = stream | dgram | raw | rdm | seqpacket
55
56 protocol() =
57 ip | tcp | udp | sctp | icmp | igmp | {raw, integer()}
58
59 socket()
60
61 As returned by open/1,2,3,4 and accept/1,2.
62
63 select_tag()
64
65 A tag that describes the (select) operation.
66
67 select_ref()
68
69 A reference that uniquely identifies the (select) operation.
70
71 select_info() = {select_info, select_tag(), select_ref()}
72
73 socket_counters() =
74 #{read_byte := integer() >= 0,
75 read_fails := integer() >= 0,
76 read_pkg := integer() >= 0,
77 read_pkg_max := integer() >= 0,
78 read_tries := integer() >= 0,
79 read_waits := integer() >= 0,
80 write_byte := integer() >= 0,
81 write_fails := integer() >= 0,
82 write_pkg := integer() >= 0,
83 write_pkg_max := integer() >= 0,
84 write_tries := integer() >= 0,
85 write_waits := integer() >= 0,
86 acc_success := integer() >= 0,
87 acc_fails := integer() >= 0,
88 acc_tries := integer() >= 0,
89 acc_waits := integer() >= 0}
90
91 socket_info() =
92 #{domain := domain(),
93 type := type(),
94 protocol := protocol(),
95 ctrl := pid(),
96 ctype := normal | fromfd | {fromfd, integer()},
97 counters := socket_counters(),
98 num_readers := integer() >= 0,
99 num_writers := integer() >= 0,
100 num_acceptors := integer() >= 0,
101 writable := boolean(),
102 readable := boolean()}
103
104 ip4_address() = {0..255, 0..255, 0..255, 0..255}
105
106 ip6_address() =
107 {0..65535,
108 0..65535,
109 0..65535,
110 0..65535,
111 0..65535,
112 0..65535,
113 0..65535,
114 0..65535}
115
116 sockaddr() =
117 sockaddr_in4() |
118 sockaddr_in6() |
119 sockaddr_un() |
120 sockaddr_ll()
121
122 sockaddr_in4() =
123 #{family := inet,
124 port := port_number(),
125 addr := any | broadcast | loopback | ip4_address()}
126
127 sockaddr_in6() =
128 #{family := inet6,
129 port := port_number(),
130 addr := any | loopback | ip6_address(),
131 flowinfo := in6_flow_info(),
132 scope_id := in6_scope_id()}
133
134 sockaddr_un() = #{family := local, path := binary() | string()}
135
136 sockaddr_ll() =
137 #{family := packet,
138 protocol := integer() >= 0,
139 ifindex := integer(),
140 pkttype := packet_type(),
141 hatype := integer() >= 0,
142 addr := binary()}
143
144 packet_type() =
145 host | broadcast | multicast | otherhost | outgoing |
146 loopback | user | kernel | fastroute |
147 integer() >= 0
148
149 port_number() = 0..65535
150
151 in6_flow_info() = uint20()
152
153 in6_scope_id() = uint32()
154
155 send_flags() = [send_flag()]
156
157 send_flag() = confirm | dontroute | eor | more | nosignal | oob
158
159 recv_flags() = [recv_flag()]
160
161 recv_flag() = cmsg_cloexec | errqueue | oob | peek | trunc
162
163 shutdown_how() = read | write | read_write
164
165 sockopt_level() =
166 otp | socket | ip | ipv6 | tcp | udp | sctp |
167 integer() >= 0
168
169 otp_socket_option() =
170 debug | use_registry | iow | controlling_process | rcvbuf |
171 rcvctrlbuf | sndctrlbuf | meta | fd
172
173 socket_option() =
174 acceptconn | acceptfilter | bindtodevice | broadcast |
175 busy_poll | debug | domain | dontroute | error | keepalive |
176 linger | mark | oobinline | passcred | peek_off | peercred |
177 priority | protocol | rcvbuf | rcvbufforce | rcvlowat |
178 rcvtimeo | reuseaddr | reuseport | rxq_ovfl | setfib |
179 sndbuf | sndbufforce | sndlowat | sndtimeo | timestamp | type
180
181 ip_socket_option() =
182 add_membership | add_source_membership | block_source |
183 dontfrag | drop_membership | drop_source_membership |
184 freebind | hdrincl | minttl | msfilter | mtu | mtu_discover |
185 multicast_all | multicast_if | multicast_loop |
186 multicast_ttl | nodefrag | options | pktinfo | recverr |
187 recvif | recvdstaddr | recvopts | recvorigdstaddr | recvtos |
188 recvttl | retopts | router_alert | sndsrcaddr | tos |
189 transparent | ttl | unblock_source
190
191 ipv6_socket_option() =
192 addrform | add_membership | authhdr | auth_level | checksum |
193 drop_membership | dstopts | esp_trans_level |
194 esp_network_level | faith | flowinfo | hopopts |
195 ipcomp_level | join_group | leave_group | mtu | mtu_discover |
196 multicast_hops | multicast_if | multicast_loop | portrange |
197 pktoptions | recverr | recvhoplimit | hoplimit | recvpktinfo |
198 pktinfo | recvtclass | router_alert | rthdr | tclass |
199 unicast_hops | use_min_mtu | v6only
200
201 tcp_socket_option() =
202 congestion | cork | info | keepcnt | keepidle | keepintvl |
203 maxseg | md5sig | nodelay | noopt | nopush | syncnt |
204 user_timeout
205
206 udp_socket_option() = cork
207
208 sctp_socket_option() =
209 adaption_layer | associnfo | auth_active_key | auth_asconf |
210 auth_chunk | auth_key | auth_delete_key | autoclose |
211 context | default_send_params | delayed_ack_time |
212 disable_fragments | hmac_ident | events | explicit_eor |
213 fragment_interleave | get_peer_addr_info | initmsg |
214 i_want_mapped_v4_addr | local_auth_chunks | maxseg |
215 maxburst | nodelay | partial_delivery_point |
216 peer_addr_params | peer_auth_chunks | primary_addr |
217 reset_streams | rtoinfo | set_peer_primary_addr | status |
218 use_ext_recvinfo
219
220 timeval() = #{sec := integer(), usec := integer()}
221
222 ip_tos() =
223 lowdelay | throughput | reliability | mincost | integer()
224
225 ip_mreq() =
226 #{multiaddr := ip4_address(),
227 interface := any | ip4_address()}
228
229 ip_mreq_source() =
230 #{multiaddr := ip4_address(),
231 interface := ip4_address(),
232 sourceaddr := ip4_address()}
233
234 ip_pmtudisc() = want | dont | do | probe
235
236 ip_msfilter_mode() = include | exclude
237
238 ip_msfilter() =
239 #{multiaddr := ip4_address(),
240 interface := ip4_address(),
241 mode := ip_msfilter_mode(),
242 slist := [ip4_address()]}
243
244 ip_pktinfo() =
245 #{ifindex := integer() >= 0,
246 spec_dst := ip4_address(),
247 addr := ip4_address()}
248
249 ipv6_mreq() =
250 #{multiaddr := ip6_address(), interface := integer() >= 0}
251
252 ipv6_pmtudisc() = ip_pmtudisc()
253
254 ipv6_pktinfo() = #{addr := ip6_address(), ifindex := integer()}
255
256 sctp_assoc_id() = int32()
257
258 sctp_sndrcvinfo() =
259 #{stream := uint16(),
260 ssn := uint16(),
261 flags := uint16(),
262 ppid := uint16(),
263 context := uint16(),
264 timetolive := uint16(),
265 tsn := uint16(),
266 cumtsn := uint16(),
267 assoc_id := sctp_assoc_id()}
268
269 sctp_event_subscribe() =
270 #{data_in := boolean(),
271 association := boolean(),
272 address := boolean(),
273 send_failure := boolean(),
274 peer_error := boolean(),
275 shutdown := boolean(),
276 partial_delivery := boolean(),
277 adaptation_layer := boolean(),
278 authentication := boolean(),
279 sender_dry := boolean()}
280
281 sctp_assocparams() =
282 #{assoc_id := sctp_assoc_id(),
283 max_rxt := uint16(),
284 num_peer_dests := uint16(),
285 peer_rwnd := uint32(),
286 local_rwnd := uint32(),
287 cookie_life := uint32()}
288
289 sctp_initmsg() =
290 #{num_outstreams := uint16(),
291 max_instreams := uint16(),
292 max_attempts := uint16(),
293 max_init_timeo := uint16()}
294
295 sctp_rtoinfo() =
296 #{assoc_id := sctp_assoc_id(),
297 initial := uint32(),
298 max := uint32(),
299 min := uint32()}
300
301 msghdr_flag() = ctrunc | eor | errqueue | oob | trunc
302
303 msghdr_flags() = [msghdr_flag()]
304
305 msghdr() =
306 #{addr := sockaddr(),
307 iov := [binary()],
308 ctrl := [cmsghdr_recv()] | [cmsghdr_send()],
309 flags := msghdr_flags()}
310
311 cmsghdr_level() = socket | ip | ipv6 | integer()
312
313 cmsghdr_type() =
314 credentials | hoplevel | origdstaddr | pktinfo | recvtos |
315 rights | timestamp | tos | ttl |
316 integer()
317
318 cmsghdr_recv() =
319 #{level := socket, type := timestamp, data := timeval()} |
320 #{level := socket, type := rights, data := binary()} |
321 #{level := socket, type := credentials, data := binary()} |
322 #{level := socket, type := integer(), data := binary()} |
323 #{level := ip, type := tos, data := ip_tos()} |
324 #{level := ip, type := recvtos, data := ip_tos()} |
325 #{level := ip, type := ttl, data := integer()} |
326 #{level := ip, type := recvttl, data := integer()} |
327 #{level := ip, type := pktinfo, data := ip_pktinfo()} |
328 #{level := ip, type := origdstaddr, data := sockaddr_in4()} |
329 #{level := ip,
330 type := recverr,
331 data := extended_err() | binary()} |
332 #{level := ip, type := integer(), data := binary()} |
333 #{level := ipv6, type := hoplevel, data := integer()} |
334 #{level := ipv6, type := pktinfo, data := ipv6_pktinfo()} |
335 #{level := ipv6,
336 type := recverr,
337 data := extended_err() | binary()} |
338 #{level := ipv6, type := tclass, data := integer()} |
339 #{level := ipv6, type := integer(), data := binary()} |
340 #{level := integer(), type := integer(), data := binary()}
341
342 cmsghdr_send() =
343 #{level := socket, type := timestamp, data := binary()} |
344 #{level := socket, type := rights, data := binary()} |
345 #{level := socket, type := credentials, data := binary()} |
346 #{level := socket, type := integer(), data := binary()} |
347 #{level := ip, type := tos, data := ip_tos() | binary()} |
348 #{level := ip, type := ttl, data := integer() | binary()} |
349 #{level := ip, type := integer(), data := binary()} |
350 #{level := ipv6, type := tclass, data := integer()} |
351 #{level := ipv6, type := integer(), data := binary()} |
352 #{level := udp, type := integer(), data := binary()} |
353 #{level := integer(), type := integer(), data := binary()}
354
355 icmp_dest_unreach() =
356 net_unreach | host_unreach | port_unreach | frag_needed |
357 net_unknown | host_unknown |
358 uint8()
359
360 icmpv6_dest_unreach() =
361 noroute | adm_prohibited | not_neighbour | addr_unreach |
362 port_unreach | policy_fail | reject_route |
363 uint8()
364
365 ee_origin() = none | local | icmp | icmp6 | uint8()
366
367 extended_err() =
368 #{error := term(),
369 origin := icmp,
370 type := dest_unreach,
371 code := icmp_dest_unreach(),
372 info := uint32(),
373 data := uint32(),
374 offender := undefined | sockaddr()} |
375 #{error := term(),
376 origin := icmp,
377 type := time_exceeded | uint8(),
378 code := uint8(),
379 info := uint32(),
380 data := uint32(),
381 offender := undefined | sockaddr()} |
382 #{error := term(),
383 origin := icmp6,
384 type := dest_unreach,
385 code := icmpv6_dest_unreach(),
386 info := uint32(),
387 data := uint32(),
388 offender := undefined | sockaddr()} |
389 #{error := term(),
390 origin := icmp6,
391 type := pkt_toobig | time_exceeded | uint8(),
392 code := uint8(),
393 info := uint32(),
394 data := uint32(),
395 offender := undefined | sockaddr()} |
396 #{error := term(),
397 origin := ee_origin(),
398 type := uint8(),
399 code := uint8(),
400 info := uint32(),
401 data := uint32(),
402 offender := undefined | sockaddr()}
403
404 uint8() = 0..255
405
406 uint16() = 0..65535
407
408 uint20() = 0..1048575
409
410 uint32() = 0..4294967295
411
412 int32() = -2147483648..2147483647
413
414 errcode() =
415 inet:posix() | exalloc | exmonitor | exselect | exself
416
417 The POSIX error codes are mostly come from the OS level socket
418 interface, but this module may generate some appropriate POSIX
419 codes.
420
421 The other values come from this module's lower levels and are
422 all fairly fatal internal errors:
423
424 exalloc:
425 Memory allocation failed
426
427 exmonitor:
428 Failed to set a monitor on a process
429
430 exselect:
431 Select operation failed
432
433 exself:
434 Failed to get current process
435
437 accept(LSocket) -> {ok, Socket} | {error, Reason}
438
439 accept(LSocket, Timeout) -> {ok, Socket} | {error, Reason}
440
441 Types:
442
443 LSocket = socket()
444 Timeout = timeout()
445 Socket = socket()
446 Reason = errcode() | closed | timeout
447
448 Accept a connection on a socket.
449
450 This call is used with connection-based socket types (stream or
451 seqpacket). It extracs the first pending connection request for
452 the listen socket and returns the (newly) connected socket.
453
454 accept(LSocket, Timeout :: nowait) ->
455 {ok, Socket} | {select, SelectInfo} | {error, Reason}
456
457 Types:
458
459 LSocket = Socket = socket()
460 SelectInfo = select_info()
461 Reason = errcode() | closed
462
463 Accept a connection on a socket.
464
465 This call is used with connection-based socket types (stream or
466 seqpacket). It extracs the first pending connection request for
467 the listen socket and returns the (newly) connected socket.
468
469 In the case when there is no connections waiting, the function
470 will return with the SelectInfo. The caller can then await a se‐
471 lect message, {'$socket', Socket, select, Info} (where Info is
472 the ref field from the SelectInfo), when a client connects (a
473 subsequent call to accept will then return the socket).
474
475 bind(Socket, Addr) -> {ok, Port} | {error, Reason}
476
477 Types:
478
479 Socket = socket()
480 Addr = sockaddr() | any | broadcast | loopback
481 Port = port_number()
482 Reason = inet:posix() | closed
483
484 Bind a name to a socket.
485
486 When a socket is created (with open), it has no address assigned
487 to it. bind assigns the address specified by the Addr argument.
488
489 The rules used for name binding vary between domains.
490
491 cancel(Socket, SelectInfo) -> ok | {error, Reason}
492
493 Types:
494
495 Socket = socket()
496 SelectInfo = select_info()
497 Reason = einval | closed | exself
498
499 Cancel an asynchronous request.
500
501 Call this function in order to cancel a previous asynchronous
502 call to, e.g. recv/3.
503
504 close(Socket) -> ok | {error, Reason}
505
506 Types:
507
508 Socket = socket()
509 Reason = errcode() | closed | timeout
510
511 Closes the socket.
512
513 Note:
514 Note that for e.g. protocol = tcp, most implementations doing a
515 close does not guarantee that any data sent is delivered to the
516 recipient before the close is detected at the remote side.
517
518 One way to handle this is to use the shutdown function
519 (socket:shutdown(Socket, write)) to signal that no more data is
520 to be sent and then wait for the read side of the socket to be
521 closed.
522
523
524 connect(Socket, SockAddr) -> ok | {error, Reason}
525
526 connect(Socket, SockAddr, Timeout) -> ok | {error, Reason}
527
528 Types:
529
530 Socket = socket()
531 SockAddr = sockaddr()
532 Timeout = timeout()
533 Reason = errcode() | closed | timeout
534
535 This function connects the socket to the address specied by the
536 SockAddr argument.
537
538 connect(Socket, SockAddr, Timeout :: nowait) ->
539 ok | {select, SelectInfo} | {error, Reason}
540
541 Types:
542
543 Socket = socket()
544 SockAddr = sockaddr()
545 SelectInfo = select_info()
546 Reason = errcode() | closed
547
548 This function connects the socket to the address specied by the
549 SockAddr argument.
550
551 In the case when its not possible to immediately establish a
552 connection, the function will return with the SelectInfo. The
553 caller can then await a select message, {'$socket', Socket, se‐
554 lect, Info} (where Info is the ref field from the SelectInfo, a
555 subsequent call to connect will then establish the connection).
556
557 getopt(Socket, Level :: otp, Key :: otp_socket_option()) ->
558 {ok, Value} | {error, Reason}
559
560 getopt(Socket, Level :: socket, Key :: socket_option()) ->
561 {ok, Value} | {error, Reason}
562
563 getopt(Socket, Level :: ip, Key :: ip_socket_option()) ->
564 {ok, Value} | {error, Reason}
565
566 getopt(Socket, Level :: ipv6, Key :: ipv6_socket_option()) ->
567 {ok, Value} | {error, Reason}
568
569 getopt(Socket, Level :: tcp, Key :: tcp_socket_option()) ->
570 {ok, Value} | {error, Reason}
571
572 getopt(Socket, Level :: udp, Key :: udp_socket_option()) ->
573 {ok, Value} | {error, Reason}
574
575 getopt(Socket, Level :: sctp, Key :: sctp_socket_option()) ->
576 {ok, Value} | {error, Reason}
577
578 Types:
579
580 Socket = socket()
581 Value = term()
582 Reason = inet:posix() | closed
583
584 Get an option on a socket.
585
586 What properties are valid depend both on Level and on what kind
587 of socket it is (domain, type and protocol).
588
589 See the socket options chapter of the users guide for more info.
590
591 Note:
592 Not all options are valid on all platforms. That is, even if
593 "we" support an option, that does not mean that the underlying
594 OS does.
595
596
597 getopt(Socket, Level, Key) -> ok | {ok, Value} | {error, Reason}
598
599 Types:
600
601 Socket = socket()
602 Level = integer()
603 Key = {NativeOpt, ValueSize}
604 NativeOpt = integer()
605 ValueSize = int | bool | integer() >= 0
606 Value = term()
607 Reason = inet:posix() | closed
608
609 Get an option on a socket.
610
611 When specifying Level as an integer, and therefor using "native
612 mode", it is *currently* up to the caller to know how to inter‐
613 pret the result.
614
615 For more info, see getopt above.
616
617 info(Socket) -> socket_info()
618
619 Types:
620
621 Socket = socket()
622
623 Get miscellaneous info about the socket.
624
625 The function returns a map with each info item as a key-value
626 binding. It reflects the "current" state of the socket.
627
628 Note:
629 In order to ensure data integrity, mutex'es are taken when
630 needed. So, do not call this function often.
631
632
633 listen(Socket) -> ok | {error, Reason}
634
635 listen(Socket, Backlog) -> ok | {error, Reason}
636
637 Types:
638
639 Socket = socket()
640 Backlog = integer()
641 Reason = inet:posix() | closed
642
643 Listen for connections on a socket.
644
645 number_of() -> integer() >= 0
646
647 Returns the number of active sockets.
648
649 open(FD) -> {ok, Socket} | {error, Reason}
650
651 open(FD, Opts) -> {ok, Socket} | {error, Reason}
652
653 Types:
654
655 FD = integer()
656 Opts =
657 #{domain => domain(),
658 type => type(),
659 protocol => protocol(),
660 dup => boolean(),
661 debug => boolean(),
662 use_registry => boolean()}
663 Socket = socket()
664 Reason = errcode()
665
666 Create an endpoint (socket) for communication based on an al‐
667 ready existing file descriptor. The function attempts to re‐
668 trieve domain, type and protocol from the system. This is how‐
669 ever not possible on all platforms, and in those cases it ex‐
670 pects it in Opts.
671
672 The Opts argument is intended for providing extra information
673 for the open call:
674
675 dup: boolean():
676 Shall the provided descriptor be duplicated (dup) or not.
677 Defaults to true.
678
679 debug: boolean():
680 Enable or disable debug during the open call.
681 Defaults to false.
682
683 domain: socket:domain():
684 Which domain is the descriptor of.
685
686 type: socket:type():
687 Which type is the descriptor of.
688
689 protocol: socket:protocol():
690 Which protocol is the descriptor of.
691
692 use_registry: boolean():
693 Enable or disable use of the socket registry for this
694 socket. This overrides the global value.
695 Defaults to the global value, see use_registry/1.
696
697 Note:
698 This function should be used with care!
699
700 On some platforms its necessary to provide the protocol as its
701 impossible to retrieve it.
702
703
704 open(Domain, Type) -> {ok, Socket} | {error, Reason}
705
706 open(Domain, Type, Protocol) -> {ok, Socket} | {error, Reason}
707
708 open(Domain, Type, Protocol, Opts) ->
709 {ok, Socket} | {error, Reason}
710
711 Types:
712
713 Domain = domain()
714 Type = type()
715 Protocol = default | protocol()
716 Opts =
717 #{netns => string(),
718 debug => boolean(),
719 use_registry => boolean()}
720 Socket = socket()
721 Reason = errcode()
722
723 Creates an endpoint (socket) for communication.
724
725 For some types there is a default protocol, indicated by de‐
726 fault, which it may be possible to specify. And for Domain = lo‐
727 cal, if a protocol is pecified, it must be default.
728
729 The Opts argument is intended for "other" options. The supported
730 option(s) are described below:
731
732 netns: string():
733 Used to set the network namespace during the open call. Only
734 supported on the Linux platform.
735
736 debug: boolean():
737 Enable or disable debug during the open call.
738 Defaults to false.
739
740 use_registry: boolean():
741 Enable or disable use of the socket registry for this
742 socket. This overrides the global value.
743 Defaults to the global value, see use_registry/1.
744
745 Note:
746 It may not be possible to specify the default protocol (except
747 when Domain = local). We need to be able to retreive the result‐
748 ing protocol, which is not possble on all platforms.
749
750
751 peername(Socket) -> {ok, SockAddr} | {error, Reason}
752
753 Types:
754
755 Socket = socket()
756 SockAddr = sockaddr()
757 Reason = inet:posix() | closed
758
759 Returns the address of the peer connected to the socket.
760
761 recv(Socket) -> {ok, Data} | {error, Reason}
762
763 recv(Socket, Length) -> {ok, Data} | {error, Reason}
764
765 recv(Socket, Length, Flags) -> {ok, Data} | {error, Reason}
766
767 recv(Socket, Length, Timeout) -> {ok, Data} | {error, Reason}
768
769 recv(Socket, Length, Flags, Timeout) ->
770 {ok, Data} | {error, Reason}
771
772 Types:
773
774 Socket = socket()
775 Length = integer() >= 0
776 Flags = recv_flags()
777 Timeout = timeout()
778 Data = binary()
779 Reason =
780 errcode() |
781 closed | timeout |
782 {errcode() | closed | timeout, Data :: binary()}
783
784 Receive a message from a socket.
785
786 There is a special case for the argument Length. If it is set to
787 zero (0), it means "give me everything you currently have".
788
789 recv(Socket, Length, Timeout :: nowait) ->
790 {ok, Data} |
791 {ok, {Data, SelectInfo}} |
792 {select, SelectInfo} |
793 {error, Reason}
794
795 recv(Socket, Length, Flags, Timeout :: nowait) ->
796 {ok, Data} |
797 {ok, {Data, SelectInfo}} |
798 {select, SelectInfo} |
799 {error, Reason}
800
801 Types:
802
803 Socket = socket()
804 Length = integer() >= 0
805 Flags = recv_flags()
806 Data = binary()
807 SelectInfo = select_info()
808 Reason =
809 errcode() | closed | {errcode() | closed, Data :: bi‐
810 nary()}
811
812 Receive a message from a socket.
813
814 There is a special case for the argument Length. If it is set to
815 zero (0), it means "give me everything you currently have".
816
817 In the case when there is no data waiting, the function will re‐
818 turn with the SelectInfo. The caller can then await a select
819 message, {'$socket', Socket, select, Info} (where Info is the
820 ref field from the SelectInfo), when data has arrived (a subse‐
821 quent call to recv will then return the data).
822
823 Note that if a length (> 0) is specified, and only part of that
824 amount of data is available, the function will return with that
825 data and the SelectInfo (if the caller don't want to wait for
826 the remaining data, it must immediately call the cancel/2 func‐
827 tion.)
828
829 recvfrom(Socket) -> {ok, {Source, Data}} | {error, Reason}
830
831 recvfrom(Socket, BufSz) -> {ok, {Source, Data}} | {error, Reason}
832
833 recvfrom(Socket, Flags, Timeout) ->
834 {ok, {Source, Data}} | {error, Reason}
835
836 recvfrom(Socket, BufSz, Flags) ->
837 {ok, {Source, Data}} | {error, Reason}
838
839 recvfrom(Socket, BufSz, Timeout) ->
840 {ok, {Source, Data}} | {error, Reason}
841
842 recvfrom(Socket, BufSz, Flags, Timeout) ->
843 {ok, {Source, Data}} | {error, Reason}
844
845 Types:
846
847 Socket = socket()
848 BufSz = integer() >= 0
849 Flags = recv_flags()
850 Timeout = timeout()
851 Source = sockaddr() | undefined
852 Data = binary()
853 Reason = errcode() | closed | timeout
854
855 Receive a message from a socket.
856
857 This function reads "messages", which means that regardless of
858 how much we want to read, it returns when we get a message (if
859 the buffer size is too small, the message will be truncated).
860
861 The BufSz argument basically defines the size of the receive
862 buffer. By setting the value to zero (0), the configured size
863 (setopt with Level = otp and Key = rcvbuf) is used.
864
865 It may be impossible to know what (buffer) size is appropriate
866 "in advance", and in those cases it may be convenient to use the
867 (recv) 'peek' flag. When this flag is provided, the message is
868 *not* "consumed" from the underlying buffers, so another
869 recvfrom call is needed, possibly with a then adjusted buffer
870 size.
871
872 recvfrom(Socket, Flags, Timeout :: nowait) ->
873 {ok, {Source, Data}} |
874 {select, SelectInfo} |
875 {error, Reason}
876
877 recvfrom(Socket, BufSz, Timeout :: nowait) ->
878 {ok, {Source, Data}} |
879 {select, SelectInfo} |
880 {error, Reason}
881
882 recvfrom(Socket, BufSz, Flags, Timeout :: nowait) ->
883 {ok, {Source, Data}} |
884 {select, SelectInfo} |
885 {error, Reason}
886
887 Types:
888
889 Socket = socket()
890 BufSz = integer() >= 0
891 Flags = recv_flags()
892 Source = sockaddr() | undefined
893 Data = binary()
894 SelectInfo = select_info()
895 Reason = errcode() | closed
896
897 Receive a message from a socket.
898
899 This function reads "messages", which means that regardless of
900 how much we want to read, it returns when we get a message (if
901 the buffer size is too small, the message will be truncated).
902
903 The BufSz argument basically defines the size of the receive
904 buffer. By setting the value to zero (0), the configured size
905 (setopt with Level = otp and Key = rcvbuf) is used.
906
907 It may be impossible to know what (buffer) size is appropriate
908 "in advance", and in those cases it may be convenient to use the
909 (recv) 'peek' flag. When this flag is provided, the message is
910 *not* "consumed" from the underlying buffers, so another
911 recvfrom call is needed, possibly with a then adjusted buffer
912 size.
913
914 In the case when there is no data waiting, the function will re‐
915 turn with the SelectInfo. The caller can then await a select
916 message, {'$socket', Socket, select, Info} (where Info is the
917 ref field from the SelectInfo), when data has arrived (a subse‐
918 quent call to recvfrom will then return the data).
919
920 recvmsg(Socket) -> {ok, MsgHdr} | {error, Reason}
921
922 recvmsg(Socket, Flags) -> {ok, MsgHdr} | {error, Reason}
923
924 recvmsg(Socket, Timeout) -> {ok, MsgHdr} | {error, Reason}
925
926 recvmsg(Socket, Flags, Timeout) -> {ok, MsgHdr} | {error, Reason}
927
928 recvmsg(Socket, BufSz, CtrlSz) -> {ok, MsgHdr} | {error, Reason}
929
930 recvmsg(Socket, BufSz, CtrlSz, Flags, Timeout) ->
931 {ok, MsgHdr} | {error, Reason}
932
933 Types:
934
935 Socket = socket()
936 BufSz = CtrlSz = integer() >= 0
937 Flags = recv_flags()
938 Timeout = timeout()
939 MsgHdr = msghdr()
940 Reason = errcode() | closed | timeout
941
942 Receive a message from a socket.
943
944 This function reads "messages", which means that regardless of
945 how much we want to read, it returns when we get a message.
946
947 The message will be delivered in the form of a msghdr(), which
948 may contain the source address (if socket not connected), a list
949 of cmsghdr_recv() (depends on what socket options have been set
950 and what the protocol and platform supports) and also a set of
951 flags, providing further info about the read.
952
953 The BufSz argument basically defines the size of the receive
954 buffer. By setting the value to zero (0), the configured size
955 (setopt with Level = otp and Key = rcvbuf) is used.
956
957 The CtrlSz argument basically defines the size of the receive
958 buffer for the control messages. By setting the value to zero
959 (0), the configured size (setopt with Level = otp) is used.
960
961 It may be impossible to know what (buffer) size is appropriate
962 "in advance", and in those cases it may be convenient to use the
963 (recv) 'peek' flag. When this flag is provided, the message is
964 *not* "consumed" from the underlying buffers, so another recvmsg
965 call is needed, possibly with a then adjusted buffer size.
966
967 recvmsg(Socket, Timeout :: nowait) ->
968 {ok, MsgHdr} | {select, SelectInfo} | {error, Reason}
969
970 recvmsg(Socket, Flags, Timeout :: nowait) ->
971 {ok, MsgHdr} | {select, SelectInfo} | {error, Reason}
972
973 recvmsg(Socket, BufSz, CtrlSz, Flags, Timeout :: nowait) ->
974 {ok, MsgHdr} | {select, SelectInfo} | {error, Reason}
975
976 Types:
977
978 Socket = socket()
979 BufSz = CtrlSz = integer() >= 0
980 Flags = recv_flags()
981 MsgHdr = msghdr()
982 SelectInfo = select_info()
983 Reason = errcode() | closed
984
985 Receive a message from a socket.
986
987 This function reads "messages", which means that regardless of
988 how much we want to read, it returns when we get a message.
989
990 The message will be delivered in the form of a msghdr(), which
991 may contain the source address (if socket not connected), a list
992 of cmsghdr_recv() (depends on what socket options have been set
993 and what the protocol and platform supports) and also a set of
994 flags, providing further info about the read.
995
996 The BufSz argument basically defines the size of the receive
997 buffer. By setting the value to zero (0), the configured size
998 (setopt with Level = otp and Key = rcvbuf) is used.
999
1000 The CtrlSz argument basically defines the size of the receive
1001 buffer for the control messages. By setting the value to zero
1002 (0), the configured size (setopt with Level = otp) is used.
1003
1004 It may be impossible to know what (buffer) size is appropriate
1005 "in advance", and in those cases it may be convenient to use the
1006 (recv) 'peek' flag. When this flag is provided, the message is
1007 *not* "consumed" from the underlying buffers, so another recvmsg
1008 call is needed, possibly with a then adjusted buffer size.
1009
1010 In the case when there is no data waiting, the function will re‐
1011 turn with the SelectInfo. The caller can then await a select
1012 message, {'$socket', Socket, select, Info} (where Info is the
1013 ref field from the SelectInfo), when data has arrived (a subse‐
1014 quent call to recvmsg will then return the data).
1015
1016 send(Socket, Data) -> ok | {error, Reason}
1017
1018 send(Socket, Data, Flags) -> ok | {error, Reason}
1019
1020 send(Socket, Data, Timeout) -> ok | {error, Reason}
1021
1022 send(Socket, Data, Flags, Timeout) -> ok | {error, Reason}
1023
1024 Types:
1025
1026 Socket = socket()
1027 Data = iodata()
1028 Flags = send_flags()
1029 Timeout = timeout()
1030 Reason =
1031 {errcode() | closed | timeout, Remaining :: integer() >=
1032 1}
1033
1034 Send a message on a connected socket.
1035
1036 send(Socket, Data, Timeout :: nowait) ->
1037 ok |
1038 {ok, {binary(), SelectInfo}} |
1039 {select, SelectInfo} |
1040 {ok, {RestData, SelectInfo}} |
1041 {error, Reason}
1042
1043 send(Socket, Data, Flags, Timeout :: nowait) ->
1044 ok |
1045 {select, SelectInfo} |
1046 {ok, {RestData, SelectInfo}} |
1047 {error, Reason}
1048
1049 Types:
1050
1051 Socket = socket()
1052 Data = iodata()
1053 Flags = send_flags()
1054 RestData = binary()
1055 SelectInfo = select_info()
1056 Reason = {errcode() | closed, Remaining :: integer() >= 1}
1057
1058 Send a message on a connected socket.
1059
1060 In the case when there is no room in the (system-) buffers, the
1061 function will return with the SelectInfo. The caller can then
1062 await a select message, {'$socket', Socket, select, Info} (where
1063 Info is the ref field from the SelectInfo), when there is room
1064 for more data (a subsequent call to send will then send the
1065 data).
1066
1067 Note that if not all the data was sent, the function will return
1068 with the remaining data and the SelectInfo (if the caller don't
1069 want to wait to be able to send the rest, it should immediately
1070 call the cancel/2 function.)
1071
1072 sendmsg(Socket, MsgHdr) -> ok | {ok, Remaining} | {error, Reason}
1073
1074 sendmsg(Socket, MsgHdr, Flags) -> ok | {error, Reason}
1075
1076 sendmsg(Socket, MsgHdr, Timeout) -> ok | {error, Reason}
1077
1078 sendmsg(Socket, MsgHdr, Flags, Timeout) ->
1079 ok | {ok, Remaining} | {error, Reason}
1080
1081 Types:
1082
1083 Socket = socket()
1084 MsgHdr = msghdr()
1085 Flags = send_flags()
1086 Timeout = timeout()
1087 Remaining = erlang:iovec()
1088 Reason = errcode() | closed | timeout
1089
1090 Send a message on a socket. The destination, if needed (socket
1091 not connected) is provided in the MsgHdr, which also contains
1092 the message to send, The MsgHdr may also contain an list of op‐
1093 tional cmsghdr_send() (depends on what the protocol and platform
1094 supports).
1095
1096 Unlike the send function, this one sends one message. This means
1097 that if, for whatever reason, its not possible to send the mes‐
1098 sage in one go, the function will instead return with the re‐
1099 maining data ({ok, Remaining}). Thereby leaving it up to the
1100 caller to decide what to do (retry with the remaining data of
1101 give up).
1102
1103 sendmsg(Socket, MsgHdr, Timeout :: nowait) ->
1104 ok | {ok, Remaining} | {error, Reason}
1105
1106 sendmsg(Socket, MsgHdr, Flags, Timeout :: nowait) ->
1107 ok | {ok, Remaining} | {error, Reason}
1108
1109 Types:
1110
1111 Socket = socket()
1112 MsgHdr = msghdr()
1113 Flags = send_flags()
1114 Remaining = erlang:iovec()
1115 Reason = errcode() | closed
1116
1117 Send a message on a socket. The destination, if needed (socket
1118 not connected) is provided in the MsgHdr, which also contains
1119 the message to send, The MsgHdr may also contain an list of op‐
1120 tional cmsghdr_send() (depends on what the protocol and platform
1121 supports).
1122
1123 Unlike the send function, this one sends one message. This means
1124 that if, for whatever reason, its not possible to send the mes‐
1125 sage in one go, the function will instead return with the re‐
1126 maining data ({ok, Remaining}). Thereby leaving it up to the
1127 caller to decide what to do (retry with the remaining data of
1128 give up).
1129
1130 In the case when there is no room in the (system-) buffers, the
1131 function will return with the SelectInfo. The caller can then
1132 await a select message, {'$socket', Socket, select, Info} (where
1133 Info is the ref field from the SelectInfo), when there is room
1134 for more data (a subsequent call to sendmsg will then send the
1135 data).
1136
1137 sendto(Socket, Data, Dest) -> ok | {error, Reason}
1138
1139 sendto(Socket, Data, Dest, Flags) -> ok | {error, Reason}
1140
1141 sendto(Socket, Data, Dest, Timeout) -> ok | {error, Reason}
1142
1143 sendto(Socket, Data, Dest, Flags, Timeout) -> ok | {error, Reason}
1144
1145 Types:
1146
1147 Socket = socket()
1148 Data = binary()
1149 Dest = sockaddr()
1150 Flags = send_flags()
1151 Timeout = timeout()
1152 Reason =
1153 {errcode() | closed | timeout, Remaining :: integer() >=
1154 1}
1155
1156 Send a message on a socket, to the specified destination.
1157
1158 sendto(Socket, Data, Dest, Timeout :: nowait) ->
1159 ok | {select, SelectInfo} | {error, Reason}
1160
1161 sendto(Socket, Data, Dest, Flags, Timeout :: nowait) ->
1162 ok |
1163 {ok, {binary(), SelectInfo}} |
1164 {select, SelectInfo} |
1165 {error, Reason}
1166
1167 Types:
1168
1169 Socket = socket()
1170 Data = binary()
1171 Dest = sockaddr()
1172 Flags = send_flags()
1173 SelectInfo = select_info()
1174 Reason = {errcode() | closed, Remaining :: integer() >= 1}
1175
1176 Send a message on a socket, to the specified destination.
1177
1178 In the case when there is no room in the (system-) buffers, the
1179 function will return with the SelectInfo. The caller can then
1180 await a select message, {'$socket', Socket, select, Info} (where
1181 Info is the ref field from the SelectInfo), when there is room
1182 for more data (a subsequent call to sendto will then send the
1183 data).
1184
1185 setopt(Socket, Level :: otp, Key :: otp_socket_option(), Value) ->
1186 ok | {error, Reason}
1187
1188 setopt(Socket, Level :: socket, Key :: socket_option(), Value) ->
1189 ok | {error, Reason}
1190
1191 setopt(Socket, Level :: ip, Key :: ip_socket_option(), Value) ->
1192 ok | {error, Reason}
1193
1194 setopt(Socket, Level :: ipv6, Key :: ipv6_socket_option(), Value) ->
1195 ok | {error, Reason}
1196
1197 setopt(Socket, Level :: tcp, Key :: tcp_socket_option(), Value) ->
1198 ok | {error, Reason}
1199
1200 setopt(Socket, Level :: udp, Key :: udp_socket_option(), Value) ->
1201 ok | {error, Reason}
1202
1203 setopt(Socket, Level :: sctp, Key :: sctp_socket_option(), Value) ->
1204 ok | {error, Reason}
1205
1206 setopt(Socket, Level, Key, Value) -> ok | {error, Reason}
1207
1208 Types:
1209
1210 Socket = socket()
1211 Level = Key = integer() >= 0
1212 Value = binary()
1213 Reason = inet:posix() | closed
1214
1215 Set an option on a socket.
1216
1217 What options are valid depend both on Level and on what kind of
1218 socket it is (domain, type and protocol).
1219
1220 See the socket options chapter of the users guide for more info.
1221
1222 Note:
1223 Not all options are valid on all platforms. That is, even if
1224 "we" support an option, that does not mean that the underlying
1225 OS does.
1226
1227
1228 Note:
1229 Sockets are set 'non-blocking' when created, so this option is
1230 *not* available (as it would adversely effect the Erlang VM to
1231 set a socket 'blocking').
1232
1233
1234 setopt(Socket, Level, Key, Value) -> ok | {error, Reason}
1235
1236 Types:
1237
1238 Socket = socket()
1239 Level = Key = integer() >= 0
1240 Value = binary()
1241 Reason = inet:posix() | closed
1242
1243 Set options on a socket.
1244
1245 When specifying Level as an integer, and therefor using "native
1246 mode", it is *currently* up to the caller to know how to encode
1247 the Value.
1248
1249 For more info, see setopt above.
1250
1251 shutdown(Socket, How) -> ok | {error, Reason}
1252
1253 Types:
1254
1255 Socket = socket()
1256 How = shutdown_how()
1257 Reason = inet:posix() | closed
1258
1259 Shut down all or part of a full-duplex connection.
1260
1261 sockname(Socket) -> {ok, SockAddr} | {error, Reason}
1262
1263 Types:
1264
1265 Socket = socket()
1266 SockAddr = sockaddr()
1267 Reason = inet:posix() | closed
1268
1269 Returns the current address to which the socket is bound.
1270
1271 supports() -> Supports
1272 supports(Key1 :: options) -> SupportsOptions
1273 supports(Key1 :: send_flags) -> SupportsSendFlags
1274 supports(Key1 :: recv_flags) -> SupportsRecvFlags
1275 supports(Key1 :: options, Key2 :: socket) -> SupportsOptionsSocket
1276 supports(Key1 :: options, Key2 :: ip) -> SupportsOptionsIP
1277 supports(Key1 :: options, Key2 :: ipv6) -> SupportsOptionsIPv6
1278 supports(Key1 :: options, Key2 :: tcp) -> SupportsOptionsTCP
1279 supports(Key1 :: options, Key2 :: udp) -> SupportsOptionsUDP
1280 supports(Key1 :: options, Key2 :: sctp) -> SupportsOptionsSCTP
1281
1282 Types:
1283
1284 Supports :: [{Feature, boolean()} | {send_flags, Support‐
1285 sSendFlags} | {recv_flags, SupportsRecvFlags} | {options,
1286 SupportsOptions}]
1287 Feature :: sctp | ipv6 | local | netns
1288 SupportsSendFlags :: [{send_flag(), boolean()}]
1289 SupportsRecvFlags :: [{recv_flag(), boolean()}]
1290 SupportsOptions :: [{socket, SupportsOptionsSocket} | {ip,
1291 SupportsOptionsIP} | {ipv6, SupportsOptionsIPv6} | {tcp, Sup‐
1292 portsOptionsTCP} | {udp, SupportsOptionsUDP} | {sctp, Sup‐
1293 portsOptionsSCTP}]
1294 SupportsOptionsSocket :: [{socket_option(), boolean()}]
1295 SupportsOptionsIP :: [{ip_socket_option(), boolean()}]
1296 SupportsOptionsIPv6 :: [{ipv6_socket_option(), boolean()}]
1297 SupportsOptionsTCP :: [{tcp_socket_option(), boolean()}]
1298 SupportsOptionsUDP :: [{udp_socket_option(), boolean()}]
1299 SupportsOptionsSCTP :: [{sctp_socket_option(), boolean()}]
1300
1301 This function retreives information about what the platform sup‐
1302 ports, such as if SCTP is supported, or which socket options are
1303 supported.
1304
1305 For keys other than the known the empty list is returned, Note
1306 that in a future version or on a different platform there might
1307 be more supported items.
1308
1309 is_supported(Key1 :: sctp | ipv6 | local | netns) -> boolean()
1310 is_supported(Key1 :: send_flags, Key2 :: SendFlag) -> boolean()
1311 is_supported(Key1 :: recv_flags, Key2 :: RecvFlag) -> boolean()
1312 is_supported(Key1 :: options, Key2 :: socket, Key3 :: SocketOption) ->
1313 boolean()
1314 is_supported(Key1 :: options, Key2 :: ip, Key3 :: IPSocketOption) ->
1315 boolean()
1316 is_supported(Key1 :: options, Key2 :: ipv6, Key3 :: IPv6SocketOption)
1317 -> boolean()
1318 is_supported(Key1 :: options, Key2 :: tcp, Key3 :: TCPSocketOption) ->
1319 boolean()
1320 is_supported(Key1 :: options, Key2 :: udp, Key3 :: UDPSocketOption) ->
1321 boolean()
1322 is_supported(Key1 :: options, Key2 :: sctp, Key3 :: SCTPSocketOption)
1323 -> boolean()
1324
1325 Types:
1326
1327 SocketOption :: socket_option()
1328 IPSocketOption :: ip_socket_option()
1329 IPv6SocketOption :: ipv6_socket_option()
1330 TCPSocketOption :: tcp_socket_option()
1331 UDPSocketOption :: udp_socket_option()
1332 SCTPSocketOption :: sctp_socket_option()
1333
1334 This function retreives information about what the platform sup‐
1335 ports, such as if SCTP is supported, or which socket options are
1336 supported.
1337
1338 For keys other than the known false is returned. Note that in a
1339 future version or on a different platform there might be more
1340 supported items.
1341
1342 use_registry(D :: boolean()) -> ok
1343
1344 Globally change if the socket registry is to be used or not.
1345 Note that its still possible to override this explicitly when
1346 creating an individual sockets, see open/2 or open/4 for more
1347 info (use the Extra argument).
1348
1349 which_sockets() -> [socket()]
1350
1351 which_sockets(FilterRule) -> [socket()]
1352
1353 Types:
1354
1355 FilterRule =
1356 inet | inet6 | stream | dgram | seqpacket | sctp | tcp |
1357 udp |
1358 pid() |
1359 fun((socket_info()) -> boolean())
1360
1361 Returns a list of all sockets, according to the filter rule.
1362
1363 There are several pre-made filter rule(s) and one general:
1364
1365 inet | inet6:
1366 Selection based on the domain of the socket.
1367 Only a subset is valid.
1368
1369 stream | dgram | seqpacket:
1370 Selection based on the type of the socket.
1371 Only a subset is valid.
1372
1373 sctp | tcp | udp:
1374 Selection based on the protocol of the socket.
1375 Only a subset is valid.
1376
1377 pid():
1378 Selection base on which sockets has this pid as Controlling
1379 Process.
1380
1381 fun((socket_info()) -> boolean()):
1382 The general filter rule.
1383 A fun that takes the socket info and returns a boolean()
1384 (true if the socket sould be included and false if should
1385 not).
1386
1388 client(Addr, SAddr, SPort) ->
1389 {ok, Sock} = socket:open(inet, stream, tcp),
1390 {ok, _} = socket:bind(Sock, #{family => inet,
1391 addr => Addr}),
1392 ok = socket:connect(Sock, #{family => inet,
1393 addr => SAddr,
1394 port => SPort}),
1395 Msg = list_to_binary("hello"),
1396 ok = socket:send(Sock, Msg),
1397 ok = socket:shutdown(Sock, write),
1398 {ok, Msg} = socket:recv(Sock),
1399 ok = socket:close(Sock).
1400
1401 server(Addr, Port) ->
1402 {ok, LSock} = socket:open(inet, stream, tcp),
1403 {ok, _} = socket:bind(LSock, #{family => inet,
1404 port => Port,
1405 addr => Addr}),
1406 ok = socket:listen(LSock),
1407 {ok, Sock} = socket:accept(LSock),
1408 {ok, Msg} = socket:recv(Sock),
1409 ok = socket:send(Sock, Msg),
1410 ok = socket:shutdown(Sock, write),
1411 ok = socket:close(Sock),
1412 ok = socket:close(LSock).
1413
1414
1415
1416
1417Ericsson AB kernel 7.3.1.1 socket(3)