A Client / Server prototype in Go

by Marín

I started hacker school two weeks ago, one of my goals during this time is become proficient in go or golang. So to get hands towards that goal I started working on “A tour of Go”, I’ve managed to get trough all the tour and became some way comfortable with the language and at that point I decided to start a small project looking for enlightenment and of course my “Eureka moment” in Go.

The project started as an FTP client, but it quickly evolved in a Client / Server conversation, a limited conversation. As a client you are able to connect to an IP:PORT server and send three commands to the server: {1: ls, 2: who, 3: pwd}. As a server you are able to bind a port on your localhost and listen, execute and resolve (return) the requests of the client. The server is able to support multiple clients and request at the same time thanks to Goroutines.

So, what challenges I’ve faced during this project? well, being my first project in go the simple fact to make the program compile was a challenge in the beginning but my concerns quickly evolved as my understanding of the language too. Maybe one of the “really think about” parts of the project was the for loop which allows me to read a response from the server:


readed := 0
response := make([]byte, 8)

for {
    //Read from server
    readed, err = con.Read(response)
    fmt.Print(string(response))
    if readed != len(response) {
    //Read request from keyboard
        fmt.Print("ftp> ")
        _, err = fmt.Scanf("%s", &request)
     if request == "quit" {
         break
     }
     //Send to server
     _, err = con.Write([]byte(request))
     //Valid terminated string
     _, err = con.Write([]byte("\r"))
    }
    if err != nil {
        return fmt.Errorf("Interact error: %s", err)
    }
    response = make([]byte, 8)
}

When am I going to stop? how do I know that all the data has been received and the request finished? Well, the previous code addresses all that concerns and works as long as it receives a header from the server, that means the header has to write first.

I can happily conclude that this project teach me a lot of Go, I feel way more confortable at the language now than two weeks ago (self.go==NULL) I know that maybe this isn’t the best go code that the world has ever seen but I can feel myself learning and at this point I’m happy with that, now I’ll be back to work!

If you are interested, here is the Github repo of the project.

Cheers!