Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UDP server / client in Go not reading data

Tags:

go

udp

I have written a UDP server and client in Go. I am not seeing any error messages when the server is running on port 1200, I also do not see any errors when client is trying to connect to port 1200 on the same machine (OS X 10.9.1)

The server is not printing "SOS...", that is the message the client is writing, in a infinite loop.

The client is able to send a message to the server though, however what the server is reading is 0 bytes.

Server Code

package main

import ( "net" "fmt" "time" )

func main() {

        port := "127.0.0.1:1200"

        udpAddress, err := net.ResolveUDPAddr("udp4",port)

        if err != nil {
                fmt.Println("error resolving UDP address on ", port)
                fmt.Println(err)
                return
        }

        conn ,err := net.ListenUDP("udp",udpAddress)

        if err != nil {
                fmt.Println("error listening on UDP port ", port)
                fmt.Println(err)
                return
        }

        defer conn.Close()

        var buf []byte

        for {

                time.Sleep(100 * time.Millisecond)

                n,address, err := conn.ReadFromUDP(buf)

                if err != nil {
                        fmt.Println("error reading data from connection")
                        fmt.Println(err)
                        return
                }

                if address != nil {

                        fmt.Println("got message from ", address, " with n = ", n)

                        if n > 0 {
                                fmt.Println("from address", address, "got message:", string(buf[0:n]), n)
                        }
                }
        }

}

client code, running on same server, with command go run udp-client.go :1200 or go run udp-client.go 127.0.0.1:1200

package main

import (
"fmt"
"net"
"os"
"time"
)

func main() {

        if len(os.Args) != 2{
                fmt.Fprintf(os.Stderr, "Usage:%s host:port", os.Args[0])
                os.Exit(1)
        }

        service := os.Args[1]

        fmt.Println("Connecting to server at ", service)

        conn, err := net.Dial("udp",service)

        if err != nil {
                fmt.Println("Could not resolve udp address or connect to it  on " , service)
                fmt.Println(err)
                return
        }

        fmt.Println("Connected to server at ", service)

        defer conn.Close()

        fmt.Println("About to write to connection")

        for {

                time.Sleep(1000*time.Millisecond)
                n, err := conn.Write([]byte("SOS ... \n"))
                if err != nil {
                        fmt.Println("error writing data to server", service)
                        fmt.Println(err)
                        return
                }

                if n > 0 {
                        fmt.Println("Wrote ",n, " bytes to server at ", service)
                }
        }

}
like image 352
ppone Avatar asked Dec 22 '13 15:12

ppone


1 Answers

The UDPConn.ReadFromUDP method reads data and puts it into the slice you provided. In your case this slice is nil. Therefore your buffer does not provide enough space for data. You can fix this by changing one line in the server code:

var buf []byte = make([]byte, 1500)

The buffer size should probably be chosen to fit your network protocol. Or you create a 64k buffer so that you can receive maximum sized udp packets. This however seems a bit wastefull :)

like image 88
Philipp Avatar answered Oct 18 '22 12:10

Philipp