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. Blackhole string!

Blackhole string!

Scheduled Pinned Locked Moved C#
questionloungeworkspace
14 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.
  • U User 3491102

    Below is my code to add a chat message to a multiline text box. I have tested the parameters, when I run them on messagebox's they output the proper value, but when I combine them: "chatView.Text = chatView.Text + Environment.NewLine + myName + ": "+ myText;" Only "myName" shows up on each line. What am i doing wrong?

    public delegate void chatMessageCallBack(string myName, string myText);
    private void chatMessage(string myName, string myText)
    {
    chatView.Text = chatView.Text + Environment.NewLine + myName + ": "+ myText;
    }
    private void addChatMessage(string myName, string myText)
    {
    if (chatView.InvokeRequired)
    {
    chatMessageCallBack d = new chatMessageCallBack(chatMessage);
    Invoke(d, new object[] { myName, myText });
    }
    else
    {
    chatView.Text = chatView.Text + Environment.NewLine + myName + ": " + myText;
    }
    }

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

    Member 3493799 wrote:

    Invoke(d, new object[] { myName, myText });

    IMO should be chatView.Invoke(...) :)

    Luc Pattyn [Forum Guidelines] [My Articles]


    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


    U 1 Reply Last reply
    0
    • U User 3491102

      I know! Nothing works in combination. Heck, even when I remove the newline stuff and just have myName + myText, it only shows myName... weirdest thing ever..

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #6

      what about just textBox.Text = myText;?

      Life goes very fast. Tomorrow, today is already yesterday.

      1 Reply Last reply
      0
      • U User 3491102

        Below is my code to add a chat message to a multiline text box. I have tested the parameters, when I run them on messagebox's they output the proper value, but when I combine them: "chatView.Text = chatView.Text + Environment.NewLine + myName + ": "+ myText;" Only "myName" shows up on each line. What am i doing wrong?

        public delegate void chatMessageCallBack(string myName, string myText);
        private void chatMessage(string myName, string myText)
        {
        chatView.Text = chatView.Text + Environment.NewLine + myName + ": "+ myText;
        }
        private void addChatMessage(string myName, string myText)
        {
        if (chatView.InvokeRequired)
        {
        chatMessageCallBack d = new chatMessageCallBack(chatMessage);
        Invoke(d, new object[] { myName, myText });
        }
        else
        {
        chatView.Text = chatView.Text + Environment.NewLine + myName + ": " + myText;
        }
        }

        N Offline
        N Offline
        Nagy Vilmos
        wrote on last edited by
        #7

        Running it within the IDE, put some breakpoints in on the entry to each of your methods. Step through and see what the values are that are being received. Maybe instead of:

        chatView.Text = chatView.Text + Environment.NewLine + myName + ": "+ myText;

        Try:

        String newText = chatView.Text + Environment.NewLine + myName + ": "+ myText;
        chatView.Text = newText;

        Then after newText has been created you can be sure the correct value is going ito the RHS of the expession. This is debug problem, you just need to establish the values at every step of the process.


        Panic, Chaos, Destruction. My work here is done.

        U 1 Reply Last reply
        0
        • L Luc Pattyn

          Member 3493799 wrote:

          Invoke(d, new object[] { myName, myText });

          IMO should be chatView.Invoke(...) :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          U Offline
          U Offline
          User 3491102
          wrote on last edited by
          #8

          Tried it, didnt work :( Heres the packet decoding scheme: myName = dataSender myText = dataMessage

          [StructLayout(LayoutKind.Sequential, Pack = 1)]
          public struct rawCncPacket
          {
          // IP
          public byte IPVerLen;
          public byte IPTOS;
          public short IPLength;
          public short IPID;
          public short IPOffset;
          public byte IPTTL;
          public byte IPProtocol;
          public short IPChecksum;
          public int IPSrcAddr;
          public int IPDestAddr;
          // UDP
          public short UDPsourcePort;
          public short UDPdestPort;
          public short UDPLength;
          public short UDPcheckSum;
          // CNC
          public int CNCpacketDirection;
          public int CNCpacketNumber;
          public int CNCpacketSource;
          public int CNCpacketDest;
          public short CNCpacketPort;
          public short CNCpacketHeaderEnd;
          // Data
          public byte dataType; // 1
          public byte[] dataSenderBytes;
          public string dataSender; // 12
          public byte[] dataMessageBytes;
          public string dataMessage; // 24
          public int dataPackOne;
          public int dataPackTwo;
          public byte dataPackEnd;

                          public rawCncPacket(byte\[\] packet)
                          {
                              // IP Header section
                              System.IO.MemoryStream stm = new System.IO.MemoryStream(packet, 0, 94);
                              System.IO.BinaryReader rdr = new System.IO.BinaryReader(stm);
                              IPVerLen = rdr.ReadByte();
                              IPTOS = rdr.ReadByte();
                              IPLength = rdr.ReadInt16();
                              IPID = rdr.ReadInt16();
                              IPOffset = rdr.ReadInt16();
                              IPTTL = rdr.ReadByte();
                              IPProtocol = rdr.ReadByte();
                              IPChecksum = rdr.ReadInt16();
                              IPSrcAddr = rdr.ReadInt32();
                              IPDestAddr = rdr.ReadInt32();
                              // UDP header section
                              UDPs
          
          L 1 Reply Last reply
          0
          • N Nagy Vilmos

            Running it within the IDE, put some breakpoints in on the entry to each of your methods. Step through and see what the values are that are being received. Maybe instead of:

            chatView.Text = chatView.Text + Environment.NewLine + myName + ": "+ myText;

            Try:

            String newText = chatView.Text + Environment.NewLine + myName + ": "+ myText;
            chatView.Text = newText;

            Then after newText has been created you can be sure the correct value is going ito the RHS of the expession. This is debug problem, you just need to establish the values at every step of the process.


            Panic, Chaos, Destruction. My work here is done.

            U Offline
            U Offline
            User 3491102
            wrote on last edited by
            #9

            Used breakpoints, saw the values and they were correct. newText was highlighted in red though when I got to the" chatView.Text = newText" (dont know what that means: VS2008). myName = "test" myText = "ETST13245"

            newText "\r\Test\0\0\0\0\0\0\0: ETST13245\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" string

            N 1 Reply Last reply
            0
            • U User 3491102

              Used breakpoints, saw the values and they were correct. newText was highlighted in red though when I got to the" chatView.Text = newText" (dont know what that means: VS2008). myName = "test" myText = "ETST13245"

              newText "\r\Test\0\0\0\0\0\0\0: ETST13245\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" string

              N Offline
              N Offline
              Nagy Vilmos
              wrote on last edited by
              #10

              Member 3493799 wrote:

              \0\0\0\0\0\0\0

              As soon as it's hitting the null char, the display is being truncated.


              Panic, Chaos, Destruction. My work here is done.

              U 1 Reply Last reply
              0
              • N Nagy Vilmos

                Member 3493799 wrote:

                \0\0\0\0\0\0\0

                As soon as it's hitting the null char, the display is being truncated.


                Panic, Chaos, Destruction. My work here is done.

                U Offline
                U Offline
                User 3491102
                wrote on last edited by
                #11

                Cheers, that makes sense, will change it accordingly.

                N 1 Reply Last reply
                0
                • U User 3491102

                  Cheers, that makes sense, will change it accordingly.

                  N Offline
                  N Offline
                  Nagy Vilmos
                  wrote on last edited by
                  #12

                  You are welcome. :-O After yesterday's retardfest it is nice to have a real question to answer. BTW My first port of call when code is not behaving is to debug the thing to smithereens until I find the problem. My second is a call for Port.


                  Panic, Chaos, Destruction. My work here is done.

                  U 1 Reply Last reply
                  0
                  • N Nagy Vilmos

                    You are welcome. :-O After yesterday's retardfest it is nice to have a real question to answer. BTW My first port of call when code is not behaving is to debug the thing to smithereens until I find the problem. My second is a call for Port.


                    Panic, Chaos, Destruction. My work here is done.

                    U Offline
                    U Offline
                    User 3491102
                    wrote on last edited by
                    #13

                    Opening a good old Port may be the solution to many a networking problem ;-)

                    1 Reply Last reply
                    0
                    • U User 3491102

                      Tried it, didnt work :( Heres the packet decoding scheme: myName = dataSender myText = dataMessage

                      [StructLayout(LayoutKind.Sequential, Pack = 1)]
                      public struct rawCncPacket
                      {
                      // IP
                      public byte IPVerLen;
                      public byte IPTOS;
                      public short IPLength;
                      public short IPID;
                      public short IPOffset;
                      public byte IPTTL;
                      public byte IPProtocol;
                      public short IPChecksum;
                      public int IPSrcAddr;
                      public int IPDestAddr;
                      // UDP
                      public short UDPsourcePort;
                      public short UDPdestPort;
                      public short UDPLength;
                      public short UDPcheckSum;
                      // CNC
                      public int CNCpacketDirection;
                      public int CNCpacketNumber;
                      public int CNCpacketSource;
                      public int CNCpacketDest;
                      public short CNCpacketPort;
                      public short CNCpacketHeaderEnd;
                      // Data
                      public byte dataType; // 1
                      public byte[] dataSenderBytes;
                      public string dataSender; // 12
                      public byte[] dataMessageBytes;
                      public string dataMessage; // 24
                      public int dataPackOne;
                      public int dataPackTwo;
                      public byte dataPackEnd;

                                      public rawCncPacket(byte\[\] packet)
                                      {
                                          // IP Header section
                                          System.IO.MemoryStream stm = new System.IO.MemoryStream(packet, 0, 94);
                                          System.IO.BinaryReader rdr = new System.IO.BinaryReader(stm);
                                          IPVerLen = rdr.ReadByte();
                                          IPTOS = rdr.ReadByte();
                                          IPLength = rdr.ReadInt16();
                                          IPID = rdr.ReadInt16();
                                          IPOffset = rdr.ReadInt16();
                                          IPTTL = rdr.ReadByte();
                                          IPProtocol = rdr.ReadByte();
                                          IPChecksum = rdr.ReadInt16();
                                          IPSrcAddr = rdr.ReadInt32();
                                          IPDestAddr = rdr.ReadInt32();
                                          // UDP header section
                                          UDPs
                      
                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #14

                      Hi, not sure why I am getting all this, thanks anyway. some remarks: 1. either this struct has strict requirements or it has not. if it has, I doubt it should hold both public byte[] dataSenderBytes; and public string dataSender; // 12 same for public byte[] dataMessageBytes; and public string dataMessage; // 24 if it has not, don't see why it should need a Pack=1 attribute 2. your problem is with string handling. in the native world (C, C++) strings consist of 8-bit characters (or 16-bit wide characters) and they end on the first ASCII NULL character (byte value zero), which means the NULL character itself cannot be embedded in a string. in the managed world strings can contain any character you choose; if you give 12 bytes (dataSenderBytes) to ASCII.GetString() it probably returns a 12-character string of which several could be NULL (or \0). So I suggest you get the meaningful length of those strings somehow from the byte[] or from the native world, then use ASCIIEncoding.GetString(Byte[], 0, length) :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                      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