From b92ab38e8ddab063c278ae815ee508d174dd9c5d Mon Sep 17 00:00:00 2001 From: raul Date: Sat, 16 Mar 2024 11:59:57 +0100 Subject: [PATCH] Trying to get the server to work --- server.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/server.go b/server.go index 7cf4ab4..e06b331 100644 --- a/server.go +++ b/server.go @@ -1,12 +1,23 @@ package main import ( + "bufio" "fmt" "net" + "strconv" + "strings" ) func server() { + ln, err := net.Listen("tcp", ":1302") + fmt.Printf("Listening on port 1302...\n") + catchErr(err) + for { + conn, err := ln.Accept() + catchErr(err) + go handleConn(conn) + } } func getIP(conn net.Conn) (address *net.TCPAddr) { @@ -21,4 +32,25 @@ func handleConn(conn net.Conn) { fmt.Println() address := getIP(conn) fmt.Printf("%v connected!\n", address) + go game() + for { + gameStatus() + conn.Write([]byte(gameStatus())) + conn.Write([]byte("X: ")) + x, err := bufio.NewReader(conn).ReadString('\n') + catchErr(err) + + conn.Write([]byte("Y: ")) + y, err := bufio.NewReader(conn).ReadString('\n') + x = strings.TrimRight(x, "\n") + y = strings.TrimRight(y, "\n") + catchErr(err) + xint, err := strconv.Atoi(x) + catchErr(err) + yint, err := strconv.Atoi(y) + catchErr(err) + xint-- + yint-- + checkShip(xint, yint) + } }