Disponible uniquement en anglais
Sysquake Pro – Table of Contents
Sysquake for LaTeX – Table of Contents
Socket Functions
Socket functions enable communication with a server over TCP/IP. Services which can be accessed via TCP/IP include HTTP (most common protocol for WWW documents and Web services), SMTP (for sending e-mail), POP (for receiving mail), and telnet. Both TCP (where the client and the server are connected and communicate with streams of bytes in both directions) and UDP (connectionless exchange of packets without guarantee of transfer and order) are supported.
Functions described in this section include only those required for opening and configuring the connection. They correspond to fopen for files. Input and output are done with the following generic functions:
Function | Description |
---|---|
fclose | close the file |
fgetl | read a line |
fgets | read a line |
fprintf | write formatted data |
fread | read data |
fscanf | read formatted data |
fwrite | write data |
redirect | redirect output |
fread does not block if there is not enough data; it returns immediately whatever is available in the input buffer.
Functions
gethostbyname
Resolve host name.
Syntax
ip = gethostbyname(host)
Description
gethostbyname(host) gives the IP address of host in dot notation as a string.
Example
gethostbyname('localhost') 127.0.0.1
See also
gethostname
Get name of current host.
Syntax
str = gethostname
Description
gethostname gives the name of the current host as a string.
See also
socketaccept
Accept a connection request.
Syntax
fd = socketaccept(fds)
Description
socketaccept(fds) accepts a new connection requested by a client to the server queue created with socketservernew. Its argument fds is the file descriptor returned by socketservernew.
Once a connection has been opened, the file descriptor fd can be used with functions such as fread, fwrite, fscanf, and fprintf. The connection is closed with fclose.
See also
fclose, socketconnect, socketservernew, fread, fwrite, fscanf, fgetl, fgets, fprintf
socketconnect
Change UDP connection.
Syntax
socketconnect(fd, hostname, port)
Description
socketconnect(fd,hostname,port) changes the remote host and port of the UDP connection specified by fd. An attempt to use socketconnect on a TCP connection throws an error.
See also
socketnew
Create a new connection to a server.
Syntax
fd = socketnew(hostname, port, options) fd = socketnew(hostname, port)
Description
socketnew(hostname,port) creates a new TCP connection to the specified hostname and port and returns a file descriptor fd.
The third argument of socketnew(hostname,port,options) is a structure which contains configuration settings. It is set with socketset.
Once a connection has been opened, the file descriptor fd can be used with functions such as fread, fwrite, fscanf, and fprintf. The connection is closed with fclose.
Example
fd = socketnew('www.somewebserver.com', 80, ... socketset('TextMode',true)); fprintf(fd, 'GET %s HTTP/1.0\n\n', '/'); reply = fgets(fd) reply = HTTP/1.1 200 OK fclose(fd);
See also
fclose, socketset, socketconnect, socketservernew, fread, fwrite, fscanf, fgetl, fgets, fprintf
socketservernew
Create a new server queue for accepting connections from clients.
Syntax
fds = socketservernew(port, options) fds = socketservernew(port)
Description
socketservernew(hostname,port) creates a new TCP or UDP socket for accepting incoming connections. Connections from clients are accepted with socketaccept, which must provide as input argument the file descriptor returned by socketservernew. Using multiple threads, multiple connections can be accepted on the same port, using multiple socketaccept for one socketservernew.
The second argument of socketservernew(port,options) is a structure which contains configuration settings. It is set with socketset. Options are inherited by the connections established with socketaccept. On platforms where administrator authorizations are enforced, only an administrator account (root account) can listen to a port below 1024. Only one server can listen to the same port.
To stop listening to new connections, the socket is closed with fclose. The file descriptor returned by socketservernew can be used only with socketaccept and fclose.
Example
fds = socketservernew(8080); fd = socketaccept(fds); request = fscanf(fd, 'GET %s'); fprintf(fd, 'Your request is "%s"\n', request); fclose(fd); fclose(fds);
See also
fclose, socketset, socketaccept, socketnew
socketset
Configuration settings for sockets.
Syntax
options = socketset options = socketset(name1=value1, ...) options = socketset(name1, value1, ...) options = socketset(options0, name1, value1, ...)
Description
socketset(name1,value1,...) creates the option argument used by socketnew and socketservernew. Options are specified with name/value pairs, where the name is a string which must match exactly the names in the table below. Case is significant. Alternatively, options can be given with named arguments. Options which are not specified have a default value. The result is a structure whose fields correspond to each option. Without any input argument, socketset creates a structure with all the default settings. Note that socketnew also interprets the lack of an option argument, or the empty array [], as a request to use the default values.
When its first input argument is a structure, socketset adds or changes fields which correspond to the name/value pairs which follow.
Here is the list of permissible options:
Name | Default | Meaning |
---|---|---|
ListenQueue | 5 | queue size for incoming connections |
Proto | 'tcp' | protocol ('tcp' or 'udp') |
TextMode | true | text mode |
Timeout | 30 | timeout in seconds |
When TextMode is true, input CR and CR-LF sequences are converted to LF, and output LF is converted to CR-LF, to follow the requirements of many Internet protocols where lines are separated with CR-LF. Note that TextMode is true by default.
Example
socketset ListenQueue: 5 Proto: 'tcp' TextMode: true Timeout: 30
See also
socketnew, socketservernew, socketsetopt
socketsetopt
Settings change for sockets.
Syntax
socketsetopt(fd, name1=value1, ...) socketsetopt(fd, name1, value1, ...) socketsetopt(fd, options)
Description
socketsetopt(fd,name1,value1,...) changes the options for the socket identified by fd. Options are specified by pairs of name and value. They are the same as those valid with socketset. However, only TextMode and Timeout have an effect; other ones are ignored.
socketsetopt(fd,options) takes as second argument a structure of options created with socketset.