Advertisement
Wouto1997

Reading/sending network data C#

Mar 27th, 2013
1,597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.05 KB | None | 0 0
  1.         #region Packet helpers
  2.  
  3.         #region To Byte Array
  4.         /// <summary>
  5.         /// Converts a byte value to a byte array
  6.         /// </summary>
  7.         /// <param name="b">input value</param>
  8.         /// <returns>byte array output</returns>
  9.         public static byte[] MakeByte(byte b)
  10.         {
  11.             return new byte[] { b };
  12.         }
  13.         /// <summary>
  14.         /// Converts a short value to a byte array
  15.         /// </summary>
  16.         /// <param name="s">input value</param>
  17.         /// <returns>byte array output</returns>
  18.         public static byte[] MakeShort(short s)
  19.         {
  20.             return BitConverter.GetBytes(s);
  21.         }
  22.         /// <summary>
  23.         /// Converts a ushort value to a byte array
  24.         /// </summary>
  25.         /// <param name="s">input value</param>
  26.         /// <returns>byte array output</returns>
  27.         public static byte[] MakeUshort(ushort s)
  28.         {
  29.             return BitConverter.GetBytes(s);
  30.         }
  31.         /// <summary>
  32.         /// Converts a int value to a byte array
  33.         /// </summary>
  34.         /// <param name="i">input value</param>
  35.         /// <returns>byte array output</returns>
  36.         public static byte[] MakeInt(int i)
  37.         {
  38.             return BitConverter.GetBytes(i);
  39.         }
  40.         /// <summary>
  41.         /// Converts a long value to a byte array
  42.         /// </summary>
  43.         /// <param name="l">input value</param>
  44.         /// <returns>byte array output</returns>
  45.         public static byte[] MakeLong(long l)
  46.         {
  47.             return BitConverter.GetBytes(l);
  48.         }
  49.         /// <summary>
  50.         /// Converts a float value to a byte array
  51.         /// </summary>
  52.         /// <param name="f">input value</param>
  53.         /// <returns>byte array output</returns>
  54.         public static byte[] MakeFloat(float f)
  55.         {
  56.             return BitConverter.GetBytes(f);
  57.         }
  58.         /// <summary>
  59.         /// Converts a double value to a byte array
  60.         /// </summary>
  61.         /// <param name="d">input value</param>
  62.         /// <returns>byte array output</returns>
  63.         public static byte[] MakeDouble(double d)
  64.         {
  65.             return BitConverter.GetBytes(d);
  66.         }
  67.         /// <summary>
  68.         /// Converts a string value to a byte array ( adds an integer header for it's length )
  69.         /// Uses Unicode
  70.         /// </summary>
  71.         /// <param name="s">input value</param>
  72.         /// <returns>byte array output</returns>
  73.         public static byte[] MakeString(string s)
  74.         {
  75.             List<byte> data = new List<byte>();
  76.             data.AddRange(MakeInt(Encoding.Unicode.GetByteCount(s)));
  77.             data.AddRange(Encoding.Unicode.GetBytes(s));
  78.             return data.ToArray();
  79.         }
  80.         /// <summary>
  81.         /// Converts a boolean value to a byte array
  82.         /// </summary>
  83.         /// <param name="b">input value</param>
  84.         /// <returns>byte array output</returns>
  85.         public static byte[] MakeBoolean(bool b)
  86.         {
  87.             return (b) ? new byte[] { 0x01 } : new byte[] { 0x00 };
  88.         }
  89.         /// <summary>
  90.         /// Converts a byte array to a better byte array... deal with it
  91.         /// </summary>
  92.         /// <param name="b">input array</param>
  93.         /// <returns>byte array output ( this time with size-int prefix )</returns>
  94.         public static byte[] MakeByteArray(byte[] b)
  95.         {
  96.             List<byte> data = new List<byte>();
  97.             data.AddRange(MakeInt(b.Length));
  98.             data.AddRange(b);
  99.             return data.ToArray();
  100.         }
  101.         #endregion
  102.  
  103.         #region From Byte Array
  104.         /// <summary>
  105.         /// Reads a byte value from the client's stream
  106.         /// </summary>
  107.         /// <param name="client">Networking client</param>
  108.         /// <returns>byte value</returns>
  109.         public static byte ReadByte(TcpClient client)
  110.         {
  111.             byte[] data = new byte[1];
  112.             client.GetStream().Read(data, 0, 1);
  113.             return data[0];
  114.         }
  115.         /// <summary>
  116.         /// Reads a short value from the client's stream
  117.         /// </summary>
  118.         /// <param name="client">Networking client</param>
  119.         /// <returns>short vlaue</returns>
  120.         public static short ReadShort(TcpClient client)
  121.         {
  122.             byte[] data = new byte[2];
  123.             client.GetStream().Read(data, 0, 2);
  124.             return BitConverter.ToInt16(data, 0);
  125.         }
  126.         /// <summary>
  127.         /// Reads a ushort value from the client's stream
  128.         /// </summary>
  129.         /// <param name="client">Networking client</param>
  130.         /// <returns>ushort vlaue</returns>
  131.         public static ushort ReadUshort(TcpClient client)
  132.         {
  133.             byte[] data = new byte[2];
  134.             client.GetStream().Read(data, 0, 2);
  135.             return BitConverter.ToUInt16(data, 0);
  136.         }
  137.         /// <summary>
  138.         /// Reads an integer value from the client's stream
  139.         /// </summary>
  140.         /// <param name="client">Networking client</param>
  141.         /// <returns>int value</returns>
  142.         public static int ReadInt(TcpClient client)
  143.         {
  144.             byte[] data = new byte[4];
  145.             client.GetStream().Read(data, 0, 4);
  146.             return BitConverter.ToInt32(data, 0);
  147.         }
  148.         /// <summary>
  149.         /// Reads a long value from the client's stream
  150.         /// </summary>
  151.         /// <param name="client">Networking client</param>
  152.         /// <returns>long value</returns>
  153.         public static long ReadLong(TcpClient client)
  154.         {
  155.             byte[] data = new byte[8];
  156.             client.GetStream().Read(data, 0, 8);
  157.             return BitConverter.ToInt64(data, 0);
  158.         }
  159.         /// <summary>
  160.         /// Reads a float value from the client's stream
  161.         /// </summary>
  162.         /// <param name="client">Networking client</param>
  163.         /// <returns>float value</returns>
  164.         public static float ReadFloat(TcpClient client)
  165.         {
  166.             byte[] data = new byte[4];
  167.             client.GetStream().Read(data, 0, 4);
  168.             return BitConverter.ToSingle(data, 0);
  169.         }
  170.         /// <summary>
  171.         /// Reads a double value from the client's stream
  172.         /// </summary>
  173.         /// <param name="client">Networking client</param>
  174.         /// <returns>double value</returns>
  175.         public static double ReadDouble(TcpClient client)
  176.         {
  177.             byte[] data = new byte[8];
  178.             client.GetStream().Read(data, 0, 8);
  179.             return BitConverter.ToDouble(data, 0);
  180.         }
  181.         /// <summary>
  182.         /// Reads a string value from the client's stream
  183.         /// </summary>
  184.         /// <param name="client">Networking client</param>
  185.         /// <returns>string value</returns>
  186.         public static string ReadString(TcpClient client)
  187.         {
  188.             byte[] data = new byte[ReadInt(client)];
  189.             client.GetStream().Read(data, 0, data.Length);
  190.             return Encoding.Unicode.GetString(data);
  191.         }
  192.         /// <summary>
  193.         /// Reads a boolean from the client's stream
  194.         /// </summary>
  195.         /// <param name="client">Networking client</param>
  196.         /// <returns>boolean value</returns>
  197.         public static bool ReadBool(TcpClient client)
  198.         {
  199.             byte[] data = new byte[1];
  200.             client.GetStream().Read(data, 0, 1);
  201.             if (data[0] == 0x01 || data[0] == 0x00)
  202.             {
  203.                 return (data[0] == 0x01);
  204.             }
  205.             else
  206.             {
  207.                 throw new Exception("invalid boolean value for parsing!");
  208.             }
  209.         }
  210.         /// <summary>
  211.         /// Reads a byte array from the client's stream
  212.         /// </summary>
  213.         /// <param name="client">Networking client</param>
  214.         /// <returns>byte array value</returns>
  215.         public static byte[] ReadByteArray(TcpClient client)
  216.         {
  217.             byte[] data = new byte[ReadInt(client)];
  218.             client.GetStream().Read(data, 0, data.Length);
  219.             return data;
  220.         }
  221.         #endregion
  222.  
  223.         #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement