init llm assisted tcp client
This commit is contained in:
parent
908d563bc4
commit
3f54203246
12 changed files with 584 additions and 394 deletions
|
|
@ -1,24 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Initialize JSON logger
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
addr := "192.168.1.153:8080"
|
||||
fmt.Printf("Starting TCP server on port %s\n", addr)
|
||||
//Address to bind to
|
||||
slog.Info("Starting TCP server", "address", addr)
|
||||
|
||||
//Resolve address
|
||||
addr_, err := net.ResolveTCPAddr("tcp", addr)
|
||||
if err != nil {
|
||||
fmt.Printf("Error resolving address: %s", err.Error())
|
||||
slog.Error("Error resolving address", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
//Listen for incoming connections
|
||||
listener, err := net.ListenTCP("tcp", addr_)
|
||||
if err != nil {
|
||||
fmt.Printf("Error starting server: %s", err.Error())
|
||||
slog.Error("Error starting server", "error", err)
|
||||
return
|
||||
}
|
||||
defer listener.Close()
|
||||
|
|
@ -27,7 +33,7 @@ func main() {
|
|||
//Accept incoming connection
|
||||
conn, err := listener.AcceptTCP()
|
||||
if err != nil {
|
||||
fmt.Printf("Error accepting connection: %s", err.Error())
|
||||
slog.Error("Error accepting connection", "error", err)
|
||||
continue
|
||||
}
|
||||
go handleConnection(conn)
|
||||
|
|
@ -36,25 +42,26 @@ func main() {
|
|||
|
||||
func handleConnection(conn *net.TCPConn) {
|
||||
defer conn.Close()
|
||||
fmt.Printf("New connection from %s\n", conn.RemoteAddr().String())
|
||||
slog.Info("New connection", "remote_addr", conn.RemoteAddr().String())
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
for {
|
||||
//Read up to 1024 bytes
|
||||
n, err := conn.Read(buffer)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading from connection: %s", err.Error())
|
||||
slog.Error("Error reading from connection", "error", err)
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
fmt.Printf("Received message: %s\n", string(buffer[:n]))
|
||||
slog.Info("Received message", "message", string(buffer[:n]), "bytes", n)
|
||||
|
||||
//Echo message back
|
||||
// _, err = conn.Write(buffer[:n])
|
||||
_, err = conn.Write([]byte("1"))
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing to connection: %s", err.Error())
|
||||
slog.Error("Error writing to connection", "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue