Network Programming with Windows Sockets : In-Process Servers

10/10/2010 3:15:23 PM
As mentioned previously, in-process servers are a major enhancement in serverSK. Program 1 shows how to write a DLL to provide these services. Two familiar functions are shown, a word counting function and a toupper function.
Program 1. commandIP: Sample In-Process Servers
/*                                       */
/* "In Process Server" commands to use with serverSK, etc.*/
/* */
/* There are several commands implemented as DLLs*/
/* Each command function must be a thread-safe function */
/* and take two parameters. The first is a string:*/
/* command arg1 arg2 ... argn (i.e.; a RESTRICTED command*/
/* line with no spaces or quotes in the command or args)*/
/* and the second is the file name for the output*/
/* The code is C, without decorated names*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static void ExtractToken (int, char *, char *);

__declspec (dllexport)
int __cdecl wcip (char * command, char * output_file)
/* word count; in process (ONE FILE ONLY)*/
/* Count the number of characters, works, and lines in*/
/* the file specified as the second token in "command"*/
/* NOTE: Simple version; results may differ from wc utility*/
FILE * fIn, *fOut;
int ch, c, nl, nw, nc;
char inputFile[256];

ExtractToken (1, command, inputFile);

fIn = fopen (inputFile, "r");
if (fIn == NULL) return 1;

ch = nw = nc = nl = 0;
while ((c = fgetc (fIn)) != EOF) {
if (c == '\0') break;
if (isspace(c) && isalpha(ch))
ch = c;
if (c == '\n')
fclose (fIn);
/* Write the results */
fOut = fopen (output_file, "w");
if (fOut == NULL) return 2;
fprintf (fOut, " %9d %9d %9d %s\n", nl, nw, nc, inputFile);
fclose (fOut);
return 0;

__declspec (dllexport)
int __cdecl toupperip (char * command, char * output_file)
/* convert input to upper case; in process*/
/* Input file is the second token ("toupperip" is the first)*/
FILE * fIn, *fOut;
int c;
char inputFile[256];

ExtractToken (1, command, inputFile);

fIn = fopen (inputFile, "r");
if (fIn == NULL) return 1;
fOut = fopen (output_file, "w");
if (fOut == NULL) return 2;

while ((c = fgetc (fIn)) != EOF) {
if (c == '\0') break;
if (isalpha(c)) c = toupper(c);
fputc (c, fOut);
fclose (fIn); fclose (fOut);
return 0;

static void ExtractToken (int it, char * command, char * token)
/* Extract token number "it" (first token is number 0)*/
/* from "command". Result goes in "token"*/
/* tokens are white space delimited*/
. . . (see the Examples file

By convention, the first parameter is the command line, while the second is the name of the output file. Beyond that, always remember that the function will execute in the same thread as the server thread, so there are strict requirements for thread safety, including but not limited to the following:

  • The functions should not change the process environment in any way. For example, if one of the functions changes the working directory, that change will affect the entire process.

  • Similarly, the functions should not redirect standard input or output.

  • Programming errors, such as allowing a subscript or pointer to go out of bounds or the stack to overflow, could corrupt another thread or the server process itself. More generally, the function should not generate any unhandled exception because the server will not be able to do anything other than shut down.

  • Resource leaks, such as failing to deallocate memory or to close handles, will ultimately affect the server application.

Processes do not have such stringent requirements because a process cannot normally corrupt other processes, and resources are freed when the process terminates. A typical development methodology, then, is to develop and debug a service as a process, and when it is judged to be reliable, convert it to a DLL.

Program 1 shows a small DLL library with two simple functions, wcip and toupperip, which have functionality from programs in previous chapters. The code is in C, avoiding C++ decorated names. These examples do not support Unicode as currently written.

  •  Network Programming with Windows Sockets : A Socket-Based Server with New Features
  •  Network Programming with Windows Sockets : A Socket-Based Client
  •  Network Programming with Windows Sockets : A Socket Message Receive Function
  •  Exchange Server 2010 : Operating Without Traditional Point-in-Time Backups
  •  Exchange Server 2010 : Performing Backup and Recovery for Mailbox Server Roles
  •  Exchange Server 2010 : Performing Backup and Recovery for Non-Mailbox Server Roles
  •  Exchange Server 2010 : Backup and Disaster Recovery Planning
  •  Changes to Backup and Restore in Exchange Server 2010
  •  Programming Windows Azure : Using the SDK and Development Storage
  •  Programming Windows Azure : Building a Storage Client
  •  Working with the REST API
  •  Excel Programmer : Fix Misteakes
  •  Excel Programmer : Change Recorded Code
  •  Excel Programmer : Record and Read Code
  •  Configuring Server Roles in Windows 2008 : New Roles in 2008
  •  Windows Server 2003 : Creating and Configuring Application Directory Partitions
  •  Windows Server 2003 : Configuring Forest and Domain Functional Levels
  •  Windows Server 2003 : Installing and Configuring Domain Controllers
  •  Manage Server Core
  •  Configure Server Core Postinstallation
    Top 10
    Fujifilm XF1 - The Stylish Shooter
    Nikon 1 V2 - Still Fast and Handles Better
    Asustor AS-604T 4-Bay NAS Review (Part 3)
    Asustor AS-604T 4-Bay NAS Review (Part 2)
    Asustor AS-604T 4-Bay NAS Review (Part 1)
    Toshiba Satellite U925t Review (Part 3)
    Toshiba Satellite U925t Review (Part 2)
    Toshiba Satellite U925t Review (Part 1)
    iBall Andi 4.5H - Pretty In White
    The HTC Butterfly - Full HD In 5 Inches Only
    Most View
    Windows 7 : Command-Line and Automation Tools - Batch Files, Windows PowerShell
    IIS 7.0 : Managing Configuration - Backing Up Configuration, Using Configuration History & Exporting and Importing Configuration
    SharePoint 2010: Business Connectivity Services - The Secure Store Service (part 2) - Creating a Secure Store Service Application for Impersonating
    Reporting Services with SQL Azure : Starting a SQL Azure–Based Report
    Windows Server 2008 : Transport-Level Security - Active Directory Rights Management Services
    Who’s Watching You? (Part 4)
    Automating Blind SQL Injection Exploitation
    Partitioning Disks and Preparing Them for Use in Vista
    AMD Radeon HD 7850 2GB vs. Nvidia GeForce GTX 660 2GB vs. AMD Radeon HD 7870 2GB (Part 1)
    SharePoint 2010 : Workflow Modeling and Development Tools (part 1) - Microsoft Visio 2010 & SharePoint Designer 2010
    Sony VAIO Duo 11 - The Notebook-Tablet Hybrid
    Programming .NET Components : Serialization Events (part 3) - Type-Version Tolerance
    All You Need To Know About iOS 6 (Part 3)
    Windows Phone 8 Operating System Review – Part2
    Transferring Bookmarks With Internet Usernames And Passwords To A New PC
    Advanced ASP.NET : Data Caching (part 1) - Adding Items to the Cache & A Simple Cache Test
    Asus Rog Tytan CG8565 - Clash Of The Tytan
    2012 - The Year to Come (Part 2)
    Compact System Cameras : Samsung NX200, Nikon 1J1, Olympus PEN E-PM1, Panasonic GX1, Sony NEX-5N
    Nokia Lumia 620 Review - Basic Smartphone With Good Performance And Stunning Design (Part 1)