套接字编程:在golang客户端或Java服务器中是否添加了额外的’\n’?

huangapple go评论87阅读模式
英文:

Socket programming: Is extra '\n' added in the golang client or the Java server?

问题

我已经写了一个Java TCP套接字服务,这个服务被一个Golang客户端使用。

当服务器套接字响应在Golang端解析时,情况变得奇怪。

具体来说,这是Java服务器端的代码:

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output));
bw.append('Y');        
bw.append('E');
bw.append('S');
bw.append('\n');
bw.flush();

以及这是Golang客户端的代码:

extendTimoutFor(client.conn)
	rspMsg, fault := bufio.NewReader(client.conn).ReadString('\n')
	if fault != nil {
		return UNKNOWN, fmt.Errorf("Ers reading response: %v", fault)
	}

	retStr := strings.TrimRight(rspMsg, "\n")

	if retStr == YES {
		return YES, nil
	}

	if retStr == NO {
		return NO, nil
	}

	if retStr == FAIL {
		return FAIL, nil
	}

	return UNKNOWN, fmt.Errorf("Ers parsing msg [%s]: %v", rspMsg, fault)

在客户端产生以下结果:

[UNDO|test|]: Ers parsing msg [YES
]: <nil>

我对这个结果的问题是客户端无法将retStr识别为有效的响应。由于rspMsg是我们期望的(即'YES\n'),那么如果我们在UNKNOWN块中,这意味着在这一行出现了问题:retStr := strings.TrimRight(rspMsg, "\n")

我的具体问题是:

  1. 由于存在nil错误,'\n'和(其后的任何字符)不应该被剥离,留下"YES"吗?
  2. 我是否在Java端遗漏了一些怪癖?我已经使用Java重新创建了客户端,输出结果是我期望的。
英文:

I've written a Java TCP socket service and this service is used by a golang client.

Things get weird when the server socket response gets parsed on the golang side.

Specifically, this Java server code:

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output));
bw.append(&#39;Y&#39;);        
bw.append(&#39;E&#39;);
bw.append(&#39;S&#39;);
bw.append(&#39;\n&#39;);
bw.flush();

And this golang client code:

extendTimoutFor(client.conn)
	rspMsg, fault := bufio.NewReader(client.conn).ReadString(&#39;\n&#39;)
	if fault != nil {
		return UNKNOWN, fmt.Errorf(&quot;Ers reading response: %v&quot;, fault)
	}

	retStr := strings.TrimRight(rspMsg, &quot;\n&quot;)

	if retStr == YES {
		return YES, nil
	}

	if retStr == NO {
		return NO, nil
	}

	if retStr == FAIL {
		return FAIL, nil
	}

	return UNKNOWN, fmt.Errorf(&quot;Ers parsing msg [%s]: %v&quot;, rspMsg, fault)

Produce this result in the client:

[UNDO|test|]: Ers parsing msg [YES
]: &lt;nil&gt;

My problem with the result is that the client doesn't recognize retStr as a valid response. Since rspMsg is what we expect (i.e. 'YES\n') then if we're in the UNKNOWN block it means that something went wrong during this line: retStr := strings.TrimRight(rspMsg, "\n")?

My specific questions are:

  1. Since there are nil errors shouldn't '\n' and (any characters following it) have been stripped leaving us with "YES"?
  2. Am I missing some quirk on the Java side? I've recreated the client using Java and the output is what I expect.

答案1

得分: 0

  1. 由于没有错误,所以不应该将'\n'和(其后的任何字符)删除,只留下"YES",对吗?
    是的

  2. 我是否在Java方面遗漏了一些怪癖?我使用Java重新创建了客户端,输出结果符合我的预期。
    是的

这个问题通过以下几个步骤解决:

  • 使用java.io.PrintWriter在输出中引入了一些意外字符。
  • strings.TrimRight(rspMsg, "\n")掩盖了上述问题,但在安装Golang并调试脚本后确认了这一点(使用strings.TrimSpace对上述操作是一种更健壮的方法)。
  • 切换到java.io.OutputStream.write()简化了发送给客户端的输出。
英文:
  1. Since there are nil errors shouldn't '\n' and (any characters following it) have been stripped leaving us with "YES"?
    YES

  2. Am I missing some quirk on the Java side? I've recreated the client using Java and the output is what I expect.
    YES

This was resolved with a few steps:

  • Using java.io.PrintWriter introduced some unexpected characters throughout the output.
  • strings.TrimRight(rspMsg, "\n") masked the point above but it was confirmed after installing Golang and debugging the script (using strings.TrimSpace for the operation above is a more robust approach)
  • Switching to java.io.OutputStream.write() simplified the output sent to the client

huangapple
  • 本文由 发表于 2017年3月14日 21:29:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/42787374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定