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. Chars received

Chars received

Scheduled Pinned Locked Moved C#
5 Posts 4 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.
  • T Offline
    T Offline
    Tichaona J
    wrote on last edited by
    #1

    Hi I am trying to figure out why my method keeps only showing me the first character from a sent string. I would like it to show all the characters that were sent.

    public void OnDataReceived(IAsyncResult asyn)
    {
    try
    {
    SocketPacket socketData = (SocketPacket)asyn.AsyncState;

                int iRx = 0;
    
                iRx = socketData.currentSoc.EndReceive(asyn);
    
                char\[\] chars = new char\[iRx\];
                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
    
                int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
    
                System.String szData = new System.String(chars);
    
                MessageBox.Show(szData);<<<<<<<<<<<
    
    D P L 3 Replies Last reply
    0
    • T Tichaona J

      Hi I am trying to figure out why my method keeps only showing me the first character from a sent string. I would like it to show all the characters that were sent.

      public void OnDataReceived(IAsyncResult asyn)
      {
      try
      {
      SocketPacket socketData = (SocketPacket)asyn.AsyncState;

                  int iRx = 0;
      
                  iRx = socketData.currentSoc.EndReceive(asyn);
      
                  char\[\] chars = new char\[iRx\];
                  System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
      
                  int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
      
                  System.String szData = new System.String(chars);
      
                  MessageBox.Show(szData);<<<<<<<<<<<
      
      D Offline
      D Offline
      dbrenth
      wrote on last edited by
      #2

      Instead of

      System.String szData = new System.String(chars)

      you could try

      StringBuilder str = new StringBuilder();
      for (int x = 0; x < charLen; x++)
      {
      str.Append(chars[x]);
      }

      I haven't had a problem with that.

      Brent

      T 1 Reply Last reply
      0
      • D dbrenth

        Instead of

        System.String szData = new System.String(chars)

        you could try

        StringBuilder str = new StringBuilder();
        for (int x = 0; x < charLen; x++)
        {
        str.Append(chars[x]);
        }

        I haven't had a problem with that.

        Brent

        T Offline
        T Offline
        Tichaona J
        wrote on last edited by
        #3

        Hate to break it you, but this had no effect.

        1 Reply Last reply
        0
        • T Tichaona J

          Hi I am trying to figure out why my method keeps only showing me the first character from a sent string. I would like it to show all the characters that were sent.

          public void OnDataReceived(IAsyncResult asyn)
          {
          try
          {
          SocketPacket socketData = (SocketPacket)asyn.AsyncState;

                      int iRx = 0;
          
                      iRx = socketData.currentSoc.EndReceive(asyn);
          
                      char\[\] chars = new char\[iRx\];
                      System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
          
                      int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
          
                      System.String szData = new System.String(chars);
          
                      MessageBox.Show(szData);<<<<<<<<<<<
          
          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          What happens when you put a break point at socketData.currentSoc.EndReceive and then step over the code? What do you see when you watch the variables? [Edit]I see you don't like debugging - too much like hard work is it?[/Edit]

          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          modified on Saturday, October 23, 2010 6:47 AM

          1 Reply Last reply
          0
          • T Tichaona J

            Hi I am trying to figure out why my method keeps only showing me the first character from a sent string. I would like it to show all the characters that were sent.

            public void OnDataReceived(IAsyncResult asyn)
            {
            try
            {
            SocketPacket socketData = (SocketPacket)asyn.AsyncState;

                        int iRx = 0;
            
                        iRx = socketData.currentSoc.EndReceive(asyn);
            
                        char\[\] chars = new char\[iRx\];
                        System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
            
                        int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
            
                        System.String szData = new System.String(chars);
            
                        MessageBox.Show(szData);<<<<<<<<<<<
            
            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Several things could be wrong here, however I can't tell which one(s) it will be as you have shown only a fraction of the relevant code. Here are the main possibilities: 1. you may be invoking a receive with a small buffer; if you were to ask for one byte, then you would only get one byte. 2. you might have an encoding mismatch between sender and receiver. e.g. if the sender uses Unicode, regular characters would look like real (ANSI) bytes and NULLs intertwined; an 8-bit encoder would react badly on the NULLs. 3. what EndReceive returns is the number of bytes, not characters, hence char[] chars = new char[iRx]; is wrong. IMO you should use [EDIT] Encoding.GetString() right away to convert bytes into a string.[/EDIT] :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            modified on Friday, October 22, 2010 10:04 PM

            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