dqwd71332
2019-06-21 05:41
浏览 87
已采纳

指示Go TCP Server Java TCP Client已完成写入流

I'm writing a Java TCP Client that connects to a Golang TCP Server.

The server uses the below code to read messages from clients:

func (tcpHandler TCPHandler) getClientMsgBytes(connection *net.TCPConn) ([]byte, error) {
    clientMsgBytes, err := ioutil.ReadAll(connection)
    if err != nil {            
        return nil, err
    }       

    return clientMsgBytes, nil
}

My client uses the below code to send messages to the server:

try (Socket socket = new Socket("localhost", 9000)) {
    byte[] message = getMessage();

    DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

    outputStream.write(commandMessage);     

    // Read message from the sever...          
}

My problem is that the server keeps waiting for the client to write its message even after all bytes of the message have been written to the stream. This seems to be because the ioutil.ReadAll function is waiting for an io.EOF error as the signal to stop reading from the stream.

How can I tell Go that I am done writing to the TCP stream from Java? I can change both the Java TCP Client and Golang TCP Server codes, if that helps.

NOTE: The server was written like that because our Go TCP Client uses the below code:

func writeToConnection(connection *net.TCPConn, tcpCommand structs.TCPCommand) error {
    messageBytes, err := json.Marshal(tcpCommand)
    if err != nil {
        err = merry.Wrap(err)
        return err
    }

    _, err = connection.Write(messageBytes)
    if err != nil {
        err = merry.Wrap(err)
        return err
    }

    err = connection.CloseWrite()
    if err != nil {
        err = merry.Wrap(err)
        return err
    }

    return nil
}

图片转代码服务由CSDN问答提供 功能建议

我正在编写一个连接到Golang TCP Server的Java TCP Client。

服务器使用以下代码从客户端读取消息:

  func(tcpHandler TCPHandler)getClientMsgBytes(connection * net.TCPConn)([] byte,error){  
 clientMsgBytes,错误:= ioutil.ReadAll(连接)
如果错误!= nil {
 return nil,err 
} 
 
返回clientMsgBytes,nil 
} 
   
 
 

我的客户端使用以下代码将消息发送到服务器:

  try(套接字套接字= new Socket(“ localhost”,9000))  {
 byte [] message = getMessage(); 
 
 DataOutputStream outputStream =新的DataOutputStream(socket.getOutputStream()); 
 
 outputStream.write(commandMessage);  
 
 //从服务器读取消息... 
} 
   
 
 

我的问题是服务器一直在等待客户端写入消息,甚至 在消息的所有字节都已写入流之后。 这似乎是因为 ioutil.ReadAll 函数正在等待 io.EOF 错误作为停止从流中读取的信号。

如何告诉Go我已经完成了从Java写入TCP流的准备? 如果有帮助,我可以更改Java TCP Client和Golang TCP Server的代码。

注意:服务器之所以这样编写,是因为我们的Go TCP Client使用 下面的代码:

  func writeToConnection(connection * net.TCPConn,tcpCommand structs.TCPCommand)error {
 messageBytes,err:= json.Marshal(tcpCommand)
如果 err!= nil {
 err = merry.Wrap(err)
返回err 
} 
 
 _,err = connection.Write(messageBytes)
如果err!= nil {
 err = merry。  Wrap(err)
返回err 
} 
 
 err =连接。CloseWrite()
如果err!= nil {
 err = merry.Wrap(err)
返回err 
} 
 \  n返回nil 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushen1026 2019-06-21 06:34
    已采纳

    Call shutdownOputput to match the code in the Go client.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题