0

I have a simple UDP server that works - I want to enter the data I'm getting to MySql.

When I setup the UDP server "enter" that data as a byte - then how do I convert it to string , and then cut it using delimiter ?

when i look at the CMD window - everything is working as it should, so the server is OK my problem is converting it to string so I can "cut" when I want - after "!

the message is Ok and I get it as I should ,

        char delimiter = '!';
        String CutData;

        byte[] data = new byte[1024];

        while (true)
        {
            data = newsock.Receive(ref sender);

            CutData = data.ToString();
            String[] ToDb = CutData.Split(delimiter);


            Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));

        }

Thanks,

Korenron
  • 101
  • 2
  • 10
  • What is used to terminate each sample on the TCP connection. Is there a return or some other character? – jdweng Jun 21 '18 at 11:07
  • Create a [mcve]. If your question is about UDP, the splitting part is irrelevant. If it's about splitting a string, the UDP part is irrelevant. [Edit] your question to remove the irrelevant parts and show what you have tried. – CodeCaster Jun 21 '18 at 11:08
  • @jdweng - which TCP? I;m only using UDP - and no "end" char, just new line every time – Korenron Jun 21 '18 at 11:09
  • 1
    **SQL Injection alert** before answering the main question, I would suggest to use parameters when passing values to your server [prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) – Kaj Jun 21 '18 at 11:10
  • @CodeCaster - the reason I wrote everything is so it will be easy to see what I want to do , to see the "big picture" - because maybe I did something wrong in the server part.... – Korenron Jun 21 '18 at 11:10
  • 1
    _"maybe I did something wrong in the server part"_ - my point exactly. Troubleshoot that part first. Inspect the string you receive. Does that contain what you expect it to? Then the UDP part is irrelevant, and you can create a new example using that string and without the UDP part. – CodeCaster Jun 21 '18 at 11:18
  • @CodeCaster - I have minimize it as you ask - now you can help me find the problem ? :-) – Korenron Jun 21 '18 at 11:59
  • @Kaj - I don;t think the sql injection is the problem , when I look at the CutData I don;t see nothing.... – Korenron Jun 21 '18 at 12:11
  • @Korenron I didn't say the SQL injection is your problem. I said before answering your question, consider using parameters. – Kaj Jun 21 '18 at 12:12

1 Answers1

2

Your problem is you are trying to convert byte array to string by calling ToString();

And actually the answer is in your code. You have first get the string from the byte from this line :

Encoding.ASCII.GetString(data, 0, data.Length));

so assign it to a string variable like :

CutData = Encoding.ASCII.GetString(data, 0, data.Length));
// CutData = Encoding.ASCII.GetString(data); I think this works also

then you'll be able to see the string correctly, so you can split it.

String[] ToDb = CutData.Split(delimiter);

then just to see the results : print it out.

foreach(string part in ToDb)
{
   Console.WriteLine(part);
}
Kaj
  • 806
  • 6
  • 16
  • Yap this is what I have miss...(found it in another hidding in another post here) string result = System.Text.Encoding.UTF8.GetString(byteArray); now it's working - Thanks , – Korenron Jun 21 '18 at 12:34
  • And what helped me to get the problem is reducing the code to Minimal :) Happy coding – Kaj Jun 21 '18 at 12:35