Question

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...

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top