Skip to content

Commit

Permalink
Added upd client server examples in go
Browse files Browse the repository at this point in the history
  • Loading branch information
Nipun Talukdar committed Sep 25, 2014
1 parent 5f05eef commit 8ddd596
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
23 changes: 23 additions & 0 deletions go/udpclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main
import (
"fmt"
"net"
"bufio"
)

func main() {
p := make([]byte, 2048)
conn, err := net.Dial("udp", "127.0.0.1:1234")
if err != nil {
fmt.Printf("Some error %v", err)
return
}
fmt.Fprintf(conn, "Hi UDP Server, How are you doing?")
_, err = bufio.NewReader(conn).Read(p)
if err == nil {
fmt.Printf("%s\n", p)
} else {
fmt.Printf("Some error %v\n", err)
}
conn.Close()
}
36 changes: 36 additions & 0 deletions go/udpserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main
import (
"fmt"
"net"
)


func sendResponse(conn *net.UDPConn, addr *net.UDPAddr) {
_,err := conn.WriteToUDP([]byte("From server: Hello I got your mesage "), addr)
if err != nil {
fmt.Printf("Couldn't send response %v", err)
}
}


func main() {
p := make([]byte, 2048)
addr := net.UDPAddr{
Port: 1234,
IP: net.ParseIP("127.0.0.1"),
}
ser, err := net.ListenUDP("udp", &addr)
if err != nil {
fmt.Printf("Some error %v\n", err)
return
}
for {
_,remoteaddr,err := ser.ReadFromUDP(p)
fmt.Printf("Read a message from %v %s \n", remoteaddr, p)
if err != nil {
fmt.Printf("Some error %v", err)
continue
}
go sendResponse(ser, remoteaddr)
}
}

0 comments on commit 8ddd596

Please sign in to comment.