modbus_tcp_listen.txt 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. modbus_tcp_listen(3)
  2. ====================
  3. NAME
  4. ----
  5. modbus_tcp_listen - create and listen a TCP Modbus socket (IPv4)
  6. SYNOPSIS
  7. --------
  8. *int modbus_tcp_listen(modbus_t *'ctx', int 'nb_connection');*
  9. DESCRIPTION
  10. -----------
  11. The *modbus_tcp_listen()* function shall create a socket and listen to maximum
  12. _nb_connection_ incoming connections on the specified IP address. The context
  13. _ctx _must be allocated and initialized with linkmb:modbus_new_tcp[3] before to
  14. set the IP address to listen, if IP address is o NULL, any addresses will be
  15. listen.
  16. RETURN VALUE
  17. ------------
  18. The *modbus_tcp_listen()* function shall return a new socket if
  19. successful. Otherwise it shall return -1 and set errno.
  20. EXAMPLE
  21. -------
  22. For detailed examples, see source files in tests directory:
  23. - unit-test-server.c, simple but handle only one connection
  24. - bandwith-server-many-up.c, handles several connections at once
  25. [source,c]
  26. -------------------
  27. ...
  28. /* To listen any addresses on port 502 */
  29. ctx = modbus_new_tcp(NULL, 502);
  30. /* Handle until 10 established connections */
  31. server_socket = modbus_tcp_listen(ctx, 10);
  32. /* Clear the reference set of socket */
  33. FD_ZERO(&refset);
  34. /* Add the server socket */
  35. FD_SET(server_socket, &refset);
  36. if (select(server_socket + 1, &refset, NULL, NULL, NULL) == -1) {
  37. }
  38. ...
  39. close(server_socket);
  40. modbus_free(ctx);
  41. -------------------
  42. SEE ALSO
  43. --------
  44. linkmb:modbus_new_tcp[3]
  45. linkmb:modbus_tcp_accept[3]
  46. linkmb:modbus_tcp_pi_listen[3]
  47. AUTHORS
  48. -------
  49. The libmodbus documentation was written by Stéphane Raimbault
  50. <stephane.raimbault@gmail.com>