// socket-server project main.go package main import ( "fmt" "net" "os" "strings" ) const ( SERVER_HOST = "localhost" SERVER_PORT = "9988" SERVER_TYPE = "tcp" MAGIC_HEADER = "e8437140-4347-48cc-a31d-dcdc944ffc15" ) func main() { 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")) connection.Close() }