xref: /netbsd-src/external/mit/libuv/dist/docs/src/tcp.rst (revision f0fde9902fd4d72ded2807793acc7bfaa1ebf243)
1
2.. _tcp:
3
4:c:type:`uv_tcp_t` --- TCP handle
5=================================
6
7TCP handles are used to represent both TCP streams and servers.
8
9:c:type:`uv_tcp_t` is a 'subclass' of :c:type:`uv_stream_t`.
10
11
12Data types
13----------
14
15.. c:type:: uv_tcp_t
16
17    TCP handle type.
18
19
20Public members
21^^^^^^^^^^^^^^
22
23N/A
24
25.. seealso:: The :c:type:`uv_stream_t` members also apply.
26
27
28API
29---
30
31.. c:function:: int uv_tcp_init(uv_loop_t* loop, uv_tcp_t* handle)
32
33    Initialize the handle. No socket is created as of yet.
34
35.. c:function:: int uv_tcp_init_ex(uv_loop_t* loop, uv_tcp_t* handle, unsigned int flags)
36
37    Initialize the handle with the specified flags. At the moment only the lower 8 bits
38    of the `flags` parameter are used as the socket domain. A socket will be created
39    for the given domain. If the specified domain is ``AF_UNSPEC`` no socket is created,
40    just like :c:func:`uv_tcp_init`.
41
42    .. versionadded:: 1.7.0
43
44.. c:function:: int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock)
45
46    Open an existing file descriptor or SOCKET as a TCP handle.
47
48    .. versionchanged:: 1.2.1 the file descriptor is set to non-blocking mode.
49
50    .. note::
51        The passed file descriptor or SOCKET is not checked for its type, but
52        it's required that it represents a valid stream socket.
53
54.. c:function:: int uv_tcp_nodelay(uv_tcp_t* handle, int enable)
55
56    Enable `TCP_NODELAY`, which disables Nagle's algorithm.
57
58.. c:function:: int uv_tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay)
59
60    Enable / disable TCP keep-alive. `delay` is the initial delay in seconds,
61    ignored when `enable` is zero.
62
63    After `delay` has been reached, 10 successive probes, each spaced 1 second
64    from the previous one, will still happen. If the connection is still lost
65    at the end of this procedure, then the handle is destroyed with a
66    ``UV_ETIMEDOUT`` error passed to the corresponding callback.
67
68.. c:function:: int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable)
69
70    Enable / disable simultaneous asynchronous accept requests that are
71    queued by the operating system when listening for new TCP connections.
72
73    This setting is used to tune a TCP server for the desired performance.
74    Having simultaneous accepts can significantly improve the rate of accepting
75    connections (which is why it is enabled by default) but may lead to uneven
76    load distribution in multi-process setups.
77
78.. c:function:: int uv_tcp_bind(uv_tcp_t* handle, const struct sockaddr* addr, unsigned int flags)
79
80    Bind the handle to an address and port. `addr` should point to an
81    initialized ``struct sockaddr_in`` or ``struct sockaddr_in6``.
82
83    When the port is already taken, you can expect to see an ``UV_EADDRINUSE``
84    error from either :c:func:`uv_tcp_bind`, :c:func:`uv_listen` or
85    :c:func:`uv_tcp_connect`. That is, a successful call to this function does
86    not guarantee that the call to :c:func:`uv_listen` or :c:func:`uv_tcp_connect`
87    will succeed as well.
88
89    `flags` can contain ``UV_TCP_IPV6ONLY``, in which case dual-stack support
90    is disabled and only IPv6 is used.
91
92.. c:function:: int uv_tcp_getsockname(const uv_tcp_t* handle, struct sockaddr* name, int* namelen)
93
94    Get the current address to which the handle is bound. `name` must point to
95    a valid and big enough chunk of memory, ``struct sockaddr_storage`` is
96    recommended for IPv4 and IPv6 support.
97
98.. c:function:: int uv_tcp_getpeername(const uv_tcp_t* handle, struct sockaddr* name, int* namelen)
99
100    Get the address of the peer connected to the handle. `name` must point to
101    a valid and big enough chunk of memory, ``struct sockaddr_storage`` is
102    recommended for IPv4 and IPv6 support.
103
104.. c:function:: int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle, const struct sockaddr* addr, uv_connect_cb cb)
105
106    Establish an IPv4 or IPv6 TCP connection. Provide an initialized TCP handle
107    and an uninitialized :c:type:`uv_connect_t`. `addr` should point to an
108    initialized ``struct sockaddr_in`` or ``struct sockaddr_in6``.
109
110    On Windows if the `addr` is initialized to point to an unspecified address
111    (``0.0.0.0`` or ``::``) it will be changed to point to ``localhost``.
112    This is done to match the behavior of Linux systems.
113
114    The callback is made when the connection has been established or when a
115    connection error happened.
116
117    .. versionchanged:: 1.19.0 added ``0.0.0.0`` and ``::`` to ``localhost``
118        mapping
119
120.. seealso:: The :c:type:`uv_stream_t` API functions also apply.
121
122.. c:function:: int uv_tcp_close_reset(uv_tcp_t* handle, uv_close_cb close_cb)
123
124    Resets a TCP connection by sending a RST packet. This is accomplished by
125    setting the `SO_LINGER` socket option with a linger interval of zero and
126    then calling :c:func:`uv_close`.
127    Due to some platform inconsistencies, mixing of :c:func:`uv_shutdown` and
128    :c:func:`uv_tcp_close_reset` calls is not allowed.
129
130    .. versionadded:: 1.32.0
131