Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

steel/tcp

tcp-abandon-port

Closes the output side of a tcp port, like close-output-port, but without telling the other end. The other end does not see the end of the stream until the connection itself is shut down or dropped.

(tcp-abandon-port port) -> void?

  • port : port? - a port from tcp-stream-reader

Examples

(define stream (tcp-connect "127.0.0.1:8080"))
(define port (tcp-stream-reader stream))
(tcp-abandon-port port)
(output-port? port) ;; => #false

tcp-accept

Accept a new incoming connection from this listener.

This function will block the calling thread until a new TCP connection is established. When established, the corresponding TcpStream? will be returned

Examples

(define listener (tcp-listen "127.0.0.1:8080"))

(tcp-accept listener) ;; => TcpStream?

tcp-accept-with-addr

Accept a new incoming connection from this listener.

This function will block the calling thread until a new TCP connection is established. When established, the corresponding TcpStream? will be returned with the remote address in a pair

Examples

(define listener (tcp-listen "127.0.0.1:8080"))

(tcp-accept-with-addr listener) ;; => (cons TcpStream? string?)

tcp-connect

Opens a TCP connection to a remote host.

addr is an address of the remote host as a string

If addr yields multiple addresses, tcp-connect will be attempted with each of the addresses until a connection is successful. If none of the addresses result in a successful connection, the error returned from the last connection attempt (the last address) is returned.

Examples

Open a TCP connection to 127.0.0.1:8080:

(tcp-connect "127.0.0.1:8080")

tcp-listen

Creates a new TcpListener? which will be bound to the specified address.

The returned listener is ready for accepting connections.

(tcp-listen addr) -> TcpListener?

Binding with a port number of 0 will request that the OS assigns a port to this listener. The port allocated can be queried via the tcp-listener-local-addr method.

If addr yields multiple addresses, bind will be attempted with each of the addresses until one succeeds and returns the listener. If none of the addresses succeed in creating a listener, the error returned from the last attempt (the last address) is returned.

Examples

Creates a TCP listener bound to 127.0.0.1:80:

(tcp-listen "127.0.0.1:80")

tcp-listener-local-addr

Returns the local socket address of this listener.

(tcp-listener-local-addr listener) -> string?

listener : TcpListener?

tcp-listener-set-non-blocking!

Puts the given TCP listener into non-blocking mode. Once set, tcp-accept will return immediately rather than waiting when no connection is pending.

(tcp-listener-set-non-blocking! listener) -> void?

  • listener : tcp-listener?

tcp-shutdown!

Shuts down both halves of this tcp connection.

This function will cause all pending and future I/O on the specified portions to return immediately with either an error or an okay result, dependong on if it was the write or read half.

Platform-specific behavior

Calling this function multiple times may result in different behavior, depending on the operating system. On Linux, the second call will return Ok(()), but on macOS, it will return an error wrapping the rust error ErrorKind::NotConnected. This may change in the future.

(tcp-shutdown! stream) ;; stream : TcpStream?

tcp-stream-buffered-reader

Get the reader half of this tcp stream as a buffered reader port.

(define my-port (tcp-connect "127.0.0.1:8080"))
(tcp-stream-buffered-reader my-port) ;; => port?

tcp-stream-reader

Get the reader half of this tcp stream as a port.

(define my-port (tcp-connect "127.0.0.1:8080"))
(tcp-stream-reader my-port) ;; => port?

tcp-stream-set-non-blocking!

Puts the given TCP stream into non-blocking mode. Once set, reads and writes on the stream return immediately rather than waiting when they would block.

(tcp-stream-set-non-blocking! stream) -> void?

  • stream : tcp-stream?

tcp-stream-set-read-timeout!

Sets how long a read on the given TCP stream will wait for bytes before giving up. Once set, a read that reaches the deadline behaves like a read on a non-blocking stream: the * readers will return a would-block object, the normal (non *) readers will return an error, and bytes that arrived before the deadline are kept.

(tcp-stream-set-read-timeout! stream ms) -> void?

  • stream : tcp-stream?
  • ms : (or/c int? #f) - milliseconds to wait, or #f to wait forever

Examples

(define stream (tcp-connect "127.0.0.1:8080"))
(tcp-stream-set-read-timeout! stream 5000)
(read-line* (tcp-stream-reader stream)) ;; => a line, or a would-block object

tcp-stream-writer

Get the writer half of this tcp stream as a port.

(define my-port (tcp-connect "127.0.0.1:8080"))
(tcp-stream-writer my-port) ;; => port?