forked from methane/echoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_server_go.go
47 lines (41 loc) · 900 Bytes
/
echo_server_go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Original: http://d.hatena.ne.jp/tokuhirom/20091112/1257995906
package main
import (
"net";
"os";
"fmt";
)
func Err(format string, v ...os.Error) {
fmt.Fprintf(os.Stderr, format + "\n", v)
}
func Handler(conn net.Conn) {
defer conn.Close();
buffer := make([]byte, 24);
for {
l, e := conn.Read(buffer);
switch {
case e == nil:
conn.Write(buffer[0:l])
case e == os.EOF:
return;
case e != os.EAGAIN:
Err("Err on receiving a header (%s)", e);
return;
}
}
}
func main() {
psock, e := net.Listen("tcp", ":5000");
if e != nil {
Err("an error occured(%s)", e);
return
}
for {
conn, e := psock.Accept();
if e != nil {
Err("an error occured(%s)", e);
return
}
go Handler(conn);
}
}