Sling Academy
Home/Golang/Using the `net` Package for Low-Level Network Programming in Go

Using the `net` Package for Low-Level Network Programming in Go

Last updated: November 26, 2024

In this article, we'll explore how to perform low-level network programming in Go using the net package. Go provides a comprehensive suite for handling network-related tasks, and the net package is central to building powerful network applications.

Introduction to the net Package

The net package in Go provides a portable interface for network I/O, helping developers work with network connectivity and communication protocols. Let's look at some common features of the net package:

  • Simple APIs for connection to network addresses.
  • Support for both TCP and UDP protocols.
  • Utility functions for name resolution.

Creating a Basic TCP Server

The following example demonstrates how to set up a simple TCP server that listens on a specified port and accepts incoming client connections.

package main

import (
    "fmt"
    "net"
)

func main() {
    listener, err := net.Listen("tcp", ":8080")
    if err != nil {
        fmt.Println("Error creating listener:", err)
        return
    }
    defer listener.Close()

    fmt.Println("Server is listening on port 8080")

    for {
        conn, err := listener.Accept()
        if err != nil {
            fmt.Println("Error accepting connection:", err)
            continue
        }
        go handleConnection(conn)
    }
}

func handleConnection(conn net.Conn) {
    defer conn.Close()
    fmt.Println("New connection accepted")

    buffer := make([]byte, 1024)
    _, err := conn.Read(buffer)
    if err != nil {
        fmt.Println("Error reading from connection:", err)
        return
    }
    fmt.Println("Received data:", string(buffer))
}

Creating a TCP Client

Now, let's create a basic TCP client that connects to our server and sends a message.

package main

import (
    "fmt"
    "net"
)

func main() {
    conn, err := net.Dial("tcp", "localhost:8080")
    if err != nil {
        fmt.Println("Error connecting to server:", err)
        return
    }
    defer conn.Close()

    message := "Hello, Server!"
    _, err = conn.Write([]byte(message))
    if err != nil {
        fmt.Println("Error writing to connection:", err)
        return
    }
    fmt.Println("Sent message to server:", message)
}

UDP Networking

The net package also supports UDP, which is connection-less. Below is an example of a simple UDP server:

package main

import (
    "fmt"
    "net"
)

func main() {
    addr := net.UDPAddr{
        Port: 8080,
        IP:   net.ParseIP("127.0.0.1"),
    }

    conn, err := net.ListenUDP("udp", &addr)
    if err != nil {
        fmt.Println(err)
        return
    }
    defer conn.Close()

    buffer := make([]byte, 1024)
    for {
        _, remoteAddr, err := conn.ReadFromUDP(buffer)
        if err != nil {
            fmt.Println("Error reading from UDP connection:", err)
            continue
        }
        fmt.Printf("Received %s from %s\n", string(buffer), remoteAddr)
    }
}

Conclusion

The net package makes it straightforward to build networked applications in Go. With its clean and efficient design, performing low-level network programming tasks such as creating TCP/UDP clients and servers becomes approachable even for those new to Go. By experimenting further, you can expand the functionality to meet more complex networking needs.

Next Article: Creating URL Parsers with the `net/url` Package in Go

Previous Article: Exploring the `net/http` Package for HTTP Servers and Clients in Go

Series: Working with Core package in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant