L_InetServerInit

#include "l_bitmap.h"
#include "ltnet.h"

L_LTNET_API L_INT L_InetServerInit(nPort, phComputer, pfnCallback, pUserData)

L_INT nPort;

/* port on which to listen */

L_COMP * phComputer;

/* address of variable to update */

INETCALLBACK pfnCallback;

/* pointer to a callback function */

L_VOID *pUserData;

/* user defined callback data */

Sets up a port to act as a server to which other computers can connect.

Parameter

Description

nPort

Port number on which to listen for incoming connections. Some commonly used values include:

 

Value

Meaning

 

0x015

FTP

 

0x050

Web server

 

0x077

News

 

0x019

Mail

 

0x017

Telnet

phComputer

Address of a variable to updated with the handle to the server computer

pfnCallback

Pointer to a callback function of type INETCALLBACK for receiving messages for phComputer. Use the function pointer to your callback function as the value of this parameter.

pUserData

Void pointer that you can use to pass one or more additional parameters that the callback function needs.

 

To use this feature, assign a value to a variable or create a structure that contains as many fields as you need. Then, in this parameter, pass the address of the variable or structure, casting it to L_VOID  *. The callback function, which receives the address in its own pUserData parameter, can cast it to a pointer of the appropriate data type to access your variable or structure.

 

If the additional parameters are not needed, you can pass NULL in this parameter.

Returns

SUCCESS

The function was successful.

< 1

An error occurred. Refer to Return Codes.

Comments

This function returns immediately, and the INETCALLBACK function will be called when other computers attempt to connect to phComputer.

You must initialize the LEADTOOLS Internet DLL using L_InetStartUp before calling this function.

Required DLLs and Libraries

LTNET

For a listing of the exact DLLs and Libraries needed, based on the toolkit version, refer to Files To Be Included With Your Application

See Also

Functions:

L_InetConnect, L_InetClose, L_InetAcceptConnect, INETCALLBACK, L_InetStartUp, L_InetShutDown

Topics:

Internet Functions

Example

static L_INT EXT_CALLBACK InetCallback(L_COMP hComp,
                                      L_INT  nMessage, 
                                      L_INT  nError, 
                                      L_CHAR *pBuffer,
                                      L_SIZE_T lSize,
                                      L_VOID *pUserData);

/* you do not need to use MakeProcInstance for 32 bit applications */
L_INT  InetServerInitExample(L_COMP hServer /* the handle to the server */)
{
   L_INT nRet;
   nRet = L_InetServerInit(1000, &hServer, InetCallback, NULL);
   if(nRet != SUCCESS)
   {
      MessageBox(NULL, TEXT("The server failed to start!"), TEXT("Error"), MB_OK);
      return nRet;
   }
   return SUCCESS; 
}


static L_INT EXT_CALLBACK InetCallback(L_COMP   hServer,
                                       L_INT    nMessage,
                                       L_INT    nError,
                                       L_CHAR * pBuffer,
                                       L_SIZE_T lSize,
                                       L_VOID * pUserData)
{
   UNREFERENCED_PARAMETER(pBuffer);
   UNREFERENCED_PARAMETER(lSize);
   UNREFERENCED_PARAMETER(pUserData);

   L_INT nRet;
   L_COMP hClient;

   switch(nMessage)
   {
      case INET_CONNECT_REQUEST:
         /* check the status and send some data */
         if(nError == SUCCESS)
         {
            /* somebody is trying to connect to our server accept the connection into 
               the server created above */
            nRet = L_InetAcceptConnect(hServer, &hClient, (INETCALLBACK) InetServerInitExample, NULL);
            if(nRet == SUCCESS)
               MessageBox(NULL, TEXT("Connection accepted successfully"), TEXT("Info"), MB_OK);

            /* remove the next line if you want to keep the connection open in your app 
               you should save hClient in a list of active connections */
            L_InetClose(hClient, TRUE);
         }
         else
            MessageBox(NULL, TEXT("You have failed to connect!"), TEXT("Error"), MB_OK);
      break;
   }
   return SUCCESS;
}