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
| Name | What it is for |
|---|---|
NewSCU, SCUConfig | A client: AE titles, address, timeouts, TLS, what it accepts for C‑GET. |
SCU.Associate, SCU.Release | Open and close an association; nil contexts proposes the defaults. |
SCU.Echo, SCU.Store, SCU.StoreStream | Verification and storage, from a data set or a stream. |
SCU.Find, CFindResult | A query; matches arrive on a channel. |
SCU.MoveWithSOPClass, SCU.GetWithSOPClass, RetrieveResult | Retrieval, with the counts and failed instances. |
StartServer, Server, SCPConfig | A server in the background; Stop or Shutdown it. |
NewSCP, SCP | The same, blocking in ListenAndServe. |
Handler, BaseHandler | One method per DIMSE service; the base answers C‑ECHO and refuses the rest. |
StorageHandler, EchoHandler | C‑STORE and C‑ECHO from a function. |
QueryRetrieveHandler, WorklistHandler | C‑FIND, C‑MOVE, C‑GET, and Modality Worklist. |
CompositeHandler | Route each service to its own handler. |
MPPSHandler, UPSHandler, StorageCommitmentHandler | The workflow services. |
TLSConfig | Certificates and peer verification for either side. |
StatusSuccess, IsSuccess, FormatStatus | DIMSE statuses. |
StatusError, AssociationError, TimeoutError | What 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
SCUis 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
Findreturns, 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