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.
Resolve host name.
ip = gethostbyname(host)
gethostbyname(host) gives the IP address of host in dot notation as a string.
gethostbyname('localhost')
127.0.0.1
Get name of current host.
str = gethostname
gethostname gives the name of the current host as a string.
Accept a connection request.
fd = socketaccept(fds)
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.
fclose, socketconnect, socketservernew, fread, fwrite, fscanf, fgetl, fgets, fprintf
Change UDP connection.
socketconnect(fd, hostname, port)
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.
Create a new connection to a server.
fd = socketnew(hostname, port, options) fd = socketnew(hostname, port)
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.
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);
fclose, socketset, socketconnect, socketservernew, fread, fwrite, fscanf, fgetl, fgets, fprintf
Create a new server queue for accepting connections from clients.
fds = socketservernew(port, options) fds = socketservernew(port)
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.
fds = socketservernew(8080); fd = socketaccept(fds); request = fscanf(fd, 'GET %s'); fprintf(fd, 'Your request is "%s"\n', request); fclose(fd); fclose(fds);
fclose, socketset, socketaccept, socketnew
Configuration settings for sockets.
options = socketset options = socketset(name1, value1, ...) options = socketset(options0, name1, value1, ...)
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. 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.
socketset ListenQueue: 5 Proto: 'tcp' TextMode: true Timeout: 30
socketnew, socketservernew, socketsetopt
Settings change for sockets.
socketsetopt(fd, name1, value1, ...) socketsetopt(fd, options)
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.