#TCP Server

16 messages · Page 1 of 1 (latest)

mighty flower
#

why are you using JSON here? it isn't totally clear to me

#

and to be clear: what bytes are actually being sent over the wire? are they TestUser or "TestUser" (with the quotation marks actually being sent as individual bytes)?

#

TestUser is not a valid JSON value. "TestUser" is. (and while it's totally legal to send a string as a JSON value—it doesn't necessarily have to be an object or array—it's not necessarily typical)

storm oracle
#

I think the issue might be with my client because I can fmt.PrintLn() at the start of the handler function and that prints out immediately.

mighty flower
#

(i ask mostly to confirm that you know that you need a self-terminating format if you're not using a length-describing format. JSON does provide that so it's actually positive in that way, but it's so inefficient at conveying arbitrary non-UTF-8 bytes that it usually isn't the best way to do such a thing in a network protocol.)

storm oracle
#

Actor replication would use udp so that would be just raw bytes.

#

I'm going to keep troubleshooting some more on the client

mighty flower
#

taking a network capture in the middle would be a great way to identify the problematic endpoint

#

either you see the bytes or you don't, at which point it should be clear where to look further

storm oracle
#

Yea, I didn't have json setup on the client. That appears to have been the issue (other than that I wasn't running the network update function inside the new game loop).

mighty flower
#

that's why i asked 😁 the decoder is just going to keep reading until it sees a terminating character

storm oracle
#
func recvLoop(r io.Reader) (string, error) {
    var inbuf [64]byte
    var result strings.Builder

    for {
        n, err := r.Read(inbuf[:])
        if err != nil {
            fmt.Println("Failed to receive msg from the server!")
            break
        }
        result.Write(inbuf[:n])
        fmt.Print(string(inbuf[:n]));
    }
    return result.String(), nil
}

func handleConnection(conn net.Conn) {
    defer conn.Close()
    fmt.Println("TEST!")

    var client Client
    client.Conn = conn

    // Create buffered reader and writer
    reader := bufio.NewReader(conn)
    //writer := bufio.NewWriter(conn)

    // Read username
    fmt.Println("Convert to str");
    data, err := recvLoop(reader)
    fmt.Println("Done recvLoop");
    if err != nil {
        fmt.Println("Error receiving data:", err)
        return
    }
    fmt.Println("data: ", data);
    // if err != nil {
    //     fmt.Println("Error reading username:", err)
    //     return
    // }
    client.Username = data // Remove newline character

    lobby.Mutex.Lock()
    lobby.Clients = append(lobby.Clients, &client)
    lobby.Mutex.Unlock()

    fmt.Println(client.Username, "joined the lobby")
#
Server started on port 64010
TEST!
Convert to str
"TestUser"
#

After closing the game

#
"TestUser"Failed to receive msg from the server!
Done recvLoop
data:  "TestUser"
"TestUser" joined the lobby
Error reading message: read tcp 127.0.0.1:64010->127.0.0.1:50228: wsarecv: An established connection was aborted by the software in your host machine.
#

Actually, I think I know what's going on