Commit 29a5c941 authored by Elvis Pranskevichus's avatar Elvis Pranskevichus

Merge pull request #4 from gallir/master

A simpler and perhaps faster server
parents eb6e8940 ab01099e
...@@ -3,44 +3,33 @@ ...@@ -3,44 +3,33 @@
package main package main
import ( import (
"fmt"
"net" "net"
"strconv" "strconv"
"fmt"
) )
const PORT = 25000 const PORT = 25000
func main() { func main() {
server, err := net.Listen("tcp", ":" + strconv.Itoa(PORT)) server, err := net.Listen("tcp", ":"+strconv.Itoa(PORT))
if server == nil { if server == nil {
panic("couldn't start listening: " + err.Error()) panic("couldn't start listening: " + err.Error())
} }
conns := clientConns(server)
for {
go handleConn(<-conns)
}
}
func clientConns(listener net.Listener) chan net.Conn {
ch := make(chan net.Conn)
i := 0 i := 0
go func() {
for { for {
client, err := listener.Accept() client, err := server.Accept()
if client == nil { if client == nil {
fmt.Printf("couldn't accept: " + err.Error()) fmt.Printf("couldn't accept: " + err.Error())
continue continue
} }
i++ i++
fmt.Printf("%d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr()) fmt.Printf("%d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr())
ch <- client go handleConn(client)
} }
}()
return ch
} }
func handleConn(client net.Conn) { func handleConn(client net.Conn) {
defer client.Close()
buf := make([]byte, 102400) buf := make([]byte, 102400)
for { for {
reqLen, err := client.Read(buf) reqLen, err := client.Read(buf)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment