socket -server procName port
| ||The name of a procedure to call when a connection is established.|
| ||The port to accept connections on.|
The procedure that gets called when a client attaches to the server will automatically receive three arguments:
|The channel that's been assigned to this connection|
|The IP address that this connection originated from. This can be used for simple access control.|
|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
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.
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
| ||The channel to watch for file events on.|
| || The direction of data flow to watch. May be |
| ||A script to invoke when the condition becomes true. (A channel has data or can be written to.)|
Modify the previous server by:
fileeventcommand to the
acceptClientprocedure to invoke a procedure named
readDatawhen data is available for reading.
readDataprocedure 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
| ||The address of the system to be connected to. May be a numeric address, or a fully qualified domain name.|
| ||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