package cmd import ( "bufio" "fmt" "log" "net" "strings" "time" ) var ( listenPort string = "1302" ) type Creator interface { CreateUser() } type User struct { Username string IP string } type Message struct { Contents string Date time.Time } func (u User) CreateUser(usr string, ip string) User { u.Username = usr u.IP = ip return u } func Server() { ln, err := net.Listen("tcp", ":"+listenPort) if err != nil { log.Fatalf("Error happened trying to listen on port: %v\n", err) } defer ln.Close() fmt.Printf("Listening on port %v...\n", listenPort) for { conn, err := ln.Accept() if err != nil { log.Fatalf("Error happened trying to accept connection: %v\n", err) } go handleConn(conn) } } func getUsername(conn net.Conn) string { conn.Write([]byte("What's your name?\n\r")) conn.Write([]byte("Choice: ")) name, err := bufio.NewReader(conn).ReadString('\n') if err != nil { log.Fatalf("Error occurred getting username: %v\n", err) } trimmedName := strings.TrimRight(name, "\n") return trimmedName } func getUserInput() { } func handleConn(conn net.Conn) { defer conn.Close() newUserTemplate := new(User) newUser := newUserTemplate.CreateUser(getUsername(conn), getIP(conn)) fmt.Printf("Received connection from %v at %v\n", newUser.Username, newUser.IP) fmt.Fprintln(conn, "Hello buddy!") } func getIP(conn net.Conn) (IP string) { if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok { IP = fmt.Sprintf("%v", addr.IP) } return IP }