drozwmi5440 2016-03-31 20:55
浏览 107
已采纳

在打开的TCP套接字上使用JSON回复

I have a server that successfully opens a connection with a second server. The second server performs an action and I am trying to get it to reply to the first server with a JSON on the same connection.

package main

import (
    "fmt"
    "net"
    "encoding/json"
)

type NewContainerJSON struct {
    Action string           `json:"Action"`
    ContainerName string    `json:"ContainerName"`
    BaseServer string       `json:"BaseServer"`
    CMS string              `json:"CMS"`
    WebsiteName string      `json:"WebsiteName"`
    DBrootPWD string        `json:"DBrootPWD"`
    DBadminUname string     `json:"DBadminUname"`
    DBadminPWD string       `json:"DBadminPWD"`
}

func main() {

    service := "127.0.0.1:8081"
    tcpAddr, err := net.ResolveTCPAddr("tcp", service)
    checkError(err)

    listener, err := net.ListenTCP("tcp", tcpAddr)
    checkError(err)

    conn, err := listener.Accept()
    checkError(err)

    decoder := json.NewDecoder(conn)
    encoder := json.NewEncoder(conn)

    var b NewContainerJSON
    err = decoder.Decode(&b)
    checkError(err)
    fmt.Println(b.Action)

    if b.Action == "createNew" {
        fmt.Println("This works")
        resp := []byte("And here's our repomse")
        conn.Write(resp)

        c := NewContainerJSON {
            Action: "createdNewContainer",
            ContainerName: "Test",
            BaseServer: "Test",
            CMS: "Test",
            WebsiteName: "Test",
            DBrootPWD: "Test",
            DBadminUname: "Test",
            DBadminPWD: "Test",
        }

        encoder := json.NewEncoder(conn)
        if err := encoder.Encode(c); err != nil {
            fmt.Println("encode.Encode error: ", err)
        }

        conn.Write(c)
    }
}

func checkError(err error) {
    if err != nil {
        fmt.Println("An error occurred: ", err.Error())

    }
}

I get following error on the line conn.Write(c)

cannot use c (type NewContainerJSON) as type []byte in argument to conn.Write

Two questions:

1: What exactly is this error saying? It seems to be complaining that 'c' cannot be used as a Byte when using the conn.Write function but shouldn't the json.Encoder convert the JSON to a format the conn.Write can use?

2: How exactly can I return a JSON back to the first server using the open connection?

  • 写回答

2条回答 默认 最新

  • dsbifvcxr458755493 2016-03-31 21:04
    关注

    The encoder writes the JSON encoding of c to conn on this line:

    if err := encoder.Encode(c); err != nil {
    

    That's all you need to do. Delete the call to conn.Write(c).

    The error message is telling you that the value of c cannot be used as the argument to Write because of a type mismatch. A NewContainerJSON is not a []byte.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据