SocketTips Revisited

May 29th, 2007

A number of years ago, we published a newsletter called SocketTips which provided general programming tips, how-to’s and code snippets to help developers make the most of SocketTools in their applications. We’re going to pick up where we left off, and start with covering some of the new features and functionality that was introduced in SocketTools 5.0. Each of these articles will be categorized as SocketTips, so they’ll be easy to search and reference in the future. If anyone has any ideas about articles they would like to see written, please let us know. For the first tip, we’ll talk about how to simplify your code using URLs with the FTP and HTTP components.

Coding Shortcuts Using URLs
For those of you using the SocketTools .NET Edition or Visual Edition, the typical sequence of method calls requires that you establish a connection using the Connect method, then perform the action that you want to take (for example, calling the GetFile method to download a file) and then terminating the connection by calling the Disconnect method.

For the File Transfer and Hypertext Transfer components, we’ve added the ability to use URLs without requiring that you explicitly establish a connection to the server first. New code can be written to take advantage of this feature, but existing code will continue to work as it always has.

Visual Basic Example
For example, here’s some Visual Basic code (using a Visual Edition component) that connects to our web server and downloads the index file, storing it on the local system:

Dim strHostName As String = “www.catalyst.com”
Dim strRemoteFile As String = “/index.html”
Dim strLocalFile As String = “c:\index.html”
Dim nError As Integer

nError = HttpClient1.Connect(strHostName, 80)
If nError = 0 Then
    nError = HttpClient1.GetFile(strLocalFile, strRemoteFile)
End If

If nError > 0 Then
    MsgBox(HttpClient1.LastErrorString, MsgBoxStyle.Exclamation)
    Exit Sub
End If

HttpClient1.Disconnect()

With SocketTools 5.0, that code can be reduced to the following:

Dim strLocalFile As String = “c:\index.html”
Dim strRemoteFile As String = “http://www.catalyst.com/index.html”
Dim nError As Integer

nError = HttpClient1.GetFile(strLocalFile, strRemoteFile)
If nError > 0 Then
    MsgBox(HttpClient1.LastErrorString, MsgBoxStyle.Exclamation)
    Exit Sub
End If

By providing a complete URL as the remote file that you want to download, you can omit the explicit calls to the Connect and Disconnect methods, and have the component automatically manage the connection for you. This applies to the GetFile and PutFile methods, as well as methods such as GetData, PutData, PostData and so on. This also applies to the same methods that are available in the SocketTools .NET Edition.

C/C++ Example
For developers who are using the SocketTools Library Edition, there’s some shortcuts available to you as well. The FtpDownloadFile, FtpUploadFile, HttpDownloadFile and HttpUploadFile functions are high-level “wrapper functions” around the API that enable you to upload and download files in a single function call. For example:

LPCTSTR lpszLocalFile = _T(”c:\\index.html”);
LPCTSTR lpszRemoteFile = _T(”http://www.catalyst.com/index.html”);
BOOL bResult;

bResult = HttpDownloadFile(lpszLocalFile,
                           lpszRemoteFile,
                           HTTP_TIMEOUT,
                           HTTP_OPTION_DEFAULT,
                           NULL, NULL, 0);

if (bResult == FALSE)
{
    DWORD dwError = HttpGetLastError();
    _tcprintf(_T(”Unable to download file, error %08lX\n”), dwError);
}

There still are some cases where you want to explicitly connect to the server. If you’re going to perform multiple file transfers, it’s more efficient to call the Connect method, call GetFile or PutFIle for each file, and then Disconnect. When you use URLs, then a connection will be established for each file that’s being transferred. By explicitly connecting to the server, you have more direct control over the “lifetime” of the client session. However, for single operations (downloading one file at a time, or posting data to a script on a web server) the ability to use URLs can simplify and reduce the amount of code that you need to write.

 

Leave a Reply

You must be logged in to post a comment.