简体   繁体   English

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

[英]sending ushort ulong byte data over tcpclient object

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 我在C#中的应用程序希望与第三方Tcp服务器一起发送数据并接收回响应消息...命令的语法具有UShort,ULONG,BYTE类型的数据,这是我的应用程序需要发送的示例命令是

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... 欢迎阅读书写数据方面的任何建议...

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. 我还建议使用数据包嗅探器(或在Linux上简单地使用strace)来显示正在读取和写入的数据包...您可能会在那里看到错误。 Then, use another program to interact with the server that does work, and compare bytes. 然后,使用另一个程序与可以正常工作的服务器进行交互,并比较字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM