Skip to Content
go-dicom(3)
Packages

network

The DICOM upper layer protocol and DIMSE services: an SCU that associates and sends C-ECHO, C-STORE, C-FIND, C-MOVE, C-GET and the N- services, and an SCP that accepts associations and passes each request to a handler.

import "github.com/amrshadid/go-dicom/network" · pkg.go.dev

When to use it

  • Send images to a PACS, query it, or retrieve from it (SCU).
  • Receive images, answer queries, or run a worklist, MPPS, storage commitment or UPS service (SCP).

The command line's echoscu, storescu, findscu, movescu, getscu, storescp and qrscp are built on this package. For an archive behind the SCP, see dcmstore.

How it fits together

Handler has one method per DIMSE service. Embed BaseHandler and override what you serve, use a ready-made handler, or route services to several with CompositeHandler.

Key types and functions

NameWhat it is for
NewSCU, SCUConfigA client: AE titles, address, timeouts, TLS, what it accepts for C‑GET.
SCU.Associate, SCU.ReleaseOpen and close an association; nil contexts proposes the defaults.
SCU.Echo, SCU.Store, SCU.StoreStreamVerification and storage, from a data set or a stream.
SCU.Find, CFindResultA query; matches arrive on a channel.
SCU.MoveWithSOPClass, SCU.GetWithSOPClass, RetrieveResultRetrieval, with the counts and failed instances.
StartServer, Server, SCPConfigA server in the background; Stop or Shutdown it.
NewSCP, SCPThe same, blocking in ListenAndServe.
Handler, BaseHandlerOne method per DIMSE service; the base answers C‑ECHO and refuses the rest.
StorageHandler, EchoHandlerC‑STORE and C‑ECHO from a function.
QueryRetrieveHandler, WorklistHandlerC‑FIND, C‑MOVE, C‑GET, and Modality Worklist.
CompositeHandlerRoute each service to its own handler.
MPPSHandler, UPSHandler, StorageCommitmentHandlerThe workflow services.
TLSConfigCertificates and peer verification for either side.
StatusSuccess, IsSuccess, FormatStatusDIMSE statuses.
StatusError, AssociationError, TimeoutErrorWhat went wrong, for errors.As.

Example

// Start a storage SCP, then associate to it, verify it, send it a file and
// release. Both ends run in one program; normally the peer is a PACS.
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/amrshadid/go-dicom/dataset"
	"github.com/amrshadid/go-dicom/filebase"
	"github.com/amrshadid/go-dicom/filereader"
	"github.com/amrshadid/go-dicom/network"
)

func main() {
	ctx := context.Background()

	server, err := network.StartServer(ctx,
		network.SCPConfig{AETitle: "ARCHIVE", BindAddress: "127.0.0.1", Port: 43112, MaxAssociations: 8},
		&network.StorageHandler{
			OnStore: func(ctx context.Context, sopClass, sopInstance string, ds *dataset.Dataset) uint16 {
				fmt.Println("SCP kept", sopInstance) // keep it somewhere first
				return network.StatusSuccess
			},
		})
	if err != nil {
		log.Fatal(err)
	}
	defer server.Stop()

	scu := network.NewSCU(network.SCUConfig{
		CallingAE: "MY_APP",
		CalledAE:  "ARCHIVE",
		Address:   server.Addr(),
	})
	if err := scu.Associate(ctx, nil); err != nil { // nil: the default presentation contexts
		log.Fatal(err)
	}
	defer scu.Release(ctx)

	if err := scu.Echo(ctx); err != nil {
		log.Fatal(err)
	}
	fmt.Println("C-ECHO ok")

	f, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	df, err := filereader.ReadDICOMFile(filebase.NewFileReader(f))
	if err != nil {
		log.Fatal(err)
	}
	if err := scu.Store(ctx, df.GetDataset()); err != nil {
		log.Fatal(err)
	}
	fmt.Println("C-STORE ok")
}

It starts a storage SCP on 127.0.0.1:43112, echoes, stores CT_small.dcm, and prints C-ECHO ok, the SCP's line and C-STORE ok.

Notes

  • An SCU is safe for concurrent use: operations on one association run one at a time.
  • Handlers run on the association's goroutine and may be called concurrently for different associations. Hand long work to a queue.
  • Drain the channel Find returns, or cancel the context.
  • Which syntaxes are proposed and accepted, and how extended negotiation is answered, is in CONFORMANCE.md §3.1 and §3.3; limits are in §8.2 and §7; the services in §8.5.

Warning

Handlers must be safe for concurrent use. Two associations can store or query at the same moment.

Caution

DICOM over TCP is unauthenticated and unencrypted. A called AE title is a name, not a password. On a reachable network use TLS with client certificates or SCPConfig.Admit, and set SCPConfig.MaxAssociations.

See also

dcmstore · matching · dicomweb · Network concepts · Network SCU · Network SCP · Query and retrieve · Workflow services · TLS and security · Coming from pydicom

On this page