Programming exercises from week 1

This commit is contained in:
Alexander Munch-Hansen 2020-09-07 17:20:48 +02:00
commit 4e38318564
2 changed files with 137 additions and 0 deletions

View File

@ -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 <ip>:<port>")
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 {}
}

View File

@ -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)
}
}