Skip to content

Commit

Permalink
Added examples golang net lib, tcp and udp
Browse files Browse the repository at this point in the history
  • Loading branch information
k8s4 committed Oct 24, 2024
1 parent 194ecf3 commit 48a2ab6
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/golang/cource/06e_network/clients/tcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package client

import (
"fmt"
"net"
"bufio"
)

func TCPClient() {
buf := make([]byte, 1024)
conn, err := net.Dial("tcp", "localhost:1900")
if err != nil {
fmt.Printf("Connection error: %v", err)
return
}
fmt.Fprintf(conn, "Some message to server!")
_, err = bufio.NewReader(conn).Read(buf)
if err == nil {
fmt.Printf("Some in buf: %s", buf)
} else {
fmt.Printf("Read error: %s", err)
}
conn.Close()
}
24 changes: 24 additions & 0 deletions examples/golang/cource/06e_network/clients/udp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package client

import (
"fmt"
"net"
"bufio"
)

func UDPClient() {
buf := make([]byte, 1024)
conn, err := net.Dial("udp", "localhost:1901")
if err != nil {
fmt.Printf("Connection error: %v", err)
return
}
fmt.Fprintf(conn, "Some message to UDP server!")
_, err = bufio.NewReader(conn).Read(buf)
if err == nil {
fmt.Printf("Some in buf: %s", buf)
} else {
fmt.Printf("Read error: %s", err)
}
conn.Close()
}
3 changes: 3 additions & 0 deletions examples/golang/cource/06e_network/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module 06e_network

go 1.23.2
15 changes: 15 additions & 0 deletions examples/golang/cource/06e_network/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
// "golang/cource/06e_network/servers"
"golang/cource/06e_network/clients"
)

func main() {
fmt.Println("Started test servers.")
// server.TCPServer()
// server.UDPServer()
// client.TCPClient()
client.UDPClient()
}
41 changes: 41 additions & 0 deletions examples/golang/cource/06e_network/servers/tcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package server

import (
"fmt"
"net"
"os"
)

func TCPServer() {
listener, err := net.Listen("tcp", "localhost:1900")
if err != nil {
fmt.Println("Bind listener error: ", err.Error())
os.Exit(1)
}
defer listener.Close()
fmt.Println("Bind on: localhost:1900")
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
go handleRequest(conn)
}
}

func handleRequest(conn net.Conn) {
buf := make([]byte, 1024)

reqLen, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading: ", err.Error())
}
fmt.Println("Some recieved:", reqLen)
bytesWritten, err := conn.Write([]byte("Something recieved."))
if err != nil {
fmt.Println("Error writing: ", err.Error())
}
fmt.Printf("Bytes written: %d\n", bytesWritten)
conn.Close()
}
36 changes: 36 additions & 0 deletions examples/golang/cource/06e_network/servers/udp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package server

import (
"fmt"
"net"
)

func sendResponse(conn *net.UDPConn, addr *net.UDPAddr) {
_, err := conn.WriteToUDP([]byte("Response from server..."), addr)
if err != nil {
fmt.Printf("Couldn't send response: %v\n", err)
}
}

func UDPServer() {
buf := make([]byte, 2048)
addr := net.UDPAddr{
Port: 1901,
IP: net.ParseIP("127.0.0.1"),
}
listener, err := net.ListenUDP("udp", &addr)
if err != nil {
fmt.Printf("Listen UDP error: %v\n", err)
return
}
fmt.Println("listening on localhost:1901.")
for {
_, remoteaddr, err := listener.ReadFromUDP(buf)
fmt.Printf("Buffer recieved: %s", buf)
if err != nil {
fmt.Printf("Read UDP error: %v\n", err)
return
}
go sendResponse(listener, remoteaddr)
}
}

0 comments on commit 48a2ab6

Please sign in to comment.