0

I am trying to create a simple TCP server using the Google Go language. I have a client which points to 127.0.0.1:8484. When my server code tries to read the buffer I receive an EOF error on line 37. The client sends an initial tcp packet denoting its client version and other settings (the client is from a game therefore I know that I am doing something wrong). I am trying to read this packet. However, it seems I call the read too late and the client has already sent the packet and is waiting for its response. Any help would be appreciated, as I am truly stumped.

package main

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

func main() {

    service := ":8484"
    listener, err := net.Listen("tcp", service)
    checkError(err)

    fmt.Println("Login Server Started\nListening on port:", service[1:])

    for {
        conn, err := listener.Accept()
        if err != nil {
            continue
        }

        go handleClient(conn)
    }
}

func handleClient(conn net.Conn) {

    const BUFFER_LENGTH = 1024

    defer conn.Close()

    fmt.Println("Client connected from", conn.RemoteAddr())
    var buf [BUFFER_LENGTH]byte

    for {
        n, err := conn.Read(buf[0:])
        if err != nil {
            checkError(err)
            return
        }

        _, err2 := conn.Write(buf[0:n])
        if err2 != nil {
            checkError(err)
            return
        }

    }
}

func checkError(err error) {
    if err != nil {
        fmt.Fprintf(os.Stderr, "Fatal error: %s\n", err.Error())
        os.Exit(1)
    }
}

Edit for clarity: 1. Client connects to server. 2. Client sends initial information to server e.g. client version. 3. Server reads the buffer. 4. Server responds (currently I am just echoing the buffer).

It is currently failing at the read buffer stage "n, err := conn.Read(buf[0:])".

Fixed: I was being an idiot and realised I needed to send a handshake packet to the client.

John
  • 343
  • 2
  • 5
  • 15

2 Answers2

1

Well, in checkError you do an explicit os.Exit(1), which kills your server if there is a Read() error from the client.

Instead of:

    n, err := conn.Read(buf[0:])
    if err != nil {
        checkError(err)
        return
    }

You can do

    n, err := conn.Read(buf[0:])
    if err != nil {
        fmt.Printf(err)
        conn.Close()
        return
    }

Which will print the error on the server side and will go back to listening for next connection / continue to operate.

Dima
  • 2,012
  • 2
  • 17
  • 23
0

This works for me using nc:

$ nc localhost 8484
Client connected from 127.0.0.1:46464
hello
hello
world
world
^D
Fatal error: EOF
exit status 1

So I think there's nothing wrong with your connection handling. You might just be sending data that the client isn't expecting, and it's closing the connection because of that.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Thanks for the reply. I get the error when the server is trying to read data that the client has sent. So the order of things is: 1. Client connects to server. 2. Client sends initial information to server e.g. client version. 3. Server reads the buffer. 4. Server responds (currently I am just echoing the buffer). It is currently failing at the read buffer stage "n, err := conn.Read(buf[0:])". Sorry if my original post was not clear. – John May 26 '14 at 18:42