gibtar/main.go

54 lines
1.7 KiB
Go
Raw Permalink Normal View History

2023-06-19 16:09:19 -07:00
// socket-server project main.go
2023-06-19 14:30:28 -07:00
package main
2023-06-19 16:09:19 -07:00
import (
"fmt"
"net"
"os"
"strings"
2023-06-19 16:09:19 -07:00
)
const (
SERVER_HOST = "localhost"
SERVER_PORT = "9988"
SERVER_TYPE = "tcp"
MAGIC_HEADER = "e8437140-4347-48cc-a31d-dcdc944ffc15"
2023-06-19 16:09:19 -07:00
)
2023-06-19 14:30:28 -07:00
func main() {
2023-06-19 16:09:19 -07:00
fmt.Println("Server Running...")
server, err := net.Listen(SERVER_TYPE, SERVER_HOST+":"+SERVER_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
defer server.Close()
fmt.Println("Listening on " + SERVER_HOST + ":" + SERVER_PORT)
fmt.Println("Waiting for client...")
for {
connection, err := server.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
fmt.Println("client connected")
go processClient(connection)
}
}
func processClient(connection net.Conn) {
buffer := make([]byte, 1024)
mLen, err := connection.Read(buffer)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
// handshake
// split out the elements of the clients handshake
// read the first 40 bytes, check the magic header, reject invalids
// handle data format negotiation and respond with selected protocol
// store information about
// _, err = connection.Write([]byte("Thanks! Got your message:" + string(buffer[:mLen])))
data := string(buffer[:mLen])
parts := strings.Split(data, " ")
fmt.Println("Received: ", parts)
_, err = connection.Write([]byte("thanks"))
2023-06-19 16:09:19 -07:00
connection.Close()
2023-06-19 14:30:28 -07:00
}