TestSockets Source Code 6/6/07 v0.0003


The reason for this upload is to hopefully end up with template
demo source code, that future socket programmers can use as a
reference. (Linux rapidq I think is a different kettle of fish!)

I have created a QSocketEx.inc file which incorporates SOME of the
functions of RSocket.inc.  The remainder of RSocket.inc I don't
believe are necessary for any application.

A new FUNCTION Sock.WriteEx() incorporates the binary Write() SUB
plus a call to WSAGetLastError(), which returns the result back
to the calling function.  This fixed a bug whereby the server
"hung" on a premature exit of the client, mid-comms.



The projects I have "on the go" ALL now use Sock.Write/Read functions,
which deal with binary data ONLY.
I have moved away from the simpler WriteLine & ReadLine as I believe
in an ideal world they are ok to use (as per William's demo chatserver)
but in the REAL world (especially dealing with comms over the internet)
you must have tighter control over what you send but more IMPORTANTLY
what you receive.

My example code tracks the incoming IP address which can enable control
and/or logging of "bogus" connections. (My example code has a list of
valid IP addresses, in a text file, and ONLY accepts connections from
those IPs).

The server accepts a connection from a client and parses the initial
request.  The server "knows" the maximum data length that it
is EXPECTED to receive, and ONLY takes from the initial connection, a
maximum of that data length. This way, IF the server receives "bogus" data,
(for whatever reason), it terminates the connection and any further
data rxed after termination will (should) be flushed!

At any ONE time the server is busy with the ONE transaction. Any additional
requests (from another client on the network) will be queued waiting for
either the server to respond OR that client times out.


MY code:
Uses a very simple protocol between client and server.  This would be expanded
to suit other applications obviously.

This demo code could be easily expanded to enable the server to "listen" on
multiple ports too.


I have tested this across my local network (WinME - first client, Win98 - second
client, XP - server and a third client), using the source code as provided.


Last thing.  I use Sleep.MS 10 a lot, as a "yield" function.  It releases CPU
usage to the kernel (checked using Task Manager in XP). Not a big deal if
you are ONLY running the server on a PC. However, if the server is being run in 
the background and the PC is used for other things, this should have benefits
for the other programs.

I would appreciate any feedback, if you find this code of interest.


Cheers 

Don
d_homans@yahoo.com.au
6/6/07
