L_DicomListen

#include "ltdic.h"

L_INT EXT_FUNCTION L_DicomListen(hNet, pszHostAddress, nHostPort, nNbPeers)

HDICOMNET hNet;

/* a DICOM Network handle */

L_CHAR * pszHostAddress;

/* host IP address */

L_UINT nHostPort;

/* host port number */

L_INT nNbPeers;

/* maximum number of peers */

Establishes a connection to listen for incoming connection requests. This function is available in the Medical Suite Toolkit.

Parameter

Description

hNet

A DICOM Network handle. This is the handle returned from the L_DicomCreateNet function.

pszHostAddress

Character string that contains the IP address of the host computer (the server's address).

nHostPort

Port number of the host (the server's port).

nNbPeers

The maximum number of (clients) that may be connected to the server.

Returns

0

SUCCESS

>0

An error occurred. Refer to Return Codes.

Comments

If pszHostAddress is "" or NULL, the IP address will be the local computer's address.

If nHostPort is 0, the port number will be the number of the first available port.

To connect to a server as a client, you must first create and initialize a handle to the DICOM Network using L_DicomCreateNet. Next, set the callback functions you will use by calling L_DicomSetCallback. Then call L_DicomConnect to establish the connection.

To use your computer as a server, you must first create and initialize a handle to the DICOM Network using L_DicomCreateNet. Next, set the callback functions you will use by calling L_DicomSetCallback. Then call L_DicomListen to listen for incoming connection requests.

Required DLLs and Libraries

LTDIC

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_DicomCreateNet, L_DicomStartUp, L_DicomListen

Topics:

Working with DICOM Network Connections

Example

L_VOID Test2(L_VOID)
{
   L_INT nRet;
   DICOMNETCALLBACK cb;
   HDICOMNET hServer;

   /* start the network */
   nRet = L_DicomStartUp();

   hServer = L_DicomCreateNet("d:\\temp", DICOM_SECURE_NONE);

   /* set the callbacks */
   memset(&cb, 0, sizeof(DICOMNETCALLBACK));
   cb.pfnConnect = (CONNECTCALLBACK)OnConnect;
   cb.pfnAccept = (ACCEPTCALLBACK)OnAccept;
   cb.pfnClose = (CLOSECALLBACK)OnClose;
   cb.pfnReceive = (RECEIVECALLBACK)OnReceive;
   cb.pfnSend = (SENDCALLBACK)OnSend;
   cb.pfnReceiveAssociateRequest = (RECEIVEASSOCIATEREQUESTCALLBACK)OnReceiveAssociateRequest;
   cb.pfnReceiveReleaseRequest = (RECEIVERELEASEREQUESTCALLBACK)OnReceiveReleaseRequest;
   cb.pfnReceiveReleaseResponse = (RECEIVERELEASERESPONSECALLBACK)OnReceiveReleaseResponse;
   cb.pfnReceiveAbort = (RECEIVEABORTCALLBACK)OnReceiveAbort;
   cb.pfnReceiveData = (RECEIVEDATACALLBACK)OnReceiveData;
   cb.pfnReceiveAssociateAccept = (RECEIVEASSOCIATEACCEPTCALLBACK)OnReceiveAssociateAccept;
   cb.pfnReceiveAssociateReject = (RECEIVEASSOCIATEREJECTCALLBACK)OnReceiveAssociateReject;
   cb.pfnReceiveCStoreRequest = (RECEIVECSTOREREQUESTCALLBACK)OnReceiveCStoreRequest;
   cb.pfnReceiveCStoreResponse = (RECEIVECSTORERESPONSECALLBACK)OnReceiveCStoreResponse;
   cb.pfnReceiveCFindRequest = (RECEIVECFINDREQUESTCALLBACK)OnReceiveCFindRequest;
   cb.pfnReceiveCFindResponse = (RECEIVECFINDRESPONSECALLBACK)OnReceiveCFindResponse;
   cb.pfnReceiveCGetRequest = (RECEIVECGETREQUESTCALLBACK)OnReceiveCGetRequest;
   cb.pfnReceiveCGetResponse = (RECEIVECGETRESPONSECALLBACK)OnReceiveCGetResponse;
   cb.pfnReceiveCMoveRequest = (RECEIVECMOVEREQUESTCALLBACK)OnReceiveCMoveRequest;
   cb.pfnReceiveCMoveResponse = (RECEIVECMOVERESPONSECALLBACK)OnReceiveCMoveResponse;
   cb.pfnReceiveCCancelRequest = (RECEIVECCANCELREQUESTCALLBACK)OnReceiveCCancelRequest;
   cb.pfnReceiveCEchoRequest = (RECEIVECECHOREQUESTCALLBACK)OnReceiveCEchoRequest;
   cb.pfnReceiveCEchoResponse = (RECEIVECECHORESPONSECALLBACK)OnReceiveCEchoResponse;
   cb.pfnReceiveNReportRequest = (RECEIVENREPORTREQUESTCALLBACK)OnReceiveNReportRequest;
   cb.pfnReceiveNReportResponse = (RECEIVENREPORTRESPONSECALLBACK)OnReceiveNReportResponse;
   cb.pfnReceiveNGetRequest = (RECEIVENGETREQUESTCALLBACK)OnReceiveNGetRequest;
   cb.pfnReceiveNGetResponse = (RECEIVENGETRESPONSECALLBACK)OnReceiveNGetResponse;
   cb.pfnReceiveNSetRequest = (RECEIVENSETREQUESTCALLBACK)OnReceiveNSetRequest;
   cb.pfnReceiveNSetResponse = (RECEIVENSETRESPONSECALLBACK)OnReceiveNSetResponse;
   cb.pfnReceiveNActionRequest = (RECEIVENACTIONREQUESTCALLBACK)OnReceiveNActionRequest;
   cb.pfnReceiveNActionResponse = (RECEIVENACTIONRESPONSECALLBACK)OnReceiveNActionResponse;
   cb.pfnReceiveNCreateRequest = (RECEIVENCREATEREQUESTCALLBACK)OnReceiveNCreateRequest;
   cb.pfnReceiveNCreateResponse = (RECEIVENCREATERESPONSECALLBACK)OnReceiveNCreateResponse;
   cb.pfnReceiveNDeleteRequest = (RECEIVENDELETEREQUESTCALLBACK)OnReceiveNDeleteRequest;
   cb.pfnReceiveNDeleteResponse = (RECEIVENDELETERESPONSECALLBACK)OnReceiveNDeleteResponse;
   cb.pfnReceiveUnknown = (RECEIVEUNKNOWNCALLBACK)OnReceiveUnknown;
   cb.pUserData = NULL;
   L_DicomSetCallback(hServer, &cb);

   /* create a server to accept up to 25 clients */
   L_DicomListen(hServer, NULL, PEER_PORT, 25);
}

L_VOID L_EXPORT EXT_CALLBACK OnAccept(HDICOMNET hNet, L_INT nError, L_VOID
*pUserData)
{
   L_CHAR szMsg[800];
   L_CHAR szTemp[800];
   HDICOMNET hClient;
   L_INT32 lClients;
   L_CHAR szURL[200];
   L_UINT uPort;
   DICOMNETCALLBACK cb;

   wsprintf(szMsg, "***NetAccept Event ***\n nStatus[%d]\n", nError);

   if (nError != DICOM_SUCCESS)
   {
      MessageBox(NULL, szMsg, "Accept", MB_OK);
      return;
   }

   /* accept the connection attempt */
   hClient = L_DicomCreateNet(NULL, DICOM_SECURE_NONE);
   L_DicomAccept(hNet, hClient);

   /* set the callbacks */
   memset(&cb, 0, sizeof(DICOMNETCALLBACK));
   cb.pfnConnect = (CONNECTCALLBACK)OnConnect;
   cb.pfnClose = (CLOSECALLBACK)OnClose;
   cb.pfnReceive = (RECEIVECALLBACK)OnReceive;
   cb.pfnSend = (SENDCALLBACK)OnSend;
   cb.pfnReceiveAssociateRequest = (RECEIVEASSOCIATEREQUESTCALLBACK)
OnReceiveAssociateRequest;
   cb.pfnReceiveReleaseRequest = (RECEIVERELEASEREQUESTCALLBACK)
OnReceiveReleaseRequest  ;
   cb.pfnReceiveReleaseResponse  = (RECEIVERELEASERESPONSECALLBACK)
OnReceiveReleaseResponse ;
   cb.pfnReceiveAbort = (RECEIVEABORTCALLBACK)OnReceiveAbort;
   cb.pfnReceiveData = (RECEIVEDATACALLBACK)OnReceiveData;
   cb.pfnReceiveAssociateAccept  = (RECEIVEASSOCIATEACCEPTCALLBACK)
OnReceiveAssociateAccept ;
   cb.pfnReceiveAssociateReject = (RECEIVEASSOCIATEREJECTCALLBACK)
OnReceiveAssociateReject ;
   cb.pfnReceiveCStoreRequest = (RECEIVECSTOREREQUESTCALLBACK)
OnReceiveCStoreRequest;
   cb.pfnReceiveCStoreResponse = (RECEIVECSTORERESPONSECALLBACK)
OnReceiveCStoreResponse;
   cb.pfnReceiveCFindRequest = (RECEIVECFINDREQUESTCALLBACK)
OnReceiveCFindRequest;
   cb.pfnReceiveCFindResponse = (RECEIVECFINDRESPONSECALLBACK)
OnReceiveCFindResponse;
   cb.pfnReceiveCGetRequest = (RECEIVECGETREQUESTCALLBACK)
OnReceiveCGetRequest;
   cb.pfnReceiveCGetResponse = (RECEIVECGETRESPONSECALLBACK)
OnReceiveCGetResponse;
   cb.pfnReceiveCMoveRequest = (RECEIVECMOVEREQUESTCALLBACK)
OnReceiveCMoveRequest;
   cb.pfnReceiveCMoveResponse = (RECEIVECMOVERESPONSECALLBACK)
OnReceiveCMoveResponse;
   cb.pfnReceiveCCancelRequest = (RECEIVECCANCELREQUESTCALLBACK)
OnReceiveCCancelRequest;
   cb.pfnReceiveCEchoRequest = (RECEIVECECHOREQUESTCALLBACK)
OnReceiveCEchoRequest;
   cb.pfnReceiveCEchoResponse = (RECEIVECECHORESPONSECALLBACK)
OnReceiveCEchoResponse;
   cb.pfnReceiveNReportRequest = (RECEIVENREPORTREQUESTCALLBACK)
OnReceiveNReportRequest;
   cb.pfnReceiveNReportResponse = (RECEIVENREPORTRESPONSECALLBACK)
OnReceiveNReportResponse ;
   cb.pfnReceiveNGetRequest = (RECEIVENGETREQUESTCALLBACK)
OnReceiveNGetRequest;
   cb.pfnReceiveNGetResponse = (RECEIVENGETRESPONSECALLBACK)
OnReceiveNGetResponse;
   cb.pfnReceiveNSetRequest = (RECEIVENSETREQUESTCALLBACK)
OnReceiveNSetRequest;
   cb.pfnReceiveNSetResponse = (RECEIVENSETRESPONSECALLBACK)
OnReceiveNSetResponse;
   cb.pfnReceiveNActionRequest = (RECEIVENACTIONREQUESTCALLBACK)
OnReceiveNActionRequest;
   cb.pfnReceiveNActionResponse = (RECEIVENACTIONRESPONSECALLBACK)
OnReceiveNActionResponse;
   cb.pfnReceiveNCreateRequest = (RECEIVENCREATEREQUESTCALLBACK)
OnReceiveNCreateRequest;
   cb.pfnReceiveNCreateResponse = (RECEIVENCREATERESPONSECALLBACK)
OnReceiveNCreateResponse;
   cb.pfnReceiveNDeleteRequest = (RECEIVENDELETEREQUESTCALLBACK)
OnReceiveNDeleteRequest;
   cb.pfnReceiveNDeleteResponse = (RECEIVENDELETERESPONSECALLBACK)
OnReceiveNDeleteResponse;
   cb.pfnReceiveUnknown = (RECEIVEUNKNOWNCALLBACK)
OnReceiveUnknown;
   cb.pUserData = NULL;
   L_DicomSetCallback(hClient, &cb);

   lstrcat(szMsg, "Accepted\n");

   /* display some information about the connection */
   L_DicomGetHostInfo(hNet, szURL, &uPort);
   wsprintf(szTemp, "HostAddress[%s]\nHostPort[%d]\n", szURL, uPort);
   lstrcat(szMsg, szTemp);

   /* get the latest client */
   lClients = L_DicomGetClientCount(hNet);
   hClient  = L_DicomGetClient(hNet, lClients - 1);
   wsprintf(szTemp, "ClientCount[%d]\nLatest Client[%x]\n", lClients,
hClient);
   lstrcat(szMsg, szTemp);

   L_DicomGetPeerInfo(hNet, szURL, &uPort);
   wsprintf(szTemp, "PeerAddress[%s]\nPeerPort[%d]\n", szURL, uPort);
   lstrcat(szMsg, szTemp);

   MessageBox(NULL, szMsg, "Accept", MB_OK);

   /* reject the connection if we do not like the address */
   if(lstrcmp(szURL, "207.238.49.199") != 0)
   {
      /* close the connection */
      L_DicomClose(hClient);
   }
}


L_VOID L_EXPORT EXT_CALLBACK OnSend(HDICOMNET hNet, L_INT nError, L_UCHAR nType, L_UINT32 nBytes, L_VOID *pUserData)
{
   L_CHAR szText[80];
   wsprintf(szText, "Sent Data - %u\n", nBytes);
   OutputDebugString(szText);
}

L_VOID L_EXPORT EXT_CALLBACK OnReceive(HDICOMNET hNet, L_INT nError, L_UCHAR nType, L_CHAR *pBuffer, L_UINT32 nBytes, L_VOID *pUserData)
{
   L_CHAR szText[80];
   sprintf(szText, "Received Data - %u", nBytes);
   OutputDebugString(szText);
}

L_VOID L_EXPORT EXT_CALLBACK OnReceiveAssociateRequest(HDICOMNET hNet, HDICOMPDU hAssociate, L_VOID *pUserData)
{
   L_CHAR szMsg[800];
   L_UCHAR i;
   L_UCHAR nId;
   L_UCHAR nResult;
   L_CHAR L_FAR* pszAbstract=NULL;
   L_CHAR L_FAR* pszTransfer=NULL;
   HDICOMPDU hPDU=NULL;

   /* we have received an AssociateRequest */

   lstrcpy(szMsg, "*** NetReceiveAssociateRequest Event ***\n");
    
   /* check the version, if not 1, reject it */
   if(L_DicomGetVersion(hAssociate) != 1)
   {
      L_DicomSendAssociateReject(hNet, 
                                 PDU_REJECT_RESULT_PERMANENT, 
                                 PDU_REJECT_SOURCE_USER, 
                                 PDU_REJECT_REASON_UNKNOWN);
      lstrcat(szMsg, "\nSent Associate Regject!\n");
   }
   else
   {
      /* send associate accept class back */
      hPDU = L_DicomCreateAssociate(FALSE);
      L_DicomResetAssociate(hPDU, FALSE);

      /* copy all presentation objects from received hPDU   */
      /* also, reply that we only support the first Transfer*/
      /* Syntax from the received hPDU                      */
      for(i = 0; i<L_DicomGetPresentationCount(hAssociate); i++)
      {
         nId = L_DicomGetPresentation(hAssociate, i);
         pszTransfer = L_DicomGetTransfer(hAssociate, nId, 0);
         nResult = PDU_ACCEPT_RESULT_SUCCESS;
         pszAbstract = L_DicomGetAbstract(hAssociate, nId);
         L_DicomAddPresentation(hPDU, nId, nResult, pszAbstract);
         L_DicomAddTransfer(hPDU, nId, pszTransfer);
      }
      L_DicomSendAssociateAccept(hNet, hPDU);
      L_DicomFreeAssociate(hPDU);
      lstrcat(szMsg, "\nSent Associate Accept.\n");
   }
   MessageBox(NULL, szMsg, "OnReceiveAssociateRequest", MB_OK);
}

L_VOID L_EXPORT EXT_CALLBACK OnReceiveReleaseRequest(HDICOMNET hNet, L_VOID *pUserData)
{
   L_CHAR szMsg[200];
   lstrcpy(szMsg, "*** NetReceiveRelease Event ***\n");
   L_DicomSendReleaseResponse(hNet);
   lstrcat(szMsg, "Sent Release Response.\n");
   MessageBox(NULL, szMsg, "OnReceiveReleaseRequest", MB_OK);
}

L_VOID L_EXPORT EXT_CALLBACK OnReceiveAbort(HDICOMNET hNet, L_UCHAR nSource, L_UCHAR nReason, L_VOID *pUserData)
{
   L_CHAR szMsg[200];
   wsprintf(szMsg, "*** OnReceiveAbort ***\nSource[%d]\nReason[%d]", nSource, nReason);
   MessageBox(NULL, szMsg, "OnReceiveAbort", MB_OK);
}

L_VOID L_EXPORT EXT_CALLBACK OnReceiveData(HDICOMNET hNet, L_UCHAR nID, HDICOMDS hCS, HDICOMDS hDS, L_VOID *pUserData)
{
   /* This example saves the Data Set in a file */
   L_CHAR szMsg[200];
   wsprintf(szMsg, "*** OnReceiveData ***\nnID[%d]", nID);
   if(!hDS)
      lstrcat(szMsg, "\nData is a command set only!");
   else
   {
      lstrcat(szMsg, "Data includes a data set - saved to disk!");
      L_DicomSaveDS(hDS, "D:\\TEMP\\TEST1.DIC", 0);
   }
   MessageBox(NULL, szMsg, "OnReceiveData", MB_OK);
}