• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 :c:func:`uv_listen` or :c:func:`uv_tcp_connect`. That is,
85    a successful call to this function does not guarantee that the call
86    to :c:func:`uv_listen` or :c:func:`uv_tcp_connect` will succeed as well.
87
88    `flags` can contain ``UV_TCP_IPV6ONLY``, in which case dual-stack support
89    is disabled and only IPv6 is used.
90
91.. c:function:: int uv_tcp_getsockname(const uv_tcp_t* handle, struct sockaddr* name, int* namelen)
92
93    Get the current address to which the handle is bound. `name` must point to
94    a valid and big enough chunk of memory, ``struct sockaddr_storage`` is
95    recommended for IPv4 and IPv6 support.
96
97.. c:function:: int uv_tcp_getpeername(const uv_tcp_t* handle, struct sockaddr* name, int* namelen)
98
99    Get the address of the peer connected to the handle. `name` must point to
100    a valid and big enough chunk of memory, ``struct sockaddr_storage`` is
101    recommended for IPv4 and IPv6 support.
102
103.. c:function:: int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle, const struct sockaddr* addr, uv_connect_cb cb)
104
105    Establish an IPv4 or IPv6 TCP connection. Provide an initialized TCP handle
106    and an uninitialized :c:type:`uv_connect_t`. `addr` should point to an
107    initialized ``struct sockaddr_in`` or ``struct sockaddr_in6``.
108
109    On Windows if the `addr` is initialized to point to an unspecified address
110    (``0.0.0.0`` or ``::``) it will be changed to point to ``localhost``.
111    This is done to match the behavior of Linux systems.
112
113    The callback is made when the connection has been established or when a
114    connection error happened.
115
116    .. versionchanged:: 1.19.0 added ``0.0.0.0`` and ``::`` to ``localhost``
117        mapping
118
119.. seealso:: The :c:type:`uv_stream_t` API functions also apply.
120
121.. c:function:: int uv_tcp_close_reset(uv_tcp_t* handle, uv_close_cb close_cb)
122
123    Resets a TCP connection by sending a RST packet. This is accomplished by
124    setting the `SO_LINGER` socket option with a linger interval of zero and
125    then calling :c:func:`uv_close`.
126    Due to some platform inconsistencies, mixing of :c:func:`uv_shutdown` and
127    :c:func:`uv_tcp_close_reset` calls is not allowed.
128
129    .. versionadded:: 1.32.0
130
131.. c:function:: int uv_socketpair(int type, int protocol, uv_os_sock_t socket_vector[2], int flags0, int flags1)
132
133    Create a pair of connected sockets with the specified properties.
134    The resulting handles can be passed to `uv_tcp_open`, used with `uv_spawn`,
135    or for any other purpose.
136
137    Valid values for `flags0` and `flags1` are:
138
139      - UV_NONBLOCK_PIPE: Opens the specified socket handle for `OVERLAPPED`
140        or `FIONBIO`/`O_NONBLOCK` I/O usage.
141        This is recommended for handles that will be used by libuv,
142        and not usually recommended otherwise.
143
144    Equivalent to :man:`socketpair(2)` with a domain of AF_UNIX.
145
146    .. versionadded:: 1.41.0
147