Created
February 26, 2017 09:55
-
-
Save annanay25/e54b898cf0c44c67b19b697560d0bba1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"net" | |
"os" | |
) | |
func sendResponse(conn *net.UDPConn, addr *net.UDPAddr){ | |
_,err := conn.WriteToUDP([]byte("Hello from Server. Message received. "), addr) | |
if err != nil { | |
fmt.Printf("Couldn't send response %v", err) | |
} | |
} | |
func main(){ | |
/* Create the server. */ | |
ServerAddr, err := net.ResolveUDPAddr("udp", "0.0.0.0:11000") | |
if(err != nil){ | |
fmt.Println("Error ", err) | |
os.Exit(0) | |
} | |
/* Equivalent to listen */ | |
ServerConn, err := net.ListenUDP("udp", ServerAddr) | |
if(err != nil){ | |
fmt.Println("Error ", err) | |
os.Exit(0) | |
} | |
/* Defer is executed when the surrounding function returns. */ | |
defer ServerConn.Close() | |
buf := make([]byte, 1024) | |
for { | |
/* Equivalent to accept */ | |
n, addr, err := ServerConn.ReadFromUDP(buf) | |
fmt.Println("Received from ", addr," value: ", string(buf[0:n])) | |
if(err != nil){ | |
fmt.Println("Error ", err) | |
os.Exit(0) | |
} | |
go sendResponse(ServerConn, addr) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment