You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
917 B
50 lines
917 B
7 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
const (
|
||
7 years ago
|
CONN_HOST = "localhost"
|
||
7 years ago
|
CONN_PORT = "3000"
|
||
|
CONN_TYPE = "tcp"
|
||
7 years ago
|
CONN_URL = CONN_HOST + ":" + CONN_PORT
|
||
7 years ago
|
)
|
||
|
|
||
|
func main() {
|
||
|
// Listen for incoming connections.
|
||
7 years ago
|
l, err := net.Listen(CONN_TYPE, CONN_URL)
|
||
7 years ago
|
if err != nil {
|
||
|
fmt.Println("Error listening:", err.Error())
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
// Close the listener when the application closes.
|
||
|
defer l.Close()
|
||
7 years ago
|
fmt.Println("Listening on " + CONN_URL)
|
||
7 years ago
|
for {
|
||
|
// Listen for an incoming connection.
|
||
|
conn, err := l.Accept()
|
||
|
if err != nil {
|
||
|
fmt.Println("Error accepting: ", err.Error())
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
// Handle connections in a new goroutine.
|
||
|
go handleRequest(conn)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Handles incoming requests.
|
||
|
func handleRequest(conn net.Conn) {
|
||
7 years ago
|
for {
|
||
|
data, err := bufio.NewReader(conn).ReadString('\n')
|
||
|
if err != nil {
|
||
|
fmt.Println("Error reading:", err.Error())
|
||
|
break
|
||
|
}
|
||
|
fmt.Println(data)
|
||
7 years ago
|
}
|
||
|
}
|