Client / Server Communications
Library for Visual FoxPro
Programmer's Manual
(CSC4FP)
Version 6.0
August 17, 2009
This software is provided as-is.
There are no warranties, expressed or implied.
Copyright (C) 2009
All rights reserved
MarshallSoft Computing, Inc.
Post Office Box 4543
Huntsville AL 35815
Voice : 1.256.881.4630
web : www.marshallsoft.com
MARSHALLSOFT is a registered trademark of MarshallSoft Computing.
1 Introduction
1.1 Features2 CSC Library Overview
1.2 Documentation Set
1.3 Example Program
1.4 Installation
1.5 Uninstalling
1.6 Pricing
1.7 Updates
2.1 Dynamic Link Libraries3 Compiler Issues
2.2 Keycode
2.3 INCLUDE Files
2.4 FoxPro Forms
2.5 Dynamic Strings
2.6 Null Terminated Strings
2.7 Win32 STDCALL and DECLSPEC
2.8 Adding CSC4FP to a VFP Program
2.9 Example Protocol
2.10 Error Display
3.1 Compiling Programs4 Visual FoxPro Example Programs
3.2 Compiling to an Executable
3.3 Compiling CSC Source
5 Revision History
The Client / Server Communications Library for Visual FoxPro (CSC4FP) is a toolkit that allows software developers to quickly develop server and client TCP/IP and UDP applications in Visual FoxPro.
The Client / Server Communications Library (CSC) is a component DLL library used to create server and client programs that can communicate with each other across any TCP/IP or UDP network such as the Internet or a private network (intranet or LAN [local area net]). The CSC component library uses the Windows API (Application Programmer's Interface) and Windows sockets API for all communication.
The CSC library can be used to communicate with other CSC programs, or they can be used to communicate with other TCP programs such as DNS, POP3, SMTP, FTP, HTTP, etc.
The Client / Server Communications Library for Visual FoxPro (CSC4FP) component library supports all 32-bit versions of Visual FoxPro. CSC4FP includes several Visual FoxPro example programs demonstrating client/server protocols, including examples that connect to HTTP (web) and POP3 servers as well as encrypt files.
A Win32 DLL is provided. CSC4FP runs under all versions of Windows (95/98/ME/2000/2003/NT/XP/Vista). The Client / Server Communications Library SDK DLL (CSC32.DLL) can also be used from any language (Visual C++, .NET, Visual Basic, VB.NET, ACCESS, EXCEL, VBA, Delphi, COBOL, Xbase++, Visual dBase, etc.) capable of calling the Windows API.
The Client/Server Communications Programmer's Manual provides information needed to compile programs using CSC in a Visual FoxPro programming environment.
When comparing the Client/Server Communications Library against our competition, note that:
MarshallSoft also has versions of the Client/Server Communications Library for C/C++ (CSC4C), Visual Basic (CSC4VB) and Delphi (CSC4D). Each version of CSC uses the same DLL (CSC32.DLL). However, the examples provided for each version are written for the specified programming language.
All versions of the Client/Server Communications Library (CSC) can be downloaded from our web site at
http://www.marshallsoft.com/client-server-communication.htm
Our goal is to provide a robust communication component library that you and your customers can depend upon. A fully functional evaluation version is available. Contact us if you have any questions.
Some of the many features of the Client/Server Communications Library component are as follows:
CSC can also be used to communicate with other CSC programs, or used to communicate with other TCP programs such as DNS, POP3, SMTP, FTP, HTTP, etc.
A good selection of Visual FoxPro example programs with full source code is included. Refer to Section 6 for more details on each of the example projects.
cscver Displays CSC version and build
client Simple client example program.
server Server example program.
server2 Server example program (2 concurrent connections).
download Downloads text file from HTTP (web) server.
FileGet File server example that encrypts files.
FilePut File client example that decrypts files.
pop3stat Gets # emails waiting on POP3 server.
hello Form that displays CSC version and build.
uNetTime UDP client gets Network Time.
The complete set of documentation consists of three manuals in two formats. This is the first manual (CSC_4FP) in the set.
Each manual comes in two formats:
The CSC_4FP Programmer's Manual is the language specific (Visual FoxPro) manual. All language dependent programming issues are discussed in this manual. Information needed to compile programs in a Visual FoxPro environment is provided in this manual.
The CSC User's Manual (CSC_USR) discusses language independent issues. Information on Client / Server protocols as well as purchasing and license information is provided in the manual.
The CSC Reference Manual (CSC_REF) contains details on each individual CSC function.
All manuals can be viewed online at
http://www.marshallsoft.com/csc4fp.htm
The following code segment attempts to connect to the server.
DataSock = cscClient(@HostName, HostPort)
if DataSock < 0
? "ERROR: " + Str(DataSock) + " cscClient fails"
Buffer = Space(128)
BufLen = cscErrorText(DataSock, @Buffer, 128)
if BufLen > 0
? Left(Buffer, BufLen)
endif
Code = cscRelease()
return
endif
? "Connected to server"
Also see the example programs in the \CSC4FP\APPS sub-directory where CSC4FP was installed.
Uninstalling CSC4FP is very easy. CSC does NOT modify the registry. First, delete the CSC4FP project directory created when installing CSC4FP. Second, delete CSC32.DLL from the Windows directory, typically C:\WINDOWS for Windows 95/98/Me/2003/XP/VISTA or C:\WINNT for Windows NT/2000.
A developer license for the Client/Server Communications Library can be registered for $115 (or $195 with source code [ANSI C] to the library DLL). Purchasing details can be found in Section_1.4, "How to Purchase", of the CSC User's Manual (CSC_USR). See http://www.marshallsoft.com/csc_usr.htm#Section_1.4
Also see INVOICE.TXT or http://www.marshallsoft.com/order.htm
When a developer license is purchased for CSC, the developer will be sent a registered DLL plus a license file (CSCxxxxx.LIC). The license file can be used to update the registered DLL for a period of one year from purchase. Updates can be downloaded from
http://www.marshallsoft.com/update.htm
After one year, the developer license must be updated to be able to download updates. The license can be updated for $30 if ordered within one year of the original purchase (or previous update). Between one year and three years, licenses can be updated for $55. After three years, updates are $75. Updates to the source code can be purchased for $40.
Note that the registered CSC DLL does not expire (only the ability to download updates expires).
The Client/Server Communications Library (CSC) has been tested on multiple computers running Windows 95/98, Windows Me, Windows NT4, Windows 2000, Windows 2003, Windows XP and Windows Vista.
The CSC4FP library will work with all versions of 32-bit Visual FoxPro. The CSC32.DLL functions may be called by any Windows application program capable of calling the Windows API provided that the proper declaration file is used. CSC64.DLL is available to use with Win64 applications.
The SETUP installation program will copy the Lib's and DLL to the Windows directory. Refer to Section 1.4 "Installation". After SETUP is run, the CSC4FP files are copied to the directory specified (default \CSC4FP). Three sub-directories are created, as follows:
DOCS - All documentation files
APPS - All example code
DLLS - All DLL's
The Client/Server Communication Library component is a Win32 dynamic link library (DLL). A DLL is characterized by the fact that it need not be loaded until required by an application program and that only one copy of the DLL is necessary regardless of the number of application programs that use it. Contrast this to the traditional static library that is bound to each and every application that uses it at link time.
An important advantage that DLLs have over other "popular" library formats such as VBX or OCX is that DLLs are callable by all Windows applications. Since DLLs are the building blocks of the Windows Operating System, they will not be replaced by a "newer technology".
CSC32.DLL has a keycode encoded within it. The keycode is a 9 or 10 digit decimal number (unless it is 0), and will be found in the file KEYCODE.FOX. The keycode for the evaluation version is 0. You will receive a new key code when registering. The KEYCODE is passed to cscAttach.
The keycode is not the customer ID (which is a 4 or 5 digit number).
If an error message (value -74) is returned when calling cscAttach , it means that the keycode in the CSC application does not match the keycode in the DLL. After registering, it is best to remove the evaluation version of the CSC32.DLL from the Windows search.
All example programs include two files: KEYCODE.FOX and CSC32CON.FOX. The file CSC32CON.FOX contains all the necessary constants for CSC4FP, while the file KEYCODE.FOX contains the key code, as discussed in Section 2.2.
Since function declarations can not be in an INCLUDE file (at least through VFP version 9.0), they are listed in each program following the two INCLUDE files. The complete list of function declarations is also in the file CSC32FUN.FOX.
Due to the behavior of Visual FoxPro regarding INCLUDE files, it is strongly recommended that the INCLUDE files KEYCODE.FOX and CSC32CON.FOX be replaced with their contents in application programs (i.e., copy and paste contents) of the INCLUDE file.
CSC functions can be called from any Visual FoxPro code module, such as programs, classes, and forms. See the HELLO.SCT example form.
The Visual FoxPro language uses a technique known as "garbage collection" to manage string space at runtime, and may be called internally at any time by the FoxPro runtime, asynchronous to what you may be doing in your code.
When passing a string buffer to a DLL function into which text will be copied, it is strongly recommended that the local string be allocated immediately before use. For example:
BufLen = cscErrorText(ErrorCode, @Buffer, 128)
if BufLen > 0
? Left(Buffer, BufLen)
endif
This technique is not necessary for passing a string to a DLL function, only when passing a buffer to a DLL into which data is to be placed by the DLL function.
All strings returned from CSC functions are null terminated which means the end of the string is delimited by a Chr(0) character. These strings may be converted for FoxPro in one of two ways: (1) if the length of the string is known, use the FoxPro LEFT function: For example,
* get server IP address
TempBuffer = SPACE(TEMP_SIZE)
Code = fceGetString(0, FCE_GET_SERVER_IP, @TempBuffer, TEMP_SIZE)
if Code > 0
? "Server IP ", LEFT(TempBuffer, Code)
endif
If the length of the null terminated string is not known, use the FoxPro AT function to find the position of Chr(0).
CSC32 is written in ANSI C and is compiled using the _stdcall and _declspec keywords. This means that CSC32 uses the same calling conventions and file naming conventions as the Win32 API. In particular, function names are NOT decorated. There are no leading underscores or trailing "@size" strings added to function names.
The CSC32.DLL functions may be called from any Windows application program capable of calling the Windows API provided that the proper declaration file is used.
Several of the Client/Server Communications Library demonstration programs use the following example protocol:
"CSC Example Server"
The server responds with the following response strings when presented with the corresponding requests (REQ) from the client:
REQ Response String Request Example Response Example
WHO Sends name of the server. WHO W_SERVER
VER Sends server version #. VER 2.0
BYE OK (then disconnects) BYE OK
ECHO Sends string after "ECHO " ECHO Hello Hello
The above protocol is just an example. The programmer can create whatever protocol is
required. Request strings can be any length, although it is best to keep them as short as
possible..
Also refer to PROTOCOL.TXT in the \CSC4FP\APPS directory.
BUILD PROJECT C:\CSC4FP\APPS\CSCVER FROM C:\CSC4FP\APPS\CSCVER
BUILD EXE C:\CSC4FP\APPS\CSCVER FROM C:\CSC4FP\APPS\CSCVER
FoxPro executables require VFP500.DLL and VFP5ENU.DLL (ENglish User), and may have to be copied from the VFP CDROM. If you are using an earlier or later version of FoxPro than version 5.0, substitute the appropriate DLL's for the above.
The FoxPro output display window will disappear as soon as your executable completes. In order to allow the user to control when the display window disappears, add the following code to your application, just before the final return.
? " Type any key to exit..."
X = InKey(0)
The source code for the CSC DLL's is written in standard ANSI C (CSC32.C), and has been compiled using Microsoft Visual C++. The Win32 version is compiled with the STDCALL and DECLSPEC compiler keywords. Source code for the CSC library can be purchased at the same time as a CSC developer license is purchased.
CSC may also be compiled using Borland C/C++ or Watcom C/C++ compilers. If you recompile CSC32.C is compiled using Borland or Watcom compilers, the resulting DLL can only be used by applications compiled with the same compiler, unless the STDCALL and DECLSPEC keywords are specified. For more information on the C/C++ version of CSC, download the latest version of CSC4C from our web site at http://www.marshallsoft.com/client-server-communication.htm
All example programs are written for 32-bit FoxPro. Each has been tested and shows how to correctly use CSC functions. It suggested that the developer compile and run the example programs before developing an application using CSC4FP.
Because of the peculiarity of Visual FoxPro regarding INCLUDE files, it is highly recommended that the INCLUDE files KEYCODE.FOX and CSC32CON.FOX be replaced with their contents.
Refer to Section 3.1 above for information on compiling and linking the example programs. CSC functions may also be called from Visual FoxPro projects.
The CSCVER ("CSC Version") example program (CSCVER.PRG) displays the CSC version number. This is the first program to compile and build since it verifies that CSC32.DLL is installed properly.
The CLIENT example program (CLIENT.PRG) operates as a client that connects to the example server program (SERVER). Edit CLIENT.PRG with your host name or server's IP address before compiling. Start the CLIENT program after the SERVER program.
The SERVER example program (SERVER.PRG) operates as a server that accepts connections from the example client program (CLIENT). Edit SERVER.PRG with your host name or IP address before compiling.
The SERVER2 example program (SERVER2.PRG) operates as a server that accepts multiple connections from the example client program (CLIENT). Edit SERVER2.PRG with your host name or IP address before compiling.
SERVER2 accepts a maximum of two connections (clients) at any one time.
The FileGet example program (FileGet.PRG) operates as a SERVER, and receives files from the FilePut client. Files are decrypted when received. Edit FileGet.PRG with the host name or server IP address before compiling. Start FileGet before FilePut.
The FilePut example program (FilePut.PRG) operates as a CLIENT, and sends files to the FileGet server. Files are encrypted when sent. Edit FilePut.PRG with the host name or server IP address before compiling.
The FileGet2 example program (FileGet2.PRG) operates as a CLIENT, and receives files from the FilePut2 server. Files are decrypted when received. Edit FileGet2.PRG with the host name or server IP address before compiling. Start FileGet2 before FilePut2.
The FilePut2 example program (FilePut2.PRG) operates as a SERVER, and sends files to the FileGet2 client. Files are encrypted when sent. Edit FilePut2.PRG with the host name or server IP address before compiling.
The Download example client program (Download.PRG) connects to the MarshallSoft web site (HTTP server) and downloads a file from the ./files directory.
The POP3Stat example client program (POP3Stat.PRG) logs onto a POP3 account and returns the number of emails waiting. Edit POP3Stat.PRG with the POP3 Server name, user name and password before compiling.
The Hello example form displays the CSC version and build number when the command button is pressed.
From the VFP menu (File/Open), open the form HELLO.SCX (with "File of Type: Form"). When the "Form" menu tab appears on the VFP menu bar, choose "Run Form". This form can also be opened from the VFP command window by typing "modify form \csc4fp\apps\hello.scx".
uNetTime is an example UDP client that connects to a Network Time Server (on well known port 37) and gets the network time (seconds since 1 January 1900 GMT) from the server. The default server is
time-A.timefreq.bldrdoc.gov
CSC32.DLL is written in ANSI C. All language versions of CSC (C/C++, Visual Basic, Delphi, and FoxPro) use the same identical DLL.
Version 5.0: October 15, 2008
Initial Visual FoxPro release of CSC.
Version 6.0: August 17, 2009