-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
40 lines (34 loc) · 811 Bytes
/
server.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
package main
import (
"fmt"
"net"
)
func main(){
//create a UDP socket on PORT 8080
address := "localhost:8080"
udpAddr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
fmt.Println("Error resolving address:", err)
return
}
//listening to UDP connection
conn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
fmt.Println("Error listening:", err)
return
}
defer conn.Close()
fmt.Printf("Listening on %s\n", address)
//buffer for accepting data
buffer := make([]byte, 1024)
for {
//accepting datagram
n, addr, err := conn.ReadFromUDP(buffer)
if err != nil {
fmt.Println("Error reading from UDP:", err)
return
}
//displaying the data accepted
fmt.Printf("Received %s from %s\n",string(buffer[:n]), addr)
}
}