Products | Support | Send comments on this topic. | Email a link to this topic. | Back to Getting Started | Help Version 18.0.10.24
LEADTOOLS Multimedia API Help

How to Network (Client and Server) Using a User-Defined Protocol

Show in webframe

The following example demonstrates how to network (client and server) using a user-defined protocol. For an example of how to create a user-defined protocol, refer to How to Create a User-Defined Protocol.

The Server Side:

C Source

   // To use the network protocol manager interface, use the following code

   // Note that the protocol manager interface will use the protocol

   // interface

   ILMNetProtocolManager* pProtocol;

   ILMNetConnectionPoint *pConnection = NULL;

   ILMNetConnection* pServerCon = NULL;

   BSTR strPeer;

   BYTE *pBuff = NULL;

   DWORD dSize;

   

   HRESULT hRes = CoCreateInstance(&CLSID_LMNetProtocolManager, NULL, CLSCTX_ALL, &IID_ILMNetProtocolManager, (void**) &pProtocol);

   if (FAILED(hRes))

      return;

   

   // Create the server connection point   

   hRes = ILMNetProtocol_CreateConnectionPoint(pProtocol, L"myschema://10.0.4.2:27015", &pConnection);

   if (FAILED(hRes))

   {

      ILMNetProtocol_Release(pProtocol);

      return;

   }

   

   // Wait for the new incoming connection.

   do

   {

      ILMNetConnectionPoint_GetConnection(pConnection, &pServerCon, 1000);

      Sleep(100);

   } while (pServerCon == NULL);

   

   // To check whether there is a connection, use the following code

   if (ILMNetConnection_IsConnected(pServerCon) != S_OK)

   {

      ILMNetConnection_Release(pServerCon);

      ILMNetProtocol_Release(pProtocol);

      return;

   }

   

   // Get the peer name (Client Name)

   ILMNetConnection_GetPeerName(pServerCon, &strPeer);

   

   // Free the string when no longer needed

   CoTaskMemFree(strPeer);

   

   // The server is now ready to send the data

   pBuff = (BYTE*)malloc(1024);

   dSize = 1024;

   ILMNetConnection_Send(pServerCon, pBuff, dSize, &dSize, 1000);

   

   // If there is any blocking call, call the CancelBlockingCall method

   ILMNetConnection_CancelBlockingCall(pServerCon);

   

   // To disconnect, call the Disconnect method

   ILMNetConnection_Disconnect(pServerCon);

   

   // Close the connection point

   ILMNetConnectionPoint_Close(pConnection);

   

   // Free the pointers

   free(pBuff);

   ILMNetConnection_Release(pServerCon);

   ILMNetProtocol_Release(pProtocol);

C++ Source

   // To use the network protocol manager interface, use the following code

   // Note that the protocol manager interface will use the protocol

   // interface

   ILMNetProtocolManager* pProtocol;

   HRESULT hRes = CoCreateInstance(CLSID_LMNetProtocolManager, NULL, CLSCTX_ALL, IID_ILMNetProtocolManager, (void**) &pProtocol);

   if (FAILED(hRes))

      return;

   

   // Create the server connection point

   ILMNetConnectionPoint *pConnection = NULL;   

   hRes = pProtocol->CreateConnectionPoint(L"myschema://10.0.4.2:27015", &pConnection);

   if (FAILED(hRes))

   {

      pProtocol->Release();

      return;

   }

   

   // Wait for the new incoming connection.

   ILMNetConnection* pServerCon = NULL;

   do

   {

      pConnection->GetConnection(&pServerCon, 1000);

      Sleep(100);

   } while (pServerCon == NULL);

   

   // To check whether there is a connection, use the following code

   if (pServerCon->IsConnected() != S_OK)

   {

      pServerCon->Release();

      pProtocol->Release();

      return;

   }

   

   // Get the  peer name (Client Name)

   BSTR strPeer;

   pServerCon->GetPeerName(&strPeer);

   

   // Free the string when no longer needed

   CoTaskMemFree(strPeer);

   

   // The server is now ready to send the data

   BYTE *pBuff = new BYTE[1024];

   DWORD dSize = 1024;

   pServerCon->Send(pBuff, dSize, &dSize, 1000);

   

   // If there is any blocking call, call the CancelBlockingCall method

   pServerCon->CancelBlockingCall();

   

   // To disconnect, call the Disconnect method

   pServerCon->Disconnect();

   

   // Close the connection point

   pConnection->Close();

   

   // Free the pointers

   delete [] pBuff;

   pServerCon->Release();

   pProtocol->Release();

The Client Side:

C Source

   ILMNetProtocolManager* pProtocol;

   ILMNetConnection* pClientCon;

   BSTR strPeer;

   BYTE *pBuff;

   DWORD dSize;

   HRESULT hRes;

   

   // To use the network protocol manager interface, use the following code

   // Note that the protocol manager interface will use the protocol

   // interface

   hRes = CoCreateInstance(&CLSID_LMNetProtocolManager, NULL, CLSCTX_ALL, &IID_ILMNetProtocolManager, (void**) &pProtocol);

   if (FAILED(hRes))

      return;

   

   // Connect to the server

   hRes = ILMNetProtocol_Connect(pProtocol, L"myschema://10.0.4.2:27015", &pClientCon, 1000);

   if (FAILED(hRes))

   {

      ILMNetProtocol_Release(pProtocol);

      return;

   }

   

   // To check whether there is a connection, use the following code

   if (ILMNetConnection_IsConnected(pClientCon) != S_OK)

   {

      ILMNetConnection_Release(pClientCon);

      ILMNetProtocol_Release(pProtocol);

      return;

   }

   

   // Get the  peer name (Server Name)

   ILMNetConnection_GetPeerName(pClientCon, &strPeer);

   

   // Free the string when no longer needed

   CoTaskMemFree(strPeer);

   

   // The client is now ready to receive the data

   pBuff = (BYTE*)malloc(1024);

   dSize = 1024;

   ILMNetConnection_Recv(pClientCon, pBuff, dSize, &dSize, 1000);

   

   // If there is any blocking call, call the CancelBlockingCall method

   ILMNetConnection_CancelBlockingCall(pClientCon);

   

   // To disconnect, call the Disconnect method

   ILMNetConnection_Disconnect(pClientCon);

   

   // Free the pointers

   free(pBuff);

   ILMNetConnection_Release(pClientCon);

   ILMNetProtocol_Release(pProtocol);

C++ Source

   // To use the network protocol manager interface, use the following code

   // Note that the protocol manager interface will use the protocol

   // interface

   ILMNetProtocolManager* pProtocol;

   HRESULT hRes = CoCreateInstance(CLSID_LMNetProtocolManager, NULL, CLSCTX_ALL, IID_ILMNetProtocolManager, (void**) &pProtocol);

   if (FAILED(hRes))

      return;

   

   // Connect to the server

   ILMNetConnection* pClientCon;

   hRes = pProtocol->Connect(L"myschema://10.0.4.2:27015", &pClientCon, 1000);

   if (FAILED(hRes))

   {

      pProtocol->Release();

      return;

   }

   

   // To check whether there is a connection, use the following code

   if (pClientCon->IsConnected() != S_OK)

   {

      pClientCon->Release();

      pProtocol->Release();

      return;

   }

   

   // Get the  peer name (Server Name)

   BSTR strPeer;

   pClientCon->GetPeerName(&strPeer);

   

   // Free the string when no longer needed

   CoTaskMemFree(strPeer);

   

   // The client is now ready to receive the data

   BYTE *pBuff = new BYTE[1024];

   DWORD dSize = 1024;

   pClientCon->Recv(pBuff, dSize, &dSize, 1000);

   

   // If there is any blocking call, call the CancelBlockingCall method

   pClientCon->CancelBlockingCall();

   

   // To disconnect, call the Disconnect method

   pClientCon->Disconnect();

   

   // Free the pointers

   delete [] pBuff;

   pClientCon->Release();

   pProtocol->Release();

Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.