Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Actually i have created a server using tcplistner and i am using the below code to get the data and paste it on the textbox but my client device sent me a data in garbage value.
i am using stream reader to read the data but i think it's not working perfectly.
Anyone have an idea about it please tell me.
Thanks in advance.

What I have tried:

I have tried this code to read the data from client device.
ns = new NetworkStream(socketforclient);
        StreamReader sr = new StreamReader(ns);

            string aa = sr.ReadLine();
            if (aa != "")
            {

                txtinfo.AppendText("Client:" + aa + Environment.NewLine+Environment.NewLine);
            }
            else
            {
                MessageBox.Show("Send a Message first", "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
Posted
Updated 16-Mar-21 1:24am
Comments
OriginalGriff 16-Mar-21 6:14am    
"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!
Use the "Improve question" widget to edit your question and provide better information.
Member 14743579 16-Mar-21 7:07am    
actually a client is a device,this device send me a binary data but my code shows me a garbage values like ??A@ and some other characters. how to read binary data from device and display the data in textbox.
Richard MacCutchan 16-Mar-21 7:22am    
You need to check with the client what format the data is in. Do not assume that it is just a stream of readable text.

There can be several issues with the code, starting with the encoding of the content. It also depends on what you are sending. In your code, you do not show what the client is sending, only what you are reading on the server-side. It might be that the client is sending a JPEG, and the server is reading a Markdown?

I wrote an article back in the day that showcases the basic C# based Tcp communication between a server and a client program. Please read that article to get a head start: Creating a server/client application using native .NET TCP library[^].
 
Share this answer
 
Comments
Member 14743579 16-Mar-21 7:10am    
Actually client is a mechanical device who send a continuous data in binary format and i am trying to receive this data in a readable format and display it on the textbox.
Quote:
actually a client is a device,this device send me a binary data but my code shows me a garbage values like ??A@ and some other characters. how to read binary data from device and display the data in textbox.

And then you do this:
C#
string aa = sr.ReadLine();
Which says "treat the incoming data as Unicode characters and garble it all up for me, please. If there are any lines in there, anyway."
If you are receiving binary data, then handle it as binary data:
C#
int bytesCount = sr.Length;
byte[] data = new byte[bytesCount];
int actuallyRead = sr.Read(data, 0, bytesCount);
Then process the data according to the documentation for the actual device.

You need to use the device documentation as there is no "standard" that applies across all devices.
 
Share this answer
 
Comments
Member 14743579 17-Mar-21 2:04am    
streamreader does not contain a definition for sr.Length and data cannot convert from byte[] to char[] in sr.read(data,0,bytescount)..
OriginalGriff 17-Mar-21 5:04am    
No, you're right - my bad.
It's a NetworkStream, so it doesn't support seeking, and the length isn't available - you'll have to assign a byte array big enough to hold a good block, read into that and check how many characters it returned.

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