-
Notifications
You must be signed in to change notification settings - Fork 1
/
servidorEstructuras.go
146 lines (111 loc) · 2.53 KB
/
servidorEstructuras.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// server.go
package main
import (
"bytes"
"encoding/gob"
"fmt"
"math/rand"
"net"
"os"
"time"
)
const MIN = 1
const MAX = 100
func random() int {
return rand.Intn(MAX-MIN) + MIN
}
func handleConnection(c net.Conn) {
fmt.Printf("Serving %s\n", c.RemoteAddr().String())
for {
//_, err := conn.Read(tmp)
//conn.Read(tmp)
tmp := make([]byte, 500)
_, err := c.Read(tmp)
if err != nil {
fmt.Println(err)
return
}
// convert bytes into Buffer (which implements io.Reader/io.Writer)
tmpbuff := bytes.NewBuffer(tmp)
tmpstruct := new(Message)
// creates a decoder object
gobobj := gob.NewDecoder(tmpbuff)
// decodes buffer and unmarshals it into a Message struct
gobobj.Decode(tmpstruct)
// lets print out!
fmt.Println(tmpstruct) // reflects.TypeOf(tmpstruct) == Message{}
//c.Write([]byte(string(result)))
/*
/////////////////
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
temp := strings.TrimSpace(string(netData))
if temp == "STOP" {
break
}
result := strconv.Itoa(random()) + "\n"
fmt.Println("Escribiendo")
c.Write([]byte(string(result)))
*/
}
fmt.Println("Cerrando conexión!")
//c.Close()
c.Close()
}
// Create your custom data struct
type Message struct {
ID string
Data string
}
/*
func main() {
// for purpose of verbosity, I will be removing error handling from this
// sample code
server, _ := net.Listen("tcp", ":12345")
conn, _ := server.Accept()
// create a temp buffer
tmp := make([]byte, 500)
// loop through the connection to read incoming connections. If you're doing by
// directional, you might want to make this into a seperate go routine
for {
//_, err := conn.Read(tmp)
conn.Read(tmp)
// convert bytes into Buffer (which implements io.Reader/io.Writer)
tmpbuff := bytes.NewBuffer(tmp)
tmpstruct := new(Message)
// creates a decoder object
gobobj := gob.NewDecoder(tmpbuff)
// decodes buffer and unmarshals it into a Message struct
gobobj.Decode(tmpstruct)
// lets print out!
fmt.Println(tmpstruct) // reflects.TypeOf(tmpstruct) == Message{}
}
}
*/
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide a port number!")
return
}
PORT := ":" + arguments[1]
//PORT = ":12345"
l, err := net.Listen("tcp4", PORT)
if err != nil {
fmt.Println(err)
return
}
//defer l.Close()
rand.Seed(time.Now().Unix())
for {
c, err := l.Accept()
if err != nil {
fmt.Println(err)
return
}
go handleConnection(c)
}
}