Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Power up the TCP/IP capability in your PocketPC application

0.00/5 (No votes)
19 Dec 2002 1  
Establish a TCP/IP connection to other applications.

Introduction

Everyone should have an idea on accessing Winsock in eMbedded Visual Basic 3.0. Its 100% easy and straight forward as what I did in Visual Basic. But how about eMbedded Visual C++ 3.0?

The Winsock for MS Windows CE does not support WSAAsyncSelect. Hence, we need to write a chunk of code as well as the synchronization thread to handle the incoming and outgoing data ourselves.

Using the code

The posted code is the entire sample project. So you might need to cut & paste the following function in order to utilize the piece of code in your own project. Don't worry, the code is easy to identify and all you need is to filter out all my demo project GUI interface control code.

Below are the three basic functions and the two synchronization threads you need to copy:

// Function
int  InitSocket (HWND); // Initialize the Winsock.DLL
void  OpenSocket (HWND); // Open the server side socket
void  CloseSocket(HWND); // Close the server side socket

// Synchronization Thread

// Waiting for the incoming connection request
DWORD WINAPI StartListen (LPVOID pParam); 
// Reading the incoming buffer
DWORD WINAPI ReadInBuffer (LPVOID pParam); 

Once you have copied the code, you need to call the above mentioned three functions in sequence. As each of them is to perform a specific task. Em... Lets look at the first function InitSocket as below. Basically, all it does is just initialize the Winsock component (version 1.1) by calling the WSAStartup API before we can proceed to create/use any socket.

WSADATA wsaData;

// Initliatize the Winsock dll version 1.1
if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
{
    // Fail to initialize the winsock dll
    MessageBox(hWnd, TEXT("Fail to initialize the Winsock DLL!"), 
                       TEXT("MySocket"), MB_OK | MB_ICONEXCLAMATION);
    // Set the return value
    return 1;
}
else
    // Successful initialize the Winsock DLL
    // Set the return value
    return 0;

Second, we need to create a new socket to listen to an incoming request (connect to a host) on a specific port number and accept the connection request (establish the connection). But you will notice there is some extra code under the OpenSocket which is used to handle the GUI control for determining whether the socket going to be created is in Server mode or Client mode. This is because both will have two different logic flow and characteristics.

Hence, if the socket going to be created is in server mode, all you need is proceed to create the synchronization thread (StartListen). Else, you will need to execute the following code to establish a connection to the specific host followed by creating the ReadInBuffer synchronization thread.

NOTE: All the non-relevant GUI code is filtered out in the following display code (but not in the attached demo project code). You may need to add your own GUI control/updated code into this function.

// Reset the SOCKADDR_IN struct
memset(&sckAddress, 0, sizeof(sockaddr_in));
// Setup the sckClient socket
sckClient = socket(AF_INET, SOCK_STREAM, 0);
sckAddress.sin_port = htons(atoi(port));
sckAddress.sin_family = AF_INET;
sckAddress.sin_addr.s_addr = inet_addr(host);
if(connect(sckClient, (struct sockaddr *)&sckAddress, 
                     sizeof(sckAddress)) != SOCKET_ERROR)
// Create read input buffer thread
    hThread2 = CreateThread(NULL, 0, ReadInBuffer, hWnd, 0, &dwThreadID);
else
    // Notify user about the error
    MessageBox(hWnd, TEXT("Client~Fail to establish the connection!"), 
                          TEXT("MySocket"), MB_OK | MB_ICONEXCLAMATION);

What is inside the StartListen synchronization thread? When you look into the code, you'll find that it is an infinite do...while loop in this thread and perform a checking on the socket state on every loop until it gets a valid socket handle as show in the code below:

if(sckServer != INVALID_SOCKET)
{
    // Setup the port number, protocol & etc..
    sckAddress.sin_port = htons(atoi(port));
    sckAddress.sin_family= AF_INET;
    sckAddress.sin_addr.s_addr = INADDR_ANY;
    // Bind the socket
    bind(sckServer, (struct sockaddr *)&sckAddress, sizeof(sckAddress));

    // Listen to the specific port for 1 client connection only
    listen(sckServer, 1);

    // Start looping and check the respective port for incoming request
    do
    {
        // Socket callback status structure
        fds;
        // Maximum wait time for the "select" command
        tv.tv_sec = 1;
        tv.tv_usec = 1; 
        // Initialize the fd_set structure to NULL
        FD_ZERO (&fds);
        // Add the sckServer socket to fd_set structure
        FD_SET (sckServer, &fds);
        // Call the select command
        if (select(0, &fds, NULL, NULL, &tv) == 0)
            // Maximum wait time is expired.
            continue;
        // Check is there any incoming request/active in the fd_set structure
        // Zero mean no, else
        if (FD_ISSET(sckServer, &fds) != 0)
        {
            // Accept the incoming request
            sckClient = accept(sckServer, NULL, 0);
            // Close the existing listen socket (sckServer)
            closesocket(sckServer);
            // Reset the sckSocket variable to NULL
            sckServer = INVALID_SOCKET;
            // Update status control
            SetDlgItemText(hWnd, IDC_STATUS1, TEXT("Connected"));
            // Create read input buffer thread
            hThread2 = CreateThread(NULL, 0, ReadInBuffer, 
                                        hWnd, 0, &dwThreadID);
            // Self terminate this thread
            break;
        }
    }while (sckServer != INVALID_SOCKET);
}
else
    // Notify user about the error
    MessageBox(hWnd, TEXT("Server~Fail to open the socket!"), 
                  TEXT("MySocket"), MB_OK | MB_ICONEXCLAMATION);

At this stage, you still can not receive any data from the host (client) connection. So, the ReadInBuffer synchronization thread is heard of the entire demo application which enables you to receive any data from host (client) connection.

Hence, lets take a look on the ReadInBuffer coding and understand more about its operation.

HWND hWnd = NULL;
char *Buff=NULL;
TCHAR *wBuff=NULL;

int err;

// map the pass in variable
hWnd = (HWND)pParam;
// Create new string buffer
wBuff = new TCHAR[1024];
Buff = new char[1024];

if (sckClient != INVALID_SOCKET )
{
    // Loop until no data in the input buffer
    while (true)
    {
        // Reset the allocated string buffer
        memset(wBuff, TEXT('\0'), 1024*sizeof(TCHAR));
        memset(Buff, '\0', 1024);
        // Read the data from valid socket
        err = recv(sckClient, Buff, 1024, 0);
        if (err == SOCKET_ERROR || err <= 0)
        {
            // Remote terminal reset the socket connection
            CloseSocket(hWnd);
            // Self terminate the thread
            break;
        }
        else
        {
            // Convert from MultiByte to UNICODE
            mbstowcs(wBuff, Buff, 1024);
            // Display the received data
            SetDlgItemText(hWnd, IDC_EDIT3, wBuff);
        }
    }
}

// Release the used memory
delete Buff;
// Self terminate the thread
ExitThread(WM_QUIT);
// Set the return value
return 0;

From the above code, there is an infinite while loop which will constantly check the socket status and it will break the looping when there is an invalid socket detected.

Last, we must destroy the socket when not in use or upon detecting the host (client) connection is lost. Therefore, the CloseSocket function is straight forward as shown below.

// Validate
if(sckServer != INVALID_SOCKET)
{
    // Close the current server side socket
    if (closesocket(sckServer) == 0)
    // Reset the sckServer
        sckServer = INVALID_SOCKET;
    else
        // Notify user about the error
        MessageBox(hWnd, TEXT("Server~Fail to close the socket."), 
                        TEXT("MySocket"), MB_OK | MB_ICONEXCLAMATION);
}

P.S If you need to get the local machine IP address, all you need is call the GetLocalIP function as included in the demo application.

Will do.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here