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,52 +3,41 @@
package main
import (
"net"
"strconv"
"fmt"
"fmt"
"net"
"strconv"
)
const PORT = 25000
func main() {
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()
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
}
}()
return ch
server, err := net.Listen("tcp", ":"+strconv.Itoa(PORT))
if server == nil {
panic("couldn't start listening: " + err.Error())
}
i := 0
for {
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())
go handleConn(client)
}
}
func handleConn(client net.Conn) {
buf := make([]byte, 102400)
for {
reqLen, err := client.Read(buf)
if err != nil {
break
}
if reqLen > 0 {
client.Write(buf[:reqLen])
}
}
defer client.Close()
buf := make([]byte, 102400)
for {
reqLen, err := client.Read(buf)
if err != nil {
break
}
if reqLen > 0 {
client.Write(buf[:reqLen])
}
}
}
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