Skip to content

Instantly share code, notes, and snippets.

@dohzya
Created June 4, 2010 08:12
Show Gist options
  • Save dohzya/425139 to your computer and use it in GitHub Desktop.
Save dohzya/425139 to your computer and use it in GitHub Desktop.
package main
import (
"net"
"fmt"
"flag"
"log"
)
var listenPort = flag.Int("port", 7331, "port")
var verbose = flag.Bool("verbose", false, "display more informations")
var stop = false
func serve(conn net.Conn) {
buffer := make([]byte, 64)
for !stop {
_, err := conn.Read(buffer)
if err != nil {
fmt.Printf("serve: error occured: %v\n", err)
stop = true
return
}
fmt.Printf("%s", buffer)
}
conn.Close()
}
func main() {
flag.Parse()
if *verbose { fmt.Printf("Listen to port %v...\n", *listenPort) }
addr := fmt.Sprintf("127.0.0.1:%d", *listenPort)
listener, e := net.Listen("tcp", addr)
if e != nil { log.Exit("could not open connection") }
fmt.Printf("listener: %v\n", listener)
for !stop {
conn, err := listener.Accept() // il faudrait pouvoir le "reveiller" de force en cas d'arret
if err != nil { log.Exit("accept: error occured:", err) }
go serve(conn)
}
listener.Close()
fmt.Printf("Good bye\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment