Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with getting string from bytes

Problem with getting string from bytes

Scheduled Pinned Locked Moved C#
helpcsharpsysadminquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nc3b
    wrote on last edited by
    #1

    Ok here is the problem. I am developing a server in C and a client in C#. The server send streams of data to the client, and the client is responsible for decoding and interpreting these stream. So, if I send a simple "Hello world" from the server to the client, it works. If I send a byte representing an integer (I use memcpy) I can decode it in the client. But, the problems start when after some integers, I also put a string. If I put 2 bytes, each representing some number, I am still able to decode the string that follows using: System.Text.Encoding.ASCII.GetString(buffer,2,length) But if I say 3 bytes, and then the string, I don't get anything (yes, I said buffer,3,length to reflect the server changes), I get garbage.. Can anyone please help me? Thank you.

    G 1 Reply Last reply
    0
    • N nc3b

      Ok here is the problem. I am developing a server in C and a client in C#. The server send streams of data to the client, and the client is responsible for decoding and interpreting these stream. So, if I send a simple "Hello world" from the server to the client, it works. If I send a byte representing an integer (I use memcpy) I can decode it in the client. But, the problems start when after some integers, I also put a string. If I put 2 bytes, each representing some number, I am still able to decode the string that follows using: System.Text.Encoding.ASCII.GetString(buffer,2,length) But if I say 3 bytes, and then the string, I don't get anything (yes, I said buffer,3,length to reflect the server changes), I get garbage.. Can anyone please help me? Thank you.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      What kind of "garbage" do you get? I think that we have to see some code in order to tell what's wrong with the code...

      --- single minded; short sighted; long gone;

      N 1 Reply Last reply
      0
      • G Guffa

        What kind of "garbage" do you get? I think that we have to see some code in order to tell what's wrong with the code...

        --- single minded; short sighted; long gone;

        N Offline
        N Offline
        nc3b
        wrote on last edited by
        #3

        Okay then. Here is the server memcpy(msg,&type,1); memcpy(msg+1,&nr,1); memcpy(msg+2,&online,1); memcpy(msg+3,&p,sizeof(p)); type is 2, nr is 4 online is 0 and p is "Hello world". And on the client I get some unrecognizable characters (those squares you sometimes get). BUT, when online is 1 the string stays in one piece.. What am I supposed to make of this?

        L 1 Reply Last reply
        0
        • N nc3b

          Okay then. Here is the server memcpy(msg,&type,1); memcpy(msg+1,&nr,1); memcpy(msg+2,&online,1); memcpy(msg+3,&p,sizeof(p)); type is 2, nr is 4 online is 0 and p is "Hello world". And on the client I get some unrecognizable characters (those squares you sometimes get). BUT, when online is 1 the string stays in one piece.. What am I supposed to make of this?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          if only you had shown the client code and the server code in one message... and, if at all possible, the hex representation of these data bytes arriving at the client.

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          N 1 Reply Last reply
          0
          • L Luc Pattyn

            if only you had shown the client code and the server code in one message... and, if at all possible, the hex representation of these data bytes arriving at the client.

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            N Offline
            N Offline
            nc3b
            wrote on last edited by
            #5

            But I already showed more than enough. Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE. Anyway, the server uses fprintf and the client uses BeginReceive - EndReceive (IAsync bla bla). The problem is the way I am handling the array of bytes coming from the server. I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..

            L G 2 Replies Last reply
            0
            • N nc3b

              But I already showed more than enough. Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE. Anyway, the server uses fprintf and the client uses BeginReceive - EndReceive (IAsync bla bla). The problem is the way I am handling the array of bytes coming from the server. I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              nc3b wrote:

              But I already showed more than enough

              Sorry for trying to help you. :(( Debug Rule #1: when the system does not perform as designed, at least one element is wrong, and it could be anything; not a single assumption is beyond scrutiny.

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              1 Reply Last reply
              0
              • N nc3b

                But I already showed more than enough. Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE. Anyway, the server uses fprintf and the client uses BeginReceive - EndReceive (IAsync bla bla). The problem is the way I am handling the array of bytes coming from the server. I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                nc3b wrote:

                But I already showed more than enough.

                I haven't seen a single line of the code that recieves the message.

                nc3b wrote:

                Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE.

                Just because some code works in some cases doesn't automatically mean that it works in all cases.

                nc3b wrote:

                I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..

                I don't know why it happens either, as I don't know if you use BeginRecieve and EndRecieve correctly.

                --- single minded; short sighted; long gone;

                N 1 Reply Last reply
                0
                • G Guffa

                  nc3b wrote:

                  But I already showed more than enough.

                  I haven't seen a single line of the code that recieves the message.

                  nc3b wrote:

                  Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE.

                  Just because some code works in some cases doesn't automatically mean that it works in all cases.

                  nc3b wrote:

                  I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..

                  I don't know why it happens either, as I don't know if you use BeginRecieve and EndRecieve correctly.

                  --- single minded; short sighted; long gone;

                  N Offline
                  N Offline
                  nc3b
                  wrote on last edited by
                  #8

                  The thing is that it's a lot of code.. Anyways, let's see... :) The BeginReceive sock.BeginReceive(buffer,0,buffer.Length,SocketFlags.None,receive,_sock); The EndReceive int stop=_sock.EndReceive(result); Is it not correct? :confused:

                  G 1 Reply Last reply
                  0
                  • N nc3b

                    The thing is that it's a lot of code.. Anyways, let's see... :) The BeginReceive sock.BeginReceive(buffer,0,buffer.Length,SocketFlags.None,receive,_sock); The EndReceive int stop=_sock.EndReceive(result); Is it not correct? :confused:

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    That code is correct, but I have no idea if the surrounding code is correct, so that the buffer, receive, _sock and result variables are correct.

                    --- single minded; short sighted; long gone;

                    N 1 Reply Last reply
                    0
                    • G Guffa

                      That code is correct, but I have no idea if the surrounding code is correct, so that the buffer, receive, _sock and result variables are correct.

                      --- single minded; short sighted; long gone;

                      N Offline
                      N Offline
                      nc3b
                      wrote on last edited by
                      #10

                      I might as well archive the project and attach it :laugh: But anyways. I don't mind:)private System.Byte[] buffer=new Byte[1024]; private void SetupReceiveCallback(Socket _sock) { AsyncCallback receive=new AsyncCallback(OnReceive); _sock.BeginReceive(buffer,0,buffer.Length,SocketFlags.None,receive,_sock); } private void OnReceive(IAsyncResult result) { try { Socket _sock=(Socket)result.AsyncState; int stop=_sock.EndReceive(result); String str=System.Text.Encoding.ASCII.GetString(buffer,0,stop); MessageBox.Show(str); process(buffer,stop); SetupReceiveCallback(_sock); } catch { MessageBox.Show("Out"); this.Close(); } }
                      So? :laugh:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups