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. Packet issues

Packet issues

Scheduled Pinned Locked Moved C#
comdata-structures
8 Posts 2 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.
  • S Offline
    S Offline
    see_seA
    wrote on last edited by
    #1

    I've highly modified the code found here http://www.codeproject.com/KB/IP/ChatAsynchTCPSockets.aspx. When creating a packet, I give the following class the info it needs onto which is converts it into a byte[] and visa versa which can then be sent and received by a socket respectively.

    public class Data
    {
        //Default constructor
        public Command cmdCommand; //Command type (login, logout, send message, etcetera)
        public string strMessage; //Message text
        public string strName; //Name by which the client logs into the room
    
        public Data()
        {
            cmdCommand = Command.Null;
            strMessage = null;
            strName = null;
        }
    
        //Converts the bytes into an object of type Data
        public Data(byte\[\] data)
        {
            //The first four bytes are for the Command
            cmdCommand = (Command)BitConverter.ToInt32(data, 0);
    
            //The next four store the length of the name
            int nameLen = BitConverter.ToInt32(data, 4);
    
            //The next four store the length of the message
            int msgLen = BitConverter.ToInt32(data, 8);
    
            //This check makes sure that strName has been passed in the array of bytes
            if (nameLen > 0)
                strName = Encoding.UTF8.GetString(data, 12, nameLen);
            else
                strName = null;
    
            //This checks for a null message field
            if (msgLen > 0)
                strMessage = Encoding.UTF8.GetString(data, 12 + nameLen, msgLen);
            else
                strMessage = null;
        }
    
        //Converts the Data structure into an array of bytes
        public byte\[\] ToByte()
        {
            List result = new List();
    
            //First four are for the Command
            result.AddRange(BitConverter.GetBytes((int)cmdCommand));
    
            //Add the length of the name
            if (strName != null)
                result.AddRange(BitConverter.GetBytes(strName.Length));
            else
                result.AddRange(BitConverter.GetBytes(0));
    
            //Length of the message
            if (strMessage != null)
                result.AddRange(BitConverter.GetBytes(Encoding.UTF8.GetByteCount(strMessage)));
            else
                result.AddRange(BitConverter.GetBytes(0));
    
            //Add the name
            if (strName !=
    
    L 2 Replies Last reply
    0
    • S see_seA

      I've highly modified the code found here http://www.codeproject.com/KB/IP/ChatAsynchTCPSockets.aspx. When creating a packet, I give the following class the info it needs onto which is converts it into a byte[] and visa versa which can then be sent and received by a socket respectively.

      public class Data
      {
          //Default constructor
          public Command cmdCommand; //Command type (login, logout, send message, etcetera)
          public string strMessage; //Message text
          public string strName; //Name by which the client logs into the room
      
          public Data()
          {
              cmdCommand = Command.Null;
              strMessage = null;
              strName = null;
          }
      
          //Converts the bytes into an object of type Data
          public Data(byte\[\] data)
          {
              //The first four bytes are for the Command
              cmdCommand = (Command)BitConverter.ToInt32(data, 0);
      
              //The next four store the length of the name
              int nameLen = BitConverter.ToInt32(data, 4);
      
              //The next four store the length of the message
              int msgLen = BitConverter.ToInt32(data, 8);
      
              //This check makes sure that strName has been passed in the array of bytes
              if (nameLen > 0)
                  strName = Encoding.UTF8.GetString(data, 12, nameLen);
              else
                  strName = null;
      
              //This checks for a null message field
              if (msgLen > 0)
                  strMessage = Encoding.UTF8.GetString(data, 12 + nameLen, msgLen);
              else
                  strMessage = null;
          }
      
          //Converts the Data structure into an array of bytes
          public byte\[\] ToByte()
          {
              List result = new List();
      
              //First four are for the Command
              result.AddRange(BitConverter.GetBytes((int)cmdCommand));
      
              //Add the length of the name
              if (strName != null)
                  result.AddRange(BitConverter.GetBytes(strName.Length));
              else
                  result.AddRange(BitConverter.GetBytes(0));
      
              //Length of the message
              if (strMessage != null)
                  result.AddRange(BitConverter.GetBytes(Encoding.UTF8.GetByteCount(strMessage)));
              else
                  result.AddRange(BitConverter.GetBytes(0));
      
              //Add the name
              if (strName !=
      
      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Please fix the generics in the post. I am pretty sure that is a List of byte.

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      1 Reply Last reply
      0
      • S see_seA

        I've highly modified the code found here http://www.codeproject.com/KB/IP/ChatAsynchTCPSockets.aspx. When creating a packet, I give the following class the info it needs onto which is converts it into a byte[] and visa versa which can then be sent and received by a socket respectively.

        public class Data
        {
            //Default constructor
            public Command cmdCommand; //Command type (login, logout, send message, etcetera)
            public string strMessage; //Message text
            public string strName; //Name by which the client logs into the room
        
            public Data()
            {
                cmdCommand = Command.Null;
                strMessage = null;
                strName = null;
            }
        
            //Converts the bytes into an object of type Data
            public Data(byte\[\] data)
            {
                //The first four bytes are for the Command
                cmdCommand = (Command)BitConverter.ToInt32(data, 0);
        
                //The next four store the length of the name
                int nameLen = BitConverter.ToInt32(data, 4);
        
                //The next four store the length of the message
                int msgLen = BitConverter.ToInt32(data, 8);
        
                //This check makes sure that strName has been passed in the array of bytes
                if (nameLen > 0)
                    strName = Encoding.UTF8.GetString(data, 12, nameLen);
                else
                    strName = null;
        
                //This checks for a null message field
                if (msgLen > 0)
                    strMessage = Encoding.UTF8.GetString(data, 12 + nameLen, msgLen);
                else
                    strMessage = null;
            }
        
            //Converts the Data structure into an array of bytes
            public byte\[\] ToByte()
            {
                List result = new List();
        
                //First four are for the Command
                result.AddRange(BitConverter.GetBytes((int)cmdCommand));
        
                //Add the length of the name
                if (strName != null)
                    result.AddRange(BitConverter.GetBytes(strName.Length));
                else
                    result.AddRange(BitConverter.GetBytes(0));
        
                //Length of the message
                if (strMessage != null)
                    result.AddRange(BitConverter.GetBytes(Encoding.UTF8.GetByteCount(strMessage)));
                else
                    result.AddRange(BitConverter.GetBytes(0));
        
                //Add the name
                if (strName !=
        
        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        At which point do you get this error? I suspect, the byte[] you pass to the constructor does not contain the correct data. Add a check in the beginning of the constructor to ensure the byte[] is long enough at least.

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)

        S 1 Reply Last reply
        0
        • L leppie

          At which point do you get this error? I suspect, the byte[] you pass to the constructor does not contain the correct data. Add a check in the beginning of the constructor to ensure the byte[] is long enough at least.

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          S Offline
          S Offline
          see_seA
          wrote on last edited by
          #4

          As far as I can tell, I get the error after executing the following commands: In the OnAccept method..

                      byte\[\] bufData = new byte\[1024\];
                      clientSocket.BeginReceive(bufData, 0, bufData.Length, SocketFlags.None, new AsyncCallback(OnReceive),
                                                new object\[\] {clientSocket, bufData});
          

          In the OnReceive method..

          Socket clientSocket = null;
          Data msgReceived = null;
          byte[] bufData = new byte[1024];
          bool bypass = false;
          ClientInfo cInfo = null;

                  try
                  {
                      bool blInvalidClient = false;
          
                      try
                      {
                          object\[\] oData = (object\[\]) ar.AsyncState;
                          clientSocket = (Socket) oData\[0\];
                          bufData = (byte\[\]) oData\[1\];
                          clientSocket.EndReceive(ar);
          
                          //Console.WriteLine(clientList.Count);
                      }
                      catch (SocketException)
                      {
                          blInvalidClient = true;
                      }
          
                      if (!blInvalidClient)
                      {
                          //Transform the array of bytes received from the user into an
                          //intelligent form of object Data
                          msgReceived = Packet.PacketReceived(bufData);
          

          ...

          The Packet.PacketReceived method returns new Data(bufData). I've added traces in that constructor and there is something incorrect happening. All clients that communicate use the same Data class to convert the info into a byte[] so it's not as if I'm sending an arb string of bytes to the socket. But the weird thing is that this only ever happens when I sent lots of packets to the socket at once.

          S 2 Replies Last reply
          0
          • S see_seA

            As far as I can tell, I get the error after executing the following commands: In the OnAccept method..

                        byte\[\] bufData = new byte\[1024\];
                        clientSocket.BeginReceive(bufData, 0, bufData.Length, SocketFlags.None, new AsyncCallback(OnReceive),
                                                  new object\[\] {clientSocket, bufData});
            

            In the OnReceive method..

            Socket clientSocket = null;
            Data msgReceived = null;
            byte[] bufData = new byte[1024];
            bool bypass = false;
            ClientInfo cInfo = null;

                    try
                    {
                        bool blInvalidClient = false;
            
                        try
                        {
                            object\[\] oData = (object\[\]) ar.AsyncState;
                            clientSocket = (Socket) oData\[0\];
                            bufData = (byte\[\]) oData\[1\];
                            clientSocket.EndReceive(ar);
            
                            //Console.WriteLine(clientList.Count);
                        }
                        catch (SocketException)
                        {
                            blInvalidClient = true;
                        }
            
                        if (!blInvalidClient)
                        {
                            //Transform the array of bytes received from the user into an
                            //intelligent form of object Data
                            msgReceived = Packet.PacketReceived(bufData);
            

            ...

            The Packet.PacketReceived method returns new Data(bufData). I've added traces in that constructor and there is something incorrect happening. All clients that communicate use the same Data class to convert the info into a byte[] so it's not as if I'm sending an arb string of bytes to the socket. But the weird thing is that this only ever happens when I sent lots of packets to the socket at once.

            S Offline
            S Offline
            see_seA
            wrote on last edited by
            #5

            Problem seems to be here. Not sure I understand why...

                        if (nameLen > 0)
                            strName = Encoding.UTF8.GetString(data, 12, nameLen);
                        else
                            strName = null;
            
            L 1 Reply Last reply
            0
            • S see_seA

              Problem seems to be here. Not sure I understand why...

                          if (nameLen > 0)
                              strName = Encoding.UTF8.GetString(data, 12, nameLen);
                          else
                              strName = null;
              
              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              It probably does not allow you to read zero strings (assuming it needs a 0 terminator).

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              1 Reply Last reply
              0
              • S see_seA

                As far as I can tell, I get the error after executing the following commands: In the OnAccept method..

                            byte\[\] bufData = new byte\[1024\];
                            clientSocket.BeginReceive(bufData, 0, bufData.Length, SocketFlags.None, new AsyncCallback(OnReceive),
                                                      new object\[\] {clientSocket, bufData});
                

                In the OnReceive method..

                Socket clientSocket = null;
                Data msgReceived = null;
                byte[] bufData = new byte[1024];
                bool bypass = false;
                ClientInfo cInfo = null;

                        try
                        {
                            bool blInvalidClient = false;
                
                            try
                            {
                                object\[\] oData = (object\[\]) ar.AsyncState;
                                clientSocket = (Socket) oData\[0\];
                                bufData = (byte\[\]) oData\[1\];
                                clientSocket.EndReceive(ar);
                
                                //Console.WriteLine(clientList.Count);
                            }
                            catch (SocketException)
                            {
                                blInvalidClient = true;
                            }
                
                            if (!blInvalidClient)
                            {
                                //Transform the array of bytes received from the user into an
                                //intelligent form of object Data
                                msgReceived = Packet.PacketReceived(bufData);
                

                ...

                The Packet.PacketReceived method returns new Data(bufData). I've added traces in that constructor and there is something incorrect happening. All clients that communicate use the same Data class to convert the info into a byte[] so it's not as if I'm sending an arb string of bytes to the socket. But the weird thing is that this only ever happens when I sent lots of packets to the socket at once.

                S Offline
                S Offline
                see_seA
                wrote on last edited by
                #7

                Packets are lost due to this under high load. I'm out of ideas.

                S 1 Reply Last reply
                0
                • S see_seA

                  Packets are lost due to this under high load. I'm out of ideas.

                  S Offline
                  S Offline
                  see_seA
                  wrote on last edited by
                  #8

                  Hmmm. I increased the buffer size from 1024 to 4096 and everything seems to work perfectly now.

                  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