|
| 1 | +TCP Client |
| 2 | +========== |
| 3 | + |
| 4 | +.. include:: ../refs/software.tcp.client.ref |
| 5 | + |
| 6 | +UiFlow2 Example |
| 7 | +--------------- |
| 8 | + |
| 9 | +simple client |
| 10 | +^^^^^^^^^^^^^ |
| 11 | + |
| 12 | +Open the |cores3_tcp_client_example.m5f2| project in UiFlow2. |
| 13 | + |
| 14 | +This example creates a TCP client that sends data to a server. |
| 15 | + |
| 16 | +UiFlow2 Code Block: |
| 17 | + |
| 18 | + |cores3_tcp_client_example.png| |
| 19 | + |
| 20 | +Example output: |
| 21 | + |
| 22 | + None |
| 23 | + |
| 24 | +MicroPython Example |
| 25 | +------------------- |
| 26 | + |
| 27 | +simple client |
| 28 | +^^^^^^^^^^^^^ |
| 29 | + |
| 30 | +This example creates a TCP client that sends data to a server. |
| 31 | + |
| 32 | +MicroPython Code Block: |
| 33 | + |
| 34 | + .. literalinclude:: ../../../examples/softwave/tcp/cores3_tcp_client_example.py |
| 35 | + :language: python |
| 36 | + :linenos: |
| 37 | + |
| 38 | +Example output: |
| 39 | + |
| 40 | + None |
| 41 | + |
| 42 | + |
| 43 | +**API** |
| 44 | +------- |
| 45 | + |
| 46 | +.. method:: socket.socket() |
| 47 | + socket.connect(address) |
| 48 | + :no-index: |
| 49 | + |
| 50 | + Create a socket object and connect to the server. |
| 51 | + |
| 52 | + :param tuple address: A tuple containing the server IP address and port number. |
| 53 | + :return: A socket object. |
| 54 | + :rtype: socket.socket |
| 55 | + |
| 56 | + UiFlow2 Code Block: |
| 57 | + |
| 58 | + |init.png| |
| 59 | + |
| 60 | + MicroPython Code Block: |
| 61 | + |
| 62 | + .. code-block:: python |
| 63 | +
|
| 64 | + import socket |
| 65 | +
|
| 66 | + tcpc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 67 | + tcpc.connect(('192.168.8.236', 8001)) |
| 68 | +
|
| 69 | +
|
| 70 | +.. method:: socket.close() |
| 71 | + :no-index: |
| 72 | + |
| 73 | + Close the socket connection. |
| 74 | + |
| 75 | + UiFlow2 Code Block: |
| 76 | + |
| 77 | + |close.png| |
| 78 | + |
| 79 | + MicroPython Code Block: |
| 80 | + |
| 81 | + .. code-block:: python |
| 82 | +
|
| 83 | + tcpc.close() |
| 84 | +
|
| 85 | +.. method:: socket.recv(bufsize) |
| 86 | + :no-index: |
| 87 | + |
| 88 | + Receive data from the socket. |
| 89 | + |
| 90 | + :param int bufsize: The maximum amount of data to be received at once. |
| 91 | + :return: The received data. |
| 92 | + :rtype: bytes |
| 93 | + |
| 94 | + UiFlow2 Code Block: |
| 95 | + |
| 96 | + |recv.png| |
| 97 | + |
| 98 | + MicroPython Code Block: |
| 99 | + |
| 100 | + .. code-block:: python |
| 101 | +
|
| 102 | + data = tcpc.recv(1024) |
| 103 | + print(data) |
| 104 | +
|
| 105 | +
|
| 106 | +.. method:: socket.read() |
| 107 | + :no-index: |
| 108 | + |
| 109 | + Read data from the socket. |
| 110 | + |
| 111 | + :return: The received data. |
| 112 | + :rtype: bytes |
| 113 | + |
| 114 | + UiFlow2 Code Block: |
| 115 | + |
| 116 | + |read.png| |
| 117 | + |
| 118 | + MicroPython Code Block: |
| 119 | + |
| 120 | + .. code-block:: python |
| 121 | +
|
| 122 | + data = tcpc.read() |
| 123 | + print(data) |
| 124 | +
|
| 125 | +.. method:: socket.readline() |
| 126 | + :no-index: |
| 127 | + |
| 128 | + Read a line, ending in a newline character. |
| 129 | + |
| 130 | + :return: the line read. |
| 131 | + :rtype: str |
| 132 | + |
| 133 | + UiFlow2 Code Block: |
| 134 | + |
| 135 | + |readline.png| |
| 136 | + |
| 137 | + MicroPython Code Block: |
| 138 | + |
| 139 | + .. code-block:: python |
| 140 | +
|
| 141 | + data = tcpc.readline() |
| 142 | + print(data) |
| 143 | +
|
| 144 | +
|
| 145 | +.. method:: socket.send(data) |
| 146 | + :no-index: |
| 147 | + |
| 148 | + Send data to the socket. The socket must be connected to a remote socket. |
| 149 | + |
| 150 | + :param data: The data to be sent. |
| 151 | + :type data: bytes | str |
| 152 | + :return: The number of bytes sent. |
| 153 | + :rtype: int |
| 154 | + |
| 155 | + UiFlow2 Code Block: |
| 156 | + |
| 157 | + |send.png| |
| 158 | + |
| 159 | + MicroPython Code Block: |
| 160 | + |
| 161 | + .. code-block:: python |
| 162 | +
|
| 163 | + tcpc.send(b'Hello, World!') |
| 164 | + tcpc.send('Hello, World!') |
| 165 | +
|
| 166 | +
|
| 167 | +.. method:: socket.write(data) |
| 168 | + :no-index: |
| 169 | + |
| 170 | + Write the buffer of bytes to the socket. This function will try to write all |
| 171 | + data to a socket (no “short writes”). This may be not possible with a |
| 172 | + non-blocking socket though, and returned value will be less than the length |
| 173 | + of buf. |
| 174 | + |
| 175 | + :param data: The data to be sent. |
| 176 | + :type data: bytes | str |
| 177 | + :return: The number of bytes sent. |
| 178 | + :rtype: int |
| 179 | + |
| 180 | + UiFlow2 Code Block: |
| 181 | + |
| 182 | + |write.png| |
| 183 | + |
| 184 | + MicroPython Code Block: |
| 185 | + |
| 186 | + .. code-block:: python |
| 187 | +
|
| 188 | + tcpc.write(b'Hello, World!') |
| 189 | + tcpc.write('Hello, World!') |
| 190 | +
|
| 191 | +
|
| 192 | +.. method:: socket.setblocking(flag) |
| 193 | + :no-index: |
| 194 | + |
| 195 | + Set blocking or non-blocking mode of the socket: if flag is false, the |
| 196 | + socket is set to non-blocking, else to blocking mode. |
| 197 | + |
| 198 | + This method is a shorthand for certain `settimeout()` calls: |
| 199 | + |
| 200 | + * ``sock.setblocking(True)`` is equivalent to ``sock.settimeout(None)`` |
| 201 | + * ``sock.setblocking(False)`` is equivalent to ``sock.settimeout(0)`` |
| 202 | + |
| 203 | + :param bool flag: If True, the socket will be in blocking mode. If False, the socket will be in non-blocking mode. |
| 204 | + :return: None |
| 205 | + :rtype: None |
| 206 | + |
| 207 | + UiFlow2 Code Block: |
| 208 | + |
| 209 | + |setblocking.png| |
| 210 | + |
| 211 | + MicroPython Code Block: |
| 212 | + |
| 213 | + .. code-block:: python |
| 214 | +
|
| 215 | + tcpc.setblocking(True) |
| 216 | + tcpc.setblocking(False) |
| 217 | +
|
| 218 | +
|
| 219 | +.. method:: socket.settimeout(timeout) |
| 220 | + :no-index: |
| 221 | + |
| 222 | + Set a timeout on blocking socket operations. The value argument can be a |
| 223 | + nonnegative floating point number expressing seconds, or None. If a |
| 224 | + non-zero value is given, subsequent socket operations will raise an OSError |
| 225 | + exception if the timeout period value has elapsed before the operation has |
| 226 | + completed. If zero is given, the socket is put in non-blocking mode. If None |
| 227 | + is given, the socket is put in blocking mode. |
| 228 | + |
| 229 | + :param float timeout: The timeout value in seconds. If None, the socket is put in blocking mode. |
| 230 | + :return: None |
| 231 | + :rtype: None |
| 232 | + |
| 233 | + UiFlow2 Code Block: |
| 234 | + |
| 235 | + |settimeout.png| |
| 236 | + |
| 237 | + MicroPython Code Block: |
| 238 | + |
| 239 | + .. code-block:: python |
| 240 | +
|
| 241 | + tcpc.settimeout(5) |
| 242 | + tcpc.settimeout(None) |
0 commit comments