Created
October 22, 2014 18:27
-
-
Save zubietaroberto/0beed814f8df28036af5 to your computer and use it in GitHub Desktop.
Golang UDP Socket Server
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
/* | |
This script creates a simple UDP Server that exports all data received | |
through the 8080 socket into the console. | |
Made By: Roberto E. Zubieta | |
Panama City, Panamá | |
G+: https://plus.google.com/u/0/105524772414753584405/ | |
*/ | |
package main | |
import ( | |
"encoding/hex" | |
"fmt" | |
"net" | |
) | |
func main() { | |
fmt.Println("Hello World!") | |
//Basic variables | |
port := ":8080" | |
protocol := "udp" | |
//Build the address | |
udpAddr, err := net.ResolveUDPAddr(protocol, port) | |
if err != nil { | |
fmt.Println("Wrong Address") | |
return | |
} | |
//Output | |
fmt.Println("Coded by Roberto E. Zubieta\nReading " + protocol + " from " + udpAddr.String()) | |
//Create the connection | |
udpConn, err := net.ListenUDP(protocol, udpAddr) | |
if err != nil { | |
fmt.Println(err) | |
} | |
//Keep calling this function | |
for { | |
display(udpConn) | |
} | |
} | |
func display(conn *net.UDPConn) { | |
var buf [2048]byte | |
n, err := conn.Read(buf[0:]) | |
if err != nil { | |
fmt.Println("Error Reading") | |
return | |
} else { | |
fmt.Println(hex.EncodeToString(buf[0:n])) | |
fmt.Println("Package Done") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment