-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added upd client server examples in go
- Loading branch information
Nipun Talukdar
committed
Sep 25, 2014
1 parent
5f05eef
commit 8ddd596
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |