locked
TCP / Client Sockets C# RRS feed

  • Question

  • Good night i have seen tutoriales to understand this issue I see it very complicated I am something new in this please be patient, the problem I have is that I want to obtain a number from server though this

    string respuesta = System.Text.Encoding.ASCII.GetString(TiraIn);

    But when I try to get the number to make a button visible, I can not do it, can you please help me, this is my code.

                NetworkStream serverStream = clientSocket.GetStream();
                // byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from Client$");
                byte[] TiraOut = System.Text.Encoding.ASCII.GetBytes(txtServidor.Text + "$");
                serverStream.Write(TiraOut, 0, TiraOut.Length);
                serverStream.Flush();
    
                byte[] TiraIn = new byte[10025];
                // serverStream.Read(TiraIn, 0, (int)clientSocket.ReceiveBufferSize);
                serverStream.Read(TiraIn, 0, 10024);
                string respuesta = System.Text.Encoding.ASCII.GetString(TiraIn);
                msg("Resp.Server : " + respuesta);
    
                if (respuesta == "2")
                {
                    button1.Visible = true;
                }


    Sunday, November 29, 2020 6:34 AM

Answers

  • Maybe respuesta contains a different text. Did you check the value of this variable? For example, add MessageBox.Show(respuesta) before the loop and see the message box. (Or use the Debugger, breakpoints, and Watch window).

    • Marked as answer by dianas28 Tuesday, December 1, 2020 4:53 AM
    Sunday, November 29, 2020 7:18 PM

All replies

  • Maybe you should write ‘if (respuesta == "2$")’?

    Check the value of respuesta using Debugger or message box and show some details. Also show the contents of TiraIn.

    Sunday, November 29, 2020 8:37 AM
  • I am doing a for to go through the response string, but it still can not find it, can you help me please

                for(int i = 0; i < respuesta.Length; i++)
                {
                    if (respuesta[i].Equals("2"))
                    {
                        button1.Visible = true;
                        break;
                    } else
                    {
                        continue;
                    }
                }

    Sunday, November 29, 2020 6:59 PM
  • Maybe respuesta contains a different text. Did you check the value of this variable? For example, add MessageBox.Show(respuesta) before the loop and see the message box. (Or use the Debugger, breakpoints, and Watch window).

    • Marked as answer by dianas28 Tuesday, December 1, 2020 4:53 AM
    Sunday, November 29, 2020 7:18 PM