Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ALL,

i write a TCP client which able to communicate with Server but each time i am getting different-different string .
RT832
RT
RT8
RT832
R

but on hyperterminal i am always getting proper string like
RT832
RT832
RT832
RT832

below my code:
C#
private static void Main(string[] args)
       {
           TcpClient ourMagicClient = new TcpClient();

           //Connect to the server - change this IP address to match your server's IP!
           ourMagicClient.Connect("192.123.0.254", 7000);

           //Use a NetworkStream object to send and/or receive some data
           NetworkStream ns = ourMagicClient.GetStream();

           byte[] fromClient = new byte[10025];
           byte[] toClient;
           while (ns.Read(fromClient, 0, (int) ourMagicClient.ReceiveBufferSize) > 0)
           {
               int nub = ns.Read(fromClient, 0, (int)ourMagicClient.ReceiveBufferSize);
               string str = Encoding.ASCII.GetString(fromClient, 0, nub);
               Console.Write(str);
               toClient = fromClient;
              ns.Write(toClient, 0, toClient.Length);
              ns.Flush();
           }
           Console.ReadKey();
       }

Thanks in advanced.
jmd :-)
Posted
Updated 26-Mar-13 1:10am
v2
Comments
Oleksandr Kulchytskyi 26-Mar-13 4:12am    
Wow, i'm a bit wondering with your implementation...
Do you know, that after first read operation was perormed you stream will seek to the value of read bytes, as a result stream position will be = number of read bytes??
giri001 26-Mar-13 4:27am    
Hi Oleksandr,thanks for your comment but when i fixed with constant value then again i am not able to get anything.:-(
int nub = ns.Read(fromClient, 0, 5);//5 is string length.
so how can i fix this...:-(
Richard MacCutchan 26-Mar-13 6:51am    
Why do you do a second read immediately after the first? You should check the documentation for correct implementation of reading from a network stream.
giri001 26-Mar-13 7:09am    
Sure Richard ..i will do.Thanks.
ZurdoDev 26-Mar-13 7:15am    
It appears you are adding a comment instead of replying to the users comments. They will not be notified unless you reply to their comments.

You receive random results because you programmed some protocol to get random results, more exactly, the same data at random boundaries. If you used not ASCII but some binary data with different data types (or even some Unicode UTF with different character size, such as UTF-8), you would get different or invalid content. Right now you get recognizable characters only because your boundary between data elements is 1 byte.

The whole approach is wrong. You should not read all you have in the buffer, or, if you really want it, you should wait until you get enough characters (but you don't really need it). It all depends on what is going on on the other end of your RS-232 cable, but normally you would need to define some application-level protocol where you exchange the chunks of data of known size, or the size values transmitted through the protocol before data itself.

If you cannot change the behavior of the device sending your the data, and you should read everything, you have two options: 1)
read exactly what's expected; if the other side is not ready; your thread will get to wait state until data arrives to the buffer; this would be the reason to use a separate thread for communication, which is always a good idea; in this case, you also should define some conditions for the end of all the reading loop; 2) you can read the buffer content periodically, but then you would need to save the data and "parse" is after you have the copy of the buffer of sufficient size; for example, if you expect chunks of string 5 characters each, do it later, when you have one or more chunks of 5 characters. This paragraph looks somewhat uncertain, but only because you did not explain your protocol as it is supposed to be. Hope you can get a right idea.

—SA
 
Share this answer
 
Comments
giri001 26-Mar-13 16:45pm    
i will try ......thanks for deep explanation.:-)
Sergey Alexandrovich Kryukov 26-Mar-13 17:00pm    
You are welcome, as well as your follow-up questions, if you have any.
—SA
The implementation you have to read and write strings is a bit unnecessary. When I do this sort of thing, I attach both a StreamReader and a StreamWriter to the NetworkStream.

Your stream reader has a handy ReadLine() method, and the writer has a WriteLine(), so you should need to work at the buffer/encoding level, but just with strings.
 
Share this answer
 
Comments
giri001 26-Mar-13 16:51pm    
thnq Rob.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900