通过 tcpclient 对象发送 ushort ulong 字节数据

发布于 2024-09-18 05:55:24 字数 1215 浏览 12 评论 0原文

我在 C# 中的应用程序想要与第 3 方 Tcp 服务器通信来发送数据并接收返回响应消息...命令的语法有 UShort、ULONG、BYTE 类型数据 然后,我的应用程序需要发送的示例命令

USHORT 0xFFFF
USHORT 0x00D0
BYTE   0xDD

在应用程序中,我将数据发送为

  TcpClient tcpClient = new TcpClient();
       tcpClient.Connect("XX.XX.XX.XX",portnumber);

       Networkstream ns=tcpClient.GetStream();
       StreamWriter sw=new StreamWriter(ns);
       sw.Write(0xFFFF);
       sw.Write(0x00DD);
       sw.Write(0x00);

//或向它们发送字节

     sw.Write(0xFF);
       sw.Write(0xFF);
       sw.Write(0x00);
       sw.Write(0xD0);
       sw.Write(0x00);
       sw.Write(0x00);

,并且我通过服务器读取传入消息,

  while (true)
               {

                    byte[] buff=new byte[tcpClient.ReceiveBufferSize];
                    ns.Read(buff, 0, tcpClient.ReceiveBufferSize);
                 string dv= BitConverter.ToString(buff));
                }
//returned data looks like FF-A2-00-23-00-02-00-00-00-00-00-00-D9-2E-20-2E-00-A0-04-00-AE-08
//yes i know this byte syntaxes but returning data is not that i look response for command that i sent..

但返回值不是我想要的 我的代码向服务器发送数据有什么问题吗? 欢迎任何有关阅读写作数据的建议......

My application in c# wants to cominicate with 3rd party Tcp server to send data and recieve back response messages ...The syntax of commands has UShort,ULONG,BYTE type datas
a sample command that needed to send by my app is

USHORT 0xFFFF
USHORT 0x00D0
BYTE   0xDD

then in app i send data as

  TcpClient tcpClient = new TcpClient();
       tcpClient.Connect("XX.XX.XX.XX",portnumber);

       Networkstream ns=tcpClient.GetStream();
       StreamWriter sw=new StreamWriter(ns);
       sw.Write(0xFFFF);
       sw.Write(0x00DD);
       sw.Write(0x00);

//or send them bytes

     sw.Write(0xFF);
       sw.Write(0xFF);
       sw.Write(0x00);
       sw.Write(0xD0);
       sw.Write(0x00);
       sw.Write(0x00);

and I read incoming messages over server as

  while (true)
               {

                    byte[] buff=new byte[tcpClient.ReceiveBufferSize];
                    ns.Read(buff, 0, tcpClient.ReceiveBufferSize);
                 string dv= BitConverter.ToString(buff));
                }
//returned data looks like FF-A2-00-23-00-02-00-00-00-00-00-00-D9-2E-20-2E-00-A0-04-00-AE-08
//yes i know this byte syntaxes but returning data is not that i look response for command that i sent..

but returning values are not that i look for
Is there any wrong on my code with sending data to server??
and any recomendations on reading writing datas are welcome...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2024-09-25 05:55:24

当他们不知道所使用的协议时,没有人可以告诉您响应有什么问题。服务器发送该消息是因为它感觉...它可能是您的请求有问题,或者它可能是一条消息,表明它已离线以获取服务。您只能检查它关于如何解释它发送的结果的规范,或者询问维护它的人。

用您正在使用的语言标记这个问题也可能是个好主意,这样人们就可以理解函数调用以及您是否正确调用了它们。

我还建议使用数据包嗅探器(或在 Linux 上简单地使用 strace)来显示正在读取和写入的数据包......您可能会看到那里的错误。然后,使用另一个程序与正常工作的服务器交互,并比较字节。

Nobody can tell you what's wrong with the response when they don't know the protocol employed. The server's sending that because it feels like it... it might be something wrong with your request, or it might be a message indicating that it's offline for service. You can only check it's specification on how to interpret the result it did send, or ask the people who maintain it.

Might also be a good idea to tag this question with the language you're using, so people can make sense of the function calls and whether you're invoking them properly.

I'd also recommend using a packet sniffer (or on Linux simply strace) to show the packets being read and written... you will probably see the mistakes there. Then, use another program to interact with the server that does work, and compare bytes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文