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 @@
package main
import (
"fmt"
"net"
"strconv"
"fmt"
)
const PORT = 25000
func main() {
server, err := net.Listen("tcp", ":" + strconv.Itoa(PORT))
server, err := net.Listen("tcp", ":"+strconv.Itoa(PORT))
if server == nil {
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
go func() {
for {
client, err := listener.Accept()
client, err := server.Accept()
if client == nil {
fmt.Printf("couldn't accept: " + err.Error())
continue
}
i++
fmt.Printf("%d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr())
ch <- client
go handleConn(client)
}
}()
return ch
}
func handleConn(client net.Conn) {
defer client.Close()
buf := make([]byte, 102400)
for {
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