Created
March 31, 2011 01:45
-
-
Save border/895676 to your computer and use it in GitHub Desktop.
A Server By Go
This file contains 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 ( | |
"log" | |
"net" | |
) | |
func main() { | |
var conns []net.Conn | |
for i := 0; i < 20000; i++ { | |
c, err := net.Dial("tcp", "", "127.0.0.1:2020") | |
if err != nil { | |
log.Fatalf("dial error: %v", err) | |
} | |
conns = append(conns, c) | |
} | |
select {} | |
} |
This file contains 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 main() { | |
ln, err := net.Listen("tcp", ":2020") | |
if err != nil { | |
log.Fatalf("listen error: %v", err) | |
} | |
accepted := 0 | |
for { | |
conn, err := ln.Accept() | |
if err != nil { | |
log.Fatalf("accept error: %v", err) | |
} | |
accepted++ | |
go serve(conn) | |
log.Printf("Accepted %d", accepted) | |
} | |
} | |
func serve(conn net.Conn) { | |
bufr := bufio.NewReader(conn) | |
for { | |
line, err := bufr.ReadString('\n') | |
if err != nil { | |
return | |
} | |
conn.Write([]byte(line)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment