UDP Client and Server in Go vs Python

Things were a bit slow today at work so I decided that it would be best to make use of some of my downtime . For a while I’ve been looking to start delving into some network programming to make a small networked game and rather than stick to something that I’ve used for a while I thought it would be good to check out one of these new-fangled concurrent compiled languages that all of the cool kids are talking about.  After some debate between Rust and Go I ultimately decided on Go due to its fairly well developed standard library and its adoption by Google (If you’re are a Google recruiter and reading this I am currently available for hire.). After finishing the Go Tour (Which is awesome by the way),  I thought that when I got home I would take a shot at writing a small UDP server and client and compare it to its equivalent in Python.

Lets start with the clients:

Python

import socket
 
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = "I'm from Python!"
 
def main():
 
	print "UDP target IP:", UDP_IP
	print "UDP target port:", UDP_PORT
	print "message:", MESSAGE
 
	sock = socket.socket(socket.AF_INET, # Internet
	                     socket.SOCK_DGRAM) # UDP
	sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
 
main()

 

The Python bits were taken from the Python page for UDP communications. Next is the Go code:

Go

package main
 
import (
	"fmt"
	"net"
)
 
func main() {
	writeUDP()
}
 
func writeUDP() {
 
	message := "I'm from Go!"
 
	serverAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:5005")
	if err != nil {
		fmt.Println(err)
	}
	con, err := net.DialUDP("udp", nil, serverAddr)
 
	if err != nil {
		fmt.Println(err)
	}
	con.Write([]byte(message))
 
}

 

 

Believe it or not, when you take out error handling, there is actually not that much of a difference in line count. In fact I might actually prefer the Go version to the Python implementation.

 

Now its server time:

import socket
 
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
 
def main():
 
	sock = socket.socket(socket.AF_INET, # Internet
	                     socket.SOCK_DGRAM) # UDP
	sock.bind((UDP_IP, UDP_PORT))
 
	while True:
	    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
	    print "received message:", data
 
main()

 

And finally Go:

package main
 
import (
	"fmt"
	"net"
)
 
func main() {
	listenUDP()
}
 
func listenUDP() {
 
	serverAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:5005")
 
	if err != nil {
		fmt.Println(err)
	}
 
	con, err := net.ListenUDP("udp", serverAddr)
 
	if err != nil {
		fmt.Println(err)
	}
 
	for {
		rdata := make([]byte, 1024)
		con.Read(rdata)
		fmt.Println(string(rdata))
	}
 
}

The code itself is pretty straight forward. I’m looking forward to adding some of Go’s amazing concurrency features when I begin to work on some complicated examples.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>