From 4e38318564b4fe27225a56b853b1844270a26394 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Mon, 7 Sep 2020 17:20:48 +0200 Subject: [PATCH] Programming exercises from week 1 --- week_1/exercise-2-2-client.go | 52 +++++++++++++++++++++ week_1/exercise-2-2-server.go | 85 +++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 week_1/exercise-2-2-client.go create mode 100644 week_1/exercise-2-2-server.go diff --git a/week_1/exercise-2-2-client.go b/week_1/exercise-2-2-client.go new file mode 100644 index 0000000..52e3392 --- /dev/null +++ b/week_1/exercise-2-2-client.go @@ -0,0 +1,52 @@ +package main + +import ( + "bufio" + "fmt" + "net" + "os" + "strings" +) + +// Declare variable of type net.Conn, called conn. +var conn net.Conn + +func send(conn net.Conn, reader *bufio.Reader) { + for { + fmt.Print("> ") + text, _ := reader.ReadString('\n') + if text == "quit\n" { return } + fmt.Fprintf(conn, text) + } +} + +func receive(conn net.Conn) { + for { + msg, err := bufio.NewReader(conn).ReadString('\n') + if err != nil { return } + fmt.Println("From server: " + msg) + fmt.Print("> ") + } +} + + +func main() { + + reader := bufio.NewReader(os.Stdin) + fmt.Println("Please provide IP address and port number in the format :") + fmt.Print("> ") + ipAndPort, err := reader.ReadString('\n') + + fmt.Println("Debug: " + ipAndPort) + if err != nil { return } + + // Attempt to connect to provided ipAndPort. + conn, _ = net.Dial("tcp", strings.TrimSpace(ipAndPort)) + defer conn.Close() + + go send(conn, reader) + go receive(conn) + + for {} + +} diff --git a/week_1/exercise-2-2-server.go b/week_1/exercise-2-2-server.go new file mode 100644 index 0000000..8a589a6 --- /dev/null +++ b/week_1/exercise-2-2-server.go @@ -0,0 +1,85 @@ +package main + +import ( + "net" + "fmt" + "bufio" + "strings" + "os" + "strconv" + +) + +type Connections struct { + m map[string]net.Conn +} + +func (conns *Connections) Set(key string, val net.Conn) { + conns.m[key] = val +} + +func MakeConns() *Connections { + conns := new(Connections) + conns.m = make(map[string]net.Conn) + return conns +} + +func PrintHostNames() { + // _ is convention for throwing the return value away + name, _ := os.Hostname() + addrs, _ := net.LookupHost(name) + fmt.Println("Name: " + name) + + for indx, addr := range addrs { + fmt.Println("Address number " + strconv.Itoa(indx) + ": " + addr) + } +} + +func HandleConnection(conn net.Conn, outputs chan string, conns *Connections) { + defer conn.Close() + + otherEnd := conn.RemoteAddr().String() + + conns.Set(otherEnd, conn) + + for { + msg, err := bufio.NewReader(conn).ReadString('\n') + if err != nil { + fmt.Println("Ending session with " + otherEnd) + delete(conns.m, otherEnd) + return + } else { + fmt.Print("From " + otherEnd + ": ", string(msg)) + msgString := fmt.Sprintf("%s : %s", otherEnd, string(msg)) + outputs <- msgString + } + } +} + +func Broadcast(c chan string, conns *Connections) { + for { + msg := <- c + titlemsg := strings.Title(msg) + for k := range conns.m { + conns.m[k].Write([]byte(titlemsg)) + fmt.Printf("Sending %s to %s\n", strings.TrimSpace(msg), k) + } + } + +} + +func main() { + PrintHostNames() + var conns = MakeConns() + outbound := make(chan string) + go Broadcast(outbound, conns) + ln, _ := net.Listen("tcp", ":18081") + defer ln.Close() + for { + fmt.Println("Listening for connections on port 18081...") + + conn, _ := ln.Accept() + fmt.Println("Got a connection...") + go HandleConnection(conn, outbound, conns) + } +}