Continuing on binary input and output ports
The server socket will listen on the integer port specified. If provided, the interface-address, a string specifies the address of a local interface to bind to.
If not provided, the port is bound on all available interfaces. An error is raised if the socket cannot be bound and set listening. Returns true if and only if the provided value is a server socket.
Accepts an incoming connection on the provided server-socket, and returns a TCP socket handle. This function will block until an incoming connection is made, or, if set, the socket timeout is exceeded. If the latter happens, an error will be raised. Sets the socket timeout on a socket. The socket can be either a server socket or connected socket.
In the former case, this value specifies the number of milliseconds that an accept-tcp-socket can wait before timing out. In the latter, the value specifies the number of milliseconds that can elapse during a read call before timing out.
This functionality can heavily depend on the underlying support in the JVM, including security settings, restrictions, installed certificates, etc. A separate constructor for sockets open-ssl-socket and listening sockets open-ssl-listener exist, and produce sockets which are used identically to TCP sockets as previously described.
Attempts to connect to the host at the given hostname or IP address encoded as a string, at the given TCP port specified as an integer, and establish an SSL connection using the default cipher suites and protocols available. The cipher suites, protocols, and modes which this server socket will accept are set with the following functions. Returns a list of strings naming the cipher suites which are enabled for this server socket.
Accepts an ssl server socket and a list of strings naming the cipher suites which should be available for negotiation with the remote end. The previous list is returned. Returns a list of strings naming the security protocols which are enabled for this server socket. Accepts an ssl server socket and a list of strings naming the protocols which should be available for negotiation with the remote end. The previous value is returned. Returns true if the SSL server socket will be in the rare client mode after accepting the connection, rather than the more common server mode.
Sets whether the SSL server socket will be in client mode when accepting new connections. The previousvalue is returned. Returns the necessity of the remote client to authenticate to this server socket. The returned values are either needed , indicating the remote must authenticate, wanted , indicating the remote will be requiested to authenticate but is not required to, or f , indicating the remote will not be requested to authenticate.
Sets the client authentication requirements, as described in get-client-auth above. One of the above values must be specified. The behavior of the char-ready?
The function will return t only when a previous datagram contained more bytes than were requested by the read operation that received it. Opens a UDP socket that listens on listen-port optionally bound to only the interface on interface-address.
If provided, datagram-size specifies the buffer size in bytes for receiving UDP datagrams. Datagrams larger than datagram-size are truncated to that size. If unspecified, the default datagram size is bytes. Opens a UDP socket for sending datagrams to the Internet host specified by remote-host , on port remote-port.
After obtaining a UDP socket, input and output ports can be obtained in the usual manner. It is an error to attempt to obtain an output-port from a listening UDP socket, or an input port from a sending UDP socket. UDP input ports behave as ordinary input ports. When a datagram arrives as a result of any read operation on the port, their entire contents are stored in a buffer of length datagram-size bytes. Successive read operations return data from that buffer until it is exhausted, at which point a read operation will cause the UDP socket to listen for another datagram.
UDP output ports should be treated with some care, however. If a UDP output port was obtained in auto-flush mode, each write operation to the output port will cause a new datagram to be sent. Control over the size of the datagram must be maintained by using a port that does not auto-flush, writing the desired data, and flushing once the amount of data that the user wants to occupy a single UDP datagram is reached.
The behavior of constructing very large UDP packets is undefined. The packet may be silently dropped or more likely fragmented at the IP layer. This allows a program to both send and receive to an IP multicast group.
The functions described here may produce an error if the operating system does not. A program wishing to use multicast UDP sockets must first obtain a multicast socket for either listening to a multicast group, or for both listening and sending to such a group.
Opens a multicast UDP socket that listens on listen-port optionally bound only to the interface addressed by interface-address. Opens a multicast UDP socket for sending datagrams to the specified multicast group , on the specified port. The returned socket will also be capable of listening to that group on the same port and optionally bound only to the interface addressed by interface-address , though the socket will not initially be a member of the group.
Once a sending socket has been obtained the second form , an output-port can be obtained in the usual manner, and datagrams can be immediately sent to the multicast group. To receive datagrams, sockets returned from both forms must join a multicast group. Class D IP addresses are in the range Causes the given multicast socket to join the group specified by the Internet address in group.
Once joined, read operations on an obtained input-port will be able to receive datagrams destined to that group. Causes the given multicast socket to leave the group specified by the Internet address in group. Read operations on any input ports obtained from this socket will no longer receive datagrams from that group.
A single multicast socket can simultaneously listen to more than one multicast group. A socket can only send to one group, however: Multicast packets are limited in extent by their time-to-live. Each time a multicast packet crosses a router, its ttl is decremented.
In this manner, one can send datagrams only to local networks or subnetworks, as well as more grand scopes. The TTL of a socket is set using set-multicast-ttl! Sets the multicast TTL of the given socket to ttl , an integer. All datagrams sent after this call will have their TTL set to the new value. Valid multicast TTLs are in the range 0 restricted to the same host to unlimited in scope. To add a new port type, one invokes a custom port constructors described below, passing procedures Scheme or otherwise which implement the operations required by that port.
Port implementors may also use an associated port-local value to coordinate state for some specialized ports. Creates a character-input-port whose functionality is implemented by the four provided fundamental procedures. Return a character as an integer value, or -1 if the end of stream has been reached.
Reads up to count characters into the given mutable string at position offset. The number of actual characters read are returned as an integer value, or -1 if the end of stream has been reached. Returns a non-false value if one or more characters are available for reading, false otherwise. Creates a binary-input-port whose functionality is implemented by the four provided fundamental procedures. Return a byte as an integer value, or -1 if the end of stream has been reached.
Reads up to count bytes into the given binary buffer at position offset. The number of actual bytes read are returned as an integer value, or -1 if the end of stream has been reached. Returns a count of the number of bytes which are available for reading. This number need not be accurate. Creates a character-output-port whose functionality is implemented by the four provided fundamental procedures. Writes a character represented as an integer value.
Writes count characters from the given string at position offset. Creates a binary-output-port whose functionality is implemented by the four provided fundamental procedures. Writes a byte represented as an integer value. Writes count bytes from the given binary buffer at position offset. Finally, the underlying procedures which implement a custom port can be retrieved with the following function:.
SISC includes a pretty-printer, a function that behaves like write , but introduces whitespace in order to make the output of data more readable to humans. Pretty-prints the specified value, either to the specified output-port, or to the console if no output-port is specified. The load procedure accepts URLs as well as ordinary file names. The file name passed to load is resolved relative to the current-url parameter.
During the execution of load , current-url is set to the loaded file, so that any invocations of load from the loaded file resolve the given file name relative to the file currently being loaded. For example, lets assume we have a web site that serves the following files:. The load function supports many types of files which contain executable code. When a pure source file is loaded, each s-expression is evaluated in sequence, exactly as if entered into the REPL one s-expression at a time.
SISC allows the location of input, i. This procedure behaves the same as open-input-file , except that it also tracks the location of the input. Returns the current location information associated with input-port. The return value is an association list containing the following keys: If no location information is available, f is returned.
SISC provides a mechanism for locating and subsequently loading named resources, such as Scheme source files, Scheme data files, property files. This allows Scheme programs to load resources in a portable, J2EE-compliant manner. Locates the resource named by string on the Java class path. If the resource cannot be found, f is returned. If the resource cannot be found, an empty list is returned.
The file-manipulation library provides access to a number of functions for reading and manipulating files and their attributes. The file-manipulation library acts on filenames in the same manner as other Scheme file related functions, e. The following functions act on both files and directories. With the exception of file-exists?
Attempts to remove the given file or directory. Some Scheme implementations close file ports automatically after they become inaccessible to the program or when the Scheme program exits, but it is best to close file ports explicitly whenever possible.
Closing a port that has already been closed has no effect. As a side effect of creating the textual port, binary-port is closed to prevent read or write operations on binary-port from interfering with read and write operations on the new textual port. The underlying byte stream remains open, however, until the textual port is closed.
If so, the procedure port-has-port-position? For binary ports, the position is always an exact nonnegative integer byte displacement from the start of the byte stream. For textual ports, the representation of a position is unspecified; it may not be an exact nonnegative integer and, even if it is, it may not represent either a byte or character displacement in the underlying stream.
The position may be used at some later time to reset the position if the port supports set-port-position! If so, the procedure port-has-set-port-position!? For binary ports, the position pos must be an exact nonnegative integer byte displacement from the start of the byte stream. For textual ports, the representation of a position is unspecified, as described in the entry for port-position above, but pos must be an appropriate position for the textual port, which is usually guaranteed to be the case only if it was obtained from a call to port-position on the same port.
If port is a binary output port and the position is set beyond the current end of the data in the underlying stream, the stream is not extended until new data is written at that position. If new data is written at that position, the contents of each intervening position is unspecified.
If procedure returns, call-with-port closes the port and returns the values returned by procedure. If procedure does not return, an implementation is free to close the port only if it can prove that the output port is no longer accessible.
The example below copies the contents of infile to outfile, overwriting outfile if it exists. Unless an error occurs, the ports are closed after the copy has been completed. Input Operations Procedures whose primary purpose is to read data from an input port are described in this section, along with related procedures for recognizing or creating end-of-file eof objects.
Otherwise, the next available byte is returned as an unsigned 8-bit quantity, i. In contrast to get-u8 , lookahead-u8 does not consume the byte it reads from the port, so if the next operation on the port is a call to lookahead-u8 or get-u8 , the same byte is returned.
If binary-input-port is at end of file, the eof object is returned. Otherwise, get-bytevector-n reads as if with get-u8 as many bytes, up to n , as are available before the port is at end of file, and returns a new nonempty bytevector containing these bytes. The port's position is advanced past the bytes read. Otherwise, get-bytevector-some reads as if with get-u8 at least one byte and possibly more, and returns a bytevector containing these bytes.
The maximum number of bytes read by this operation is implementation-dependent. Otherwise, get-bytevector-all reads as if with get-u8 all of the bytes available before the port is at end of file and returns a bytevector containing these bytes. Otherwise, the next available character is returned and the port's position is advanced one character. If textual-input-port is a transcoded port, the position in the underlying byte stream may advance by more than one byte.
Otherwise, the next available character is returned. In contrast to get-char , lookahead-char does not consume the character it reads from the port, so if the next operation on the port is a call to lookahead-char or get-char , the same character is returned. The procedure get-word defined below returns the next word from a textual input port as a string, where a word is defined to be a sequence of alphabetic characters. Since get-word does not know until it sees one character beyond the word that it has read the entire word, it uses lookahead-char to determine the next character and get-char to consume the character.
If textual-input-port is at end of file, the eof object is returned. Otherwise, get-string-n reads as if with get-char as many characters, up to n , as are available before the port is at end of file, and returns a new nonempty string containing these characters.
The port's position is advanced past the characters read. Otherwise, get-string-all reads as if with get-char all of the characters available before the port is at end of file and returns a string containing these characters. Otherwise, get-line reads as if with get-char all of the characters available before the port is at end of file or a line-feed character has been read and returns a string containing all but the line-feed character of the characters read.
If textual-input-port reaches end of file before the start of the external representation of a datum is found, the eof object is returned. Otherwise, get-datum reads as many characters as necessary, and no more, to parse a single datum, and returns a newly allocated object whose structure is determined by the external representation. Output Operations Procedures whose primary purpose is to send data to an output port are described in this section.
This procedure writes octet to binary-output-port , advancing the port's position by one byte. If not supplied, start defaults to zero and n defaults to the difference between the length of bytevector and start. This procedure writes the n bytes of bytevector starting at start to the port and advances the its position past the end of the bytes written. If textual-output-port is a transcoded port, the position in the underlying byte stream may advance by more than one byte.
If not supplied, start defaults to zero and n defaults to the difference between the length of string and start. This procedure writes the n characters of string starting at start to the port and advances the its position past the end of the characters written. If obj does not have an external representation as a datum, the behavior is unspecified. The precise external representation is implementation-dependent, but when obj does have an external representation as a datum, put-datum should produce a sequence of characters that can later be read by get-datum as an object equivalent in the sense of equal?
If called without an explicit port argument, the current input or output port is used, as appropriate. The following shows the use of open-input-file , read , and close-port in an expression that gathers a list of objects from the file named by "myfile. The following shows the use of open-output-file to write a list of objects the value of list-to-be-printed , separated by newlines, to the file named by "myfile.
If procedure returns, call-with-input-file closes the input port and returns the values returned by procedure. If procedure does not return, an implementation is free to close the input port only if it can prove that the input port is no longer accessible. The following example shows the use of call-with-input-file in an expression that gathers a list of objects from the file named by "myfile.
If procedure returns, call-with-output-file closes the output port and returns the values returned by procedure. If procedure does not return, an implementation is free to close the output port only if it can prove that the output port is no longer accessible. The following shows the use of call-with-output-file to write a list of objects the value of list-to-be-printed , separated by newlines, to the file named by "myfile.
If thunk returns, the port is closed and the current input port is restored to its old value. Before opening a file for input or output, by whatever method, the filename argument is converted to canonical form by calling the procedure merge-pathnames with filename as its sole argument. Thus, filename can be either a string or a pathname, and it is merged with the current pathname defaults to produce the pathname that is then opened.
Any file can be opened in one of two modes, normal or binary. Normal mode is for accessing text files, and binary mode is for accessing other files. Unix does not distinguish these modes, but Windows do: In binary mode, such ports do not perform newline translation. Unless otherwise mentioned, the procedures in this section open files in normal mode.
If the file cannot be opened, an error of type condition-type: Takes a filename referring to an output file to be created and returns an output port capable of writing characters to a new file by that name. The optional argument append?