Sobes.tech
Senior

[name] asked: Have you ever worked with TCP, UDP, or other protocols?

sobes.tech AI

Answer from AI

Yes, I have worked with TCP and UDP protocols. For example, in Go projects, I used the net package to create TCP servers and clients, ensuring reliable data transmission with error checking and packet order. UDP was used for tasks where speed is important and packet loss is acceptable, such as streaming audio or video. Here's a simple example of a TCP server in Go:

package main

import (
    "fmt"
    "net"
)

func main() {
    ln, err := net.Listen("tcp", ":8080")
    if err != nil {
        panic(err)
    }
    defer ln.Close()

    for {
        conn, err := ln.Accept()
        if err != nil {
            continue
        }
        go func(c net.Conn) {
            defer c.Close()
            buf := make([]byte, 1024)
            n, err := c.Read(buf)
            if err == nil {
                fmt.Println("Received:", string(buf[:n]))
                c.Write([]byte("Hello from server"))
            }
        }(conn)
    }
}