<p>I have a sample code in C#. It uses Core.Streaming to do the stuff. I have tried using DialTCP() from 'net' but no luck. </p>
<p>Here's C# sample code: </p>
<pre><code>var client = new StreamingClient();
client.CompressionType = PacketCompressionType.Gzip;
client.EncodingType = PacketEncodingType.Json;
client.PacketReceived += new
StreamingClient.PacketReceivedHandler(client_PacketReceived);
try {
var auth = client.Connect("export.kt01.net", 11011, "username", "password");
Console.WriteLine("Connected (" + auth.queueLength + " items in queue)...");
while (client.Connected) {
Thread.Sleep(100);
if (Console.KeyAvailable) {
terminated = true;
Console.Write("Stopping...");
break;
}
}
} catch (Exception ex) {
Console.WriteLine("[ERROR] " + ex.Message);
Thread.Sleep(1000); // wait a second before retrying
} finally {
client.Disconnect();
Console.WriteLine("Disconnected");
}
</code></pre>
<hr/>**评论:**<br/><br/>neondirt: <pre><p>Maybe try posting your Go code instead?
Also, explain why it doesn't work (as you expected) and any errors you receive.</p></pre>jhadi: <pre><pre><code>func main() {
user := &User{Username: "dummy",Password: "dummy"}
jsondata, err := json.Marshal(user)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(jsondata))
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s host:port ", os.Args[0])
os.Exit(1)
}
service := os.Args[1]
tcpAddr, err := net.ResolveTCPAddr("tcp", service)
checkError(err)
conn, err := net.DialTCP("tcp", nil, tcpAddr)
checkError(err)
_, err = conn.Write([]byte(jsondata))
checkError(err)
result, err := ioutil.ReadAll(conn)
checkError(err)
fmt.Println(string(result))
os.Exit(0)
</code></pre>
<p>}</p>
<p>I have to send username and password in json format, after that server will send me auth response.
Currently I just receive "connection reset by peer" error. </p></pre>jerf: <pre><blockquote>
<p>I have to send username and password in json format, after that server will send me auth response. Currently I just receive "connection reset by peer" error. </p>
</blockquote>
<p>Mmmm, that's tricky to get right. Are you responsible for the server on the other end or speaking to somebody else's server?</p>
<p>JSON doesn't have an end-of-JSON delimiter. A streaming parser can notice when the stream has ended, but it is very easy to imagine that the server is not using a streaming parser and is instead looking for some delimiter to indicate that the JSON is done, like a newline or something. The difference could be something as simple as your C# happens to incidentally terminate the JSON with a CRLF, which the server was looking for as the sign to send the block of text to the parser, and the Go code isn't emitting it.</p>
<p>If you do control both sides of the connection, I recommend delimiting the connection with something, for better reliability and debugging. Something as simple as "4 bytes in network order indicating the length of the JSON to come" is enough to solve that problem.</p>
<p>If that is not the problem, you may want to wrap the conn with something that prints all the bytes going in and out, so you can see exactly what's going on.</p></pre>jhadi: <pre><p>Unfortunately, I'm not responsible for the server on the other end. I'll try printing all the bytes though. Thanks</p></pre>: <pre><p>[deleted]</p></pre>jerf: <pre><p><a href="/u/jhadi" rel="nofollow">/u/jhadi</a>: This is a good suggestion, and what I'm about to say is not disagreement with paul2048, but elaboration.</p>
<p>Telnet doesn't work like a lot of people expect. By default it buffers entire lines before sending them over, which means you can telnet to a port, and type in the whole line of JSON, and backspace and such, and the server won't get the backspaces (which it won't know what to do with), it just gets the line.</p>
<p>But when it gets that line, it will also have the \n in it from when you pushed enter, so bear that in mind; it may still be a difference between what you do and what your code does, even if you copy and paste the exact same JSON line into the telnet client.</p>
<p>(Many people don't realize this is Telnet's default behavior because when you telnet to a shell, the shell immediately puts the Telnet connection in "raw" mode, which sends all keystrokes instantly. The Telnet protocol, which exists and is a thing, has a command for that. This is also why telnet is such a good default client for line-based connections like HTTP; you still can edit a line before sending it along, since there aren't very many, if any, protocols that will accept embedded backspace characters and process them. Which is a good thing; they all would have broken in the Unicode era.)</p></pre>neondirt: <pre><p>I guess that seems fine. Since you're getting "reset by peer", have you verified that you're sending the correct message? Tried writing the json data to stdout?</p></pre>jhadi: <pre><p>I have made a struct for user:</p>
<pre><code>type User struct {
Username string `json:"username"`
Password string `json:"password"`
}
</code></pre>
<p>and then I initialize it using username and password: </p>
<pre><code>user := &User{Username: "dummy",Password: "dummy"}
</code></pre>
<p>after that I convert it to json: </p>
<pre><code>jsondata, err := json.Marshal(user)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(jsondata))
</code></pre>
<p>once Println executes I get:</p>
<pre><code>{"username": "dummy", "password": "dummy"}
</code></pre>
<p>However the I'm required it to send in this format: </p>
<pre><code>{
"username": "dummy",
"password": "password"
}
</code></pre>
<p>I thought skipping newlines might not be the issue. But I'll try this way too. </p></pre>neondirt: <pre><p>But have you compared it to what the C# code sends?
White-space sensitivity isn't unheard of, unfortunately...</p></pre>Morgahl: <pre><p>Make sure you are handling that gzip data on the go end. Given the code you've shown this is not the case.</p></pre>nsd433: <pre><p>You can try comparing packet captures of the working C# and the non-working Go clients in wireshark or similar tools that can follow a TCP connection (that makes it easier than reassembling the stream from raw packets, though it does hide where the Push flags happened)</p></pre>Defender90: <pre><p>This may not help you but I had to implement a client for a wacky 1980's binary protocol that was built for serial lines and ported by the manufacturer to TCP/IP. Maybe you can use this as a guide:</p>
<p><a href="https://github.com/chrissnell/gopherwx/blob/master/station.go" rel="nofollow">https://github.com/chrissnell/gopherwx/blob/master/station.go</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传