Compare strings objects

how to test to see if they have same text

Page 1 of 1

3 Replies - 484 Views - Last Post: 13 March 2010 - 08:35 AM Rate Topic: -----

#1 Guest_luke83*


Reputation:

Compare strings objects

Posted 13 March 2010 - 06:13 AM

Hello everyone,
I am very new to programming and i have encounted a problem, i have some text coming back from a TCp Client and i want to see if it the same as a predefined stament i have saved as a variable. If it matches i want to send it a string of text to a label on my form. If the first one fails i want to get it to check another predefined string and send a different message to my label on my form, if both fail i want a general error on my form.

Please help this is doing my head in!!!! i have tried acouple of differnt ways and i still cant get it to do what i want

heres what i have been playing around with :

                 string okay = "OKAY";
                 string notokay = "NOTOKAY";
                 mprstatus = data;  //data is the result from the tcp client
                 
                try
                {
                  if (mprstatus == okay )

                    {
                        
                        MessageBox.Show("yes");
                        LblMprStatus.Text = "file found"; //" File Found, Ready to feed into BOF";
                        client.Close();
                        server.Stop();
                        return;
                    }
                

                 else if(mprstatus == notokay)
                      {
                        
                        MessageBox.Show("No");
                        LblMprStatus.Text = "file not found"; //" File Found, Ready to feed into BOF";
                        client.Close();
                        server.Stop();
                        return;
                    }
                }
                catch// ((mprstatus != okay|| (mprstatus == notokay))
                {
                    LblMprStatus.Text = " ERROR recieving message";
                    client.Close();
                    server.Stop();
                    return;
                }

This post has been edited by JackOfAllTrades: 13 March 2010 - 06:28 AM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE...[/code]


Is This A Good Question/Topic? 0

Replies To: Compare strings objects

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: Compare strings objects

Posted 13 March 2010 - 06:31 AM

Quote

i still cant get it to do what i want

You haven't told us...what IS it doing? Have you considered stepping through it in the debugger? How about using
if (String.Compare(mprstatus, okay, true) == 0) 
{ 
    Console.WriteLine("worked");
}
else
{
    Console.WriteLine("failed");
}
?

Although we don't even know what mprstatus is as a data type.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_luke83*


Reputation:

Re: Compare strings objects

Posted 13 March 2010 - 08:06 AM

Sorry , this is the entire Method i have at the moment its alittle different from the last one as i have been trying to find ways around my problem to no luck. As you can see Jackifalltrades i tried your idea but it just keeps failing. Does it have something to do with it being return in a ascii format? Or could it be the free software im using to sendthe signal to my program , maybe i need to tell it to read only upto the "y" in ok , is "hello" different to "hello " im using the .compare ????
Also the 2 options im looking for are "upid;okay" and " upid;notokay" this is what the data object should have in it.
  
private void listeningaction()
        {
            
            //MessageBox.Show("listeningaction!");
    TcpListener server=null;
    try
    {


        // TcpListener server = new TcpListener(port);
        server = new TcpListener(localAddr, port);

        // Start listening for client requests.
        server.Start();
        //MessageBox.Show(" server start ");
        // Buffer for reading data
        Byte[] bytes = new Byte[256];
        String data = null;
        MessageBox.Show("waiting for connection");
        // Enter the listening loop.
        while(true)
        {

            //System.Console.Write("Waiting for a connection... ");

            // Perform a blocking call to accept requests.
            // You could also user server.AcceptSocket() here.
            TcpClient client = server.AcceptTcpClient();
            MessageBox.Show("Connected!");



            //  // Get a stream object for reading and writing
            NetworkStream stream = client.GetStream();

            int i;

            //  // Loop to receive all the data sent by the client.
            if ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                //MessageBox.Show(" second loop ");
                // Translate data bytes to a ASCII string.
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                //System.Console.WriteLine("Received: {0}", data);
                //MessageBox.Show("Received: {0}", data);

                // Process the data sent by the client.
                data = data.ToUpper();

                byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);


                string okay = "OKAY";
               // string notokay = "NOTOKAY";
                mprstatus = data;
                

                LblMprStatus.Text = mprstatus ;
                if (string.Compare(mprstatus, okay, true) == 0)
                {
                    MessageBox.Show("work bitch");
                }

                else
                {
                    MessageBox.Show("didnt work");
                }


                client.Close();
                server.Stop();
                return;

                  
            }

            // Shutdown and end connection
            client.Close();
            return;
        }
    }

    catch (SocketException e)
    {
        System.Console.WriteLine("SocketException: {0}", e);
    }
   
 

    //System.Console.WriteLine("\nHit enter to continue...");
    //System.Console.Read();
  }


Was This Post Helpful? 0

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: Compare strings objects

Posted 13 March 2010 - 08:35 AM

What about something like this? Off the cuff, may not even compile.
int bytesRead =	0;
StringBuilder sb = new StringBuilder();
while ((bytesRead = stream.Read(bytes, 0, bytes.Length)) != 0)
{
    sb.Append(System.Text.Encoding.ASCII.GetString(bytes).ToUpper());
}

var data = sb.ToString();
if (data.StartsWith("OKAY"))
    MessageBox.Show("Succeeded");
else
    MessageBox.Show("Failed; received: " + data);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1