Network Programming with Windows Sockets : In-Process Servers
Published 3 min readBy Programming4us Editorial Team
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
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.