Created
March 16, 2017 22:24
-
-
Save jordanorelli/dd7e9f2d823162fcf14820753af7210f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"log" | |
"net" | |
) | |
func handle(conn *net.TCPConn) { | |
defer conn.Close() | |
addr := conn.RemoteAddr() | |
br := bufio.NewReader(conn) | |
for { | |
line, err := br.ReadString('\n') | |
if err != nil { | |
log.Printf("[%v] unable to read a line: %v\n", addr, err) | |
return | |
} | |
log.Printf("[%v] %s", addr, line) | |
} | |
} | |
func main() { | |
l, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IPv4(0, 0, 0, 0)}) | |
if err != nil { | |
log.Fatalf("unable to open a tcp port: %v\n", err) | |
} | |
log.Printf("listening for tcp connections on %v\n", l.Addr()) | |
for { | |
conn, err := l.AcceptTCP() | |
if err != nil { | |
log.Fatalf("unable to handle a new incoming connection: %v\n", err) | |
} | |
log.Printf("accepted a connection: %v\n", conn) | |
go handle(conn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment