socket

package
v1.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2026 License: Apache-2.0 Imports: 1 Imported by: 2

README

Socket

Network socket implementations for TCP and UDP protocols.

Overview

The socket package provides simplified network programming with protocol-specific subpackages:

Choosing a Protocol

Feature TCP (socket/tcp) UDP (socket/udp)
Connection Connection-oriented Connectionless
Reliability Guaranteed delivery Best effort
Ordering In-order delivery No ordering guarantee
Server ✅ Implemented ✅ Implemented
Client ✅ Implemented ✅ Implemented
Use Cases Web servers, databases, file transfer DNS, gaming, metrics, streaming

Installation

go get -u github.com/common-library/go/socket/tcp
go get -u github.com/common-library/go/socket/udp

Quick Start

TCP Server
import "github.com/common-library/go/socket/tcp"

server := &tcp.Server{}
err := server.Start("tcp", ":8080", 100,
    func(client tcp.Client) {
        data, _ := client.Read(1024)
        client.Write("Echo: " + data)
    },
    func(err error) {
        log.Printf("Accept error: %v", err)
    },
)
TCP Client
import "github.com/common-library/go/socket/tcp"

client := &tcp.Client{}
client.Connect("tcp", "localhost:8080")
client.Write("Hello")
data, _ := client.Read(1024)
client.Close()
UDP Client
import (
    "time"
    "github.com/common-library/go/socket/udp"
)

client := &udp.Client{}
client.Connect("udp4", "localhost:8080")
client.Send([]byte("Hello"))
data, addr, _ := client.Receive(1024, 5*time.Second)
client.Close()
UDP Server
import (
    "net"
    "github.com/common-library/go/socket/udp"
)

server := &udp.Server{}
err := server.Start("udp4", ":8080", 1024,
    func(data []byte, addr net.Addr, conn net.PacketConn) {
        conn.WriteTo(data, addr)  // Echo back
    },
    true,  // Async handler
    func(err error) {
        log.Printf("Error: %v", err)
    },
)
defer server.Stop()

Features

TCP (socket/tcp)
  • Connection-oriented reliable communication
  • Concurrent client handling with goroutine pools
  • Automatic resource management
  • Graceful shutdown support
  • Local and remote address access
UDP (socket/udp)
  • Connectionless packet-based communication
  • Packet send/receive with timeout support
  • Async/sync packet handler options
  • Optional error handling callbacks
  • Read/write buffer configuration
  • Graceful shutdown with handler completion wait

Migration from v1.x

Previous versions used a single socket package for TCP. For backward compatibility, type aliases are provided:

// Old code (still works with deprecation warnings)
import "github.com/common-library/go/socket"

server := &socket.Server{}  // Deprecated: use tcp.Server
client := &socket.Client{}  // Deprecated: use tcp.Client

Recommended migration:

// New code
import "github.com/common-library/go/socket/tcp"

server := &tcp.Server{}
client := &tcp.Client{}

Testing

Run all tests:

go test -v ./...

Run protocol-specific tests:

go test -v ./tcp
go test -v ./udp

License

This package is part of the common-library project and follows the same license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client deprecated

type Client = tcp.Client

Deprecated: Use github.com/common-library/go/socket/tcp.Client instead. This type alias is provided for backward compatibility and will be removed in v2.0.0. To migrate, change:

import "github.com/common-library/go/socket"
client := &socket.Client{}

To:

import "github.com/common-library/go/socket/tcp"
client := &tcp.Client{}

type Server deprecated

type Server = tcp.Server

Deprecated: Use github.com/common-library/go/socket/tcp.Server instead. This type alias is provided for backward compatibility and will be removed in v2.0.0. To migrate, change:

import "github.com/common-library/go/socket"
server := &socket.Server{}

To:

import "github.com/common-library/go/socket/tcp"
server := &tcp.Server{}

Directories

Path Synopsis
Package tcp provides TCP socket client and server implementations.
Package tcp provides TCP socket client and server implementations.
Package udp provides UDP socket client and server implementations.
Package udp provides UDP socket client and server implementations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL