socket.texi 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. @node TCP & UDP sockets
  2. @section TCP & UDP sockets
  3. @cindex networking
  4. @stindex sockets
  5. @stindex udp-sockets
  6. Scheme48 provides a simple facility for TCP & UDP sockets. Both the
  7. structures @code{sockets} and @code{udp-sockets} export several general
  8. socket-related procedures:
  9. @deffn procedure close-socket socket @returns{} unspecified
  10. @deffnx procedure socket-port-number socket @returns{} integer
  11. @deffnx procedure get-host-name @returns{} string
  12. @code{Close-socket} closes @var{socket}, which may be any type of
  13. socket. @code{Socket-port-number} returns the port number through
  14. which @var{socket} is communicating. @code{Get-host-name} returns the
  15. network name of the current machine.
  16. @strong{Note:} Programmers should be wary of storing the result of a
  17. call to @code{get-host-name} in a dumped heap image, because the actual
  18. machine's host name may vary from invocation to invocation of the
  19. Scheme48 VM on that image, since heap images may be resumed on multiple
  20. different machines.
  21. @end deffn
  22. @subsection TCP sockets
  23. @stindex sockets
  24. The @code{sockets} structure provides simple TCP socket facilities.
  25. @deffn procedure open-socket [port-number] @returns{} socket
  26. @deffnx procedure socket-accept socket @returns{} [input-port output-port]
  27. The server interface. @code{Open-socket} creates a socket that listens
  28. on @var{port-number}, which defaults to a random number above 1024.
  29. @code{Socket-accept} blocks until there is a client waiting to be
  30. accepted, at which point it returns two values: an input port & an
  31. output port to send & receive data to & from the client.
  32. @end deffn
  33. @deffn procedure socket-client host-name port-number @returns{} [input-port output-port]
  34. Connects to the server at @var{port-number} denoted by the machine name
  35. @var{host-name} and returns an input port and an output port for
  36. sending & receiving data to & from the server. @code{Socket-client}
  37. blocks the current thread until the server accepts the connection
  38. request.
  39. @end deffn
  40. @subsection UDP sockets
  41. @stindex udp-sockets
  42. The @code{udp-sockets} structure defines a UDP socket facility.
  43. @deffn procedure open-udp-socket [port-number] @returns{} socket
  44. Opens a UDP socket on @var{port-number}, or a random port number if
  45. none was passed. @code{Open-udp-socket} returns two values: an input
  46. UDP socket and an output UDP socket.
  47. @end deffn
  48. @deffn procedure udp-send socket address buffer count @returns{} count-sent
  49. @deffnx procedure udp-receive socket buffer @returns{} [count-received remote-address]
  50. @code{Udp-send} attempts to send @var{count} elements from the string
  51. or byte vector @var{buffer} from the output UDP socket @var{socket} to
  52. the UDP address @var{address}, and returns the number of octets it
  53. successfully sent. @code{Udp-receive} receives a UDP message from
  54. @var{socket}, reading it into @var{buffer} destructively. It returns
  55. two values: the number of octets read into @var{buffer} and the address
  56. whence the octets came.
  57. @end deffn
  58. @deffn procedure lookup-udp-address name port @returns{} udp-address
  59. @deffnx procedure udp-address? object @returns{} boolean
  60. @deffnx procedure udp-address-address address @returns{} c-byte-vector
  61. @deffnx procedure udp-address-port address @returns{} port-number
  62. @deffnx procedure udp-address-hostname address @returns{} string-address
  63. @code{Lookup-udp-address} returns a UDP address for the machine name
  64. @var{name} at the port number @var{port}. @code{Udp-address?} is the
  65. disjoint type predicate for UDP addresses. @code{Udp-address-address}
  66. returns a byte vector that contains the C representation of
  67. @var{address}, suitable for passing to C with Scheme48's C FFI.
  68. @code{Udp-address-port} returns the port number of @var{address}.
  69. @code{Udp-address-hostname} returns a string representation of the IP
  70. address of @var{address}.
  71. @end deffn