Skip to content

Commit cd69a55

Browse files
committed
go server written
1 parent e703e89 commit cd69a55

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

Go/src/client/main.go

+14-26
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
package main
22

33
import (
4+
"encoding/binary"
45
"log"
56
"net"
6-
//"fmt"
7-
//"bytes"
8-
"encoding/binary"
97

108
"code.google.com/p/goprotobuf/proto"
119
"example_mess"
1210
)
1311

14-
var _ = net.Dial
15-
//var _ = bytes.Buffer
12+
func passOrDie(err error) {
13+
if err != nil {
14+
log.Fatal(err)
15+
}
16+
}
1617

1718
func main() {
18-
1919
socket, err := net.Dial("unix", "socket")
20-
if err!= nil {
21-
log.Fatal("Failed to connect to socket: ", err)
22-
}
23-
20+
passOrDie(err)
2421
defer socket.Close()
2522

26-
test := &example_mess.Boring {
23+
msg := &example_mess.Boring{
2724
Cont: proto.String("Greets from Go"),
2825
}
29-
serialized, err := proto.Marshal(test)
30-
if err != nil {
31-
log.Fatal("marshaling error: ", err)
32-
}
33-
34-
35-
//buf := new(bytes.Buffer)
36-
println(len(serialized))
37-
//err = binary.Write(buf, binary.BigEndian, int32(1337))
38-
//fmt.Printf("% d", buf.Bytes())
39-
26+
27+
serialized, err := proto.Marshal(msg)
28+
passOrDie(err)
29+
4030
// Go is unique in that, thanks to its interfaces, it's practical to encode directly to the socket
4131
err = binary.Write(socket, binary.BigEndian, int32(len(serialized)))
42-
if err != nil {
43-
log.Fatal("failed writing to socket: ", err)
44-
}
45-
32+
passOrDie(err)
33+
4634
socket.Write(serialized)
4735
}

Go/src/server/main.go

+40-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
11
package main
22

33
import (
4+
"encoding/binary"
45
"log"
6+
"net"
7+
"syscall"
58

69
"code.google.com/p/goprotobuf/proto"
710
"example_mess"
811
)
912

10-
func main() {
11-
test := &example_mess.Boring {
12-
Cont: proto.String("wew"),
13-
}
14-
_, err := proto.Marshal(test)
13+
func passOrDie(err error) {
1514
if err != nil {
16-
log.Fatal("marshaling error: ", err)
15+
log.Fatal(err)
1716
}
1817
}
18+
19+
func handler(connection net.Conn) {
20+
var message_length int32
21+
22+
// encoding/binary is quite clever! Notice that we're reading from the socket and encoding directly into the int32 here. Exactly 4 bytes (the size of message_length) will be read.
23+
err := binary.Read(connection, binary.BigEndian, &message_length)
24+
passOrDie(err)
25+
26+
println(message_length)
27+
28+
msg := new(example_mess.Boring)
29+
30+
buf := make([]byte, message_length)
31+
err = binary.Read(connection, binary.BigEndian, buf)
32+
passOrDie(err)
33+
err = proto.Unmarshal(buf, msg)
34+
passOrDie(err)
35+
36+
println("Message: ", msg.GetCont())
37+
38+
}
39+
40+
func main() {
41+
syscall.Unlink("socket")
42+
listener, err := net.Listen("unix", "socket")
43+
passOrDie(err)
44+
45+
for {
46+
connection, err := listener.Accept()
47+
passOrDie(err)
48+
49+
go handler(connection)
50+
}
51+
52+
}

0 commit comments

Comments
 (0)