0

I am writing a c# program which will recieve a message from a server, the problem it recieves the byte from the message but does not read the byte. It instead reads "recieved command = system.byte []. Below is the client code

   TcpClient tcpclnt = new TcpClient();
                 
   tcpclnt.Connect(RecieveIP.Text, 8001); // use the ipaddress as in the server program
   MessageBox.Show("Connected");

   Console.Write("Enter the string to be transmitted : ");
   Stream stem = tcpclnt.GetStream();
   MessageBox.Show("Listening for information......");
   byte[] bb = new byte[100];
   int k = stm.Read(bb, 0, 100);

   for (int i = 0; i < k; i++)
        Console.Write(Convert.ToString(bb));

   string toattk = bb.ToString();

   MessageBox.Show("Recieved Command = " + toattk);

Sorry for the incorrect code formatting, am typing from a phone.

But i am wondering why it would be showing this instead of the decoded message? I can add the server code if necessary. The computers do successfully connect.

Edit:

So the edits worked, thank you! But I am trying to code so that a certain code will execute if the recieved message equals "g". Despite that the strings match, the if then statement does not execute. I am wondering why that might be? Here is the code? Thanks.

byte[] bb = new byte[100];

            int k = stm.Read(bb, 0, 100);



            for (int i = 0; i < k; i++)

                Console.Write(Convert.ToString(bb));



          



            string atk = Encoding.ASCII.GetString(bb);

            MessageBox.Show("Recieved Command " + atk);



            if (atk == "g")

            {

                MessageBox.Show("information received.");

                search.RunWorkerAsync();

            }
Theoneguy
  • 13
  • 4
  • Is there some particular reason you have to use a raw stream? There are many protocols and libraries on top of tcp that provide a higher level abstraction and are easier to use. – JonasH Apr 18 '22 at 17:14
  • So that the server can simply send text that is entered in a winform box. – Theoneguy Apr 18 '22 at 17:17
  • 2
    You can't use .ToString() directly on a byte array to get its string representation as you've already demonstrated. There's no way for the runtime to know how to translate the bytes to a string. This SO post shows how you could do the conversion: https://stackoverflow.com/questions/11654562/how-to-convert-byte-array-to-string – emagers Apr 18 '22 at 17:22
  • Does this answer your question? [How to convert byte array to string](https://stackoverflow.com/questions/11654562/how-to-convert-byte-array-to-string) – emagers Apr 18 '22 at 17:22
  • If you are using an appropriate protocol you can send strings, or even arbitrary serializable objects. If you can make do with a request-response, then a simple webserver, or perhaps gRPC would work. For arbitrary direction of communication you could use a message buss, like [NetMQ](https://netmq.readthedocs.io/en/latest/message/). No need to deal with bytes directly. – JonasH Apr 18 '22 at 17:24
  • One more question, I am using ipv4 addresses for chatting with one specific computer. But if I want to listen for any computers on the network, I could code to listen only on the port but not for a specific ip address? Thanks. – Theoneguy Apr 18 '22 at 18:19
  • @Theoneguy Not with a TcpClient. That can only create a point-to-point connection to one particular server. You can use UDP to broadcast messages to many systems (and listen from many), but that requires additional complexity, because you have to care about packeting the data correctly yourself. – PMF Apr 18 '22 at 18:44
  • Well, so the code worked fine and the message was decided. Thanks! But what I am trying to do is have certain code execute when a certain character is recieved. The example is listed above under the "edited" code, if the character recieved is "g" the certain code on the client runs. though the strings match, the code for receiving "g" does not run. Why might this be? Thank you. – Theoneguy Apr 19 '22 at 15:12

1 Answers1

0

What you're basically doing is calling ToString() on a byte array. Since that has no specific overload of ToString, it will fall back to the default Object.ToString() implementation, which returns the name of the type. To convert a byte array to a string, decode it with an encoder, e.g:

string data = Encoding.ASCII.GetString(bb);
Console.WriteLine(data);
PMF
  • 14,535
  • 3
  • 23
  • 49