-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
66 lines (51 loc) · 1.21 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
A very simple TCP server written in Go.
This is a toy project that I used to learn the fundamentals of writing
Go code and doing some really basic network stuff.
Maybe it will be fun for you to read. It's not meant to be
particularly idiomatic, or well-written for that matter.
*/
package main
import (
"bufio"
"fmt"
"net"
"strings"
)
var addr = "localhost"
var port = "9090"
func startServer() {
fmt.Println("Starting server...")
src := "localhost:9090"
listener, _ := net.Listen("tcp", src)
fmt.Printf("Listening on %s.\n", src)
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
fmt.Printf("Some connection error: %s\n", err)
}
go serverHandleConnection(conn)
}
}
func serverHandleConnection(conn net.Conn) {
remoteAddr := conn.RemoteAddr().String()
fmt.Println("Client connected from " + remoteAddr)
scanner := bufio.NewScanner(conn)
for {
ok := scanner.Scan()
if !ok {
break
}
serverHandleMessage(scanner.Text(), conn)
}
fmt.Println("Client at " + remoteAddr + " disconnected.")
}
func serverHandleMessage(message string, conn net.Conn) {
msg := strings.TrimSpace(message)
if len(msg) > 0 {
if msg != "YDB>" {
IRC.Privmsg("#alyx", msg)
}
}
}