Trying to get the server to work
This commit is contained in:
parent
8cd45de78f
commit
b92ab38e8d
32
server.go
32
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue