Search code examples
tcpgoasyncsocket

Google Go EOF on reading from connection using Read()


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.


Solution

  • 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.