how do i compare string formats?!

on the client side i send a string in this format:

Object xyData = xco.ToString("000") + "%" + yco.ToString("000") + "%";

byte[] xybyData = System.Text.Encoding.UTF8.GetBytes(xyData.ToString());


if (m_clientSocket != null)
{

m_clientSocket.Send(xybyData);
}
and on the server side i receive it like this:

if (szData.Contains("%"))
{
richTextRxMessage.Text = szData;

xyData = szData.Split('%');


x = Convert.ToInt32(xyData[0]);
y = Convert.ToInt32(xyData[1]);
}

the problem is somtimes another client send some other data containing something else.. and the server mix up the data,, the zdData will become like this szData = "\0\0134%\0"..
i want to compare the string format when i receive the data on the server..
Note.. the data buffer is 8.

any idea on how to do that ! or fix it
thnx



Answer this question

how do i compare string formats?!

  • John230873

    hi,

    i guess you don't need the second '%' flag so you can remove it as soon as you recieve this data like to  take the substring from index 0 to the last index of % and to split the string and to see the array length if it 1 or 2, then you can select the recieved data you want

    or at least you can count the charachter % in the recieved string and that will tell you which packet is that

    hope this helps


  • Bhupathi Venkatesh

    What type is "xco"

  • Sharma

    You haven't provided enough of the code to really make an accurate evaluation.

    What I think is going on is either you've got a difference of locale problem or an invalid encoding/decoding of text data (or both).

    How is szData created If you are not using UTF8Encoding.GetString(byte[]) you'll get strange errors because you're encoding in one format and decoding in another.

    I would suggest you add an InvariantCulture to your calls to formatting methods like ToString and ToInt32 when you're using text to store/transfer numerical data that isn't displayed to the user. For Example:

    Client

    int xco = 1, yco = 2;

    Object xyData = xco.ToString("000", CultureInfo.InvariantCulture)

    + "%" + yco.ToString("000", CultureInfo.InvariantCulture);

    byte[] xybyData = System.Text.Encoding.UTF8.GetBytes(xyData.ToString());

    Server

    String szData = System.Text.Encoding.UTF8.GetString(xybyData);

    String[] arr = szData.Split('%');

    int x = Convert.ToInt32(arr[0], CultureInfo.InvariantCulture);

    int y = Convert.ToInt32(arr[1], CultureInfo.InvariantCulture);

    By the way, you don't need the xyData.ToString() if you make xyData a String type instead of Object.



  • clkdiv

    xco and yco are "int" there values are assigned by the x and y coordinates of the mouse..


  • how do i compare string formats?!