socket
command:
socket -server procName port
procName
| The name of a procedure to call when a connection is established. |
port
| The port to accept connections on. |
The procedure that gets called when a client attaches to the server will automatically receive three arguments:
channel
| The channel that's been assigned to this connection |
ipAddress
| The IP address that this connection originated from. This can be used for simple access control. |
port
| The port number assigned to this connection. This can be used to distinguish multiple connections from a single IP address. |
A server can be tested by connecting to it with the telnet
or putty
application.
Write a test server that will accept a connection, send a message and immediately close the channel.
Modify the previous server by adding a global associative array named
State
with an index named count
. When a
connection is made the server should report how many connections have
been made before closing the channel.
A server can't wait for one client to enter data, ignoring other clients, and it's wasteful of CPU resources to round-robin poll all the clients to see if any have input to process.
The fileevent
command makes a server event driven -
it will only read data when data is available for reading, instead of
polling or waiting for input.
fileevent channel direction script
channel
| The channel to watch for file events on. |
direction
| The direction of data flow to watch. May be readable
or writeable
|
script
| A script to invoke when the condition becomes true. (A channel has data or can be written to.) |
Modify the previous server by:
fileevent
command to the acceptClient
procedure to invoke a procedure named readData
when data is
available for reading.
readData
procedure should echo the data that
was received.
Modify the code from exercise 2 to save a list of client channels and transmit a message every second.
To open a client-side connection to a server:
socket IP_Address port
IP_Address
| The address of the system to be connected to. May be a numeric address, or a fully qualified domain name. |
port
| The port to connect to on the target machine. |
Write a client that will connect to the server in exercise 4, get and print 2 lines of data from the server, then close the channel.
Put the open/read/puts lines in a loop so that they will run 5 times.
You will probably need to add an after
command inside the
loop.