diff --git a/Socket/ByteOrder/ByteOrder.csproj b/Socket/ByteOrder/ByteOrder.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Socket/ByteOrder/ByteOrder.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Socket/ByteOrder/Program.cs b/Socket/ByteOrder/Program.cs new file mode 100644 index 0000000..060865a --- /dev/null +++ b/Socket/ByteOrder/Program.cs @@ -0,0 +1,30 @@ +using System.Net; + +namespace ByteOrder; + +internal class Program +{ + static void Main(string[] args) + { + /* + * 바이트 오더: 바이트를 메모리에 저장하는 순서 + * 인텔/AMD 계열: 리틀엔디언 - 하위 바이트 먼저 저장 + * ARM 계열: 빅엔디언 - 상위 바이트 먼저 저장 + */ + + byte[] buffer = BitConverter.GetBytes(12345678); + Console.WriteLine(BitConverter.ToString(buffer)); + + // ★★★ 네트워크 레벨에서는 반드시 빅엔디언 쓰기로 약속함 + + // 호스트 바이트 순서에서 네트워크 바이트 순서로 변경 + int num2 = IPAddress.HostToNetworkOrder(12345678); + byte[] buffer2 = BitConverter.GetBytes(num2); + Console.WriteLine(BitConverter.ToString(buffer2)); + + // 네트워크 바이트 순서에서 호스트 바이트 순서로 변경 + int num3 = BitConverter.ToInt32(buffer2); + int num4 = IPAddress.NetworkToHostOrder(num3); + Console.WriteLine(num4); + } +} \ No newline at end of file diff --git a/Socket/Serialize/Program.cs b/Socket/Serialize/Program.cs new file mode 100644 index 0000000..ddf2247 --- /dev/null +++ b/Socket/Serialize/Program.cs @@ -0,0 +1,27 @@ +using System.Text; + +namespace Serialize; + +internal class Program +{ + static void Main(string[] args) + { + //int num = 1234; + //byte[] buffer = BitConverter.GetBytes(num); + //Console.WriteLine($"int byte length: {buffer.Length}"); // 8bit * 4 = 4byte + //int num2 = BitConverter.ToInt32(buffer, 0); + //Console.WriteLine($"deserialized: {num2}"); + + //long num = 1234; + //byte[] buffer = BitConverter.GetBytes(num); + //Console.WriteLine($"long byte length: {buffer.Length}"); // 8bit * 8 = 8byte + //long num2 = BitConverter.ToInt32(buffer, 0); + //Console.WriteLine($"deserialized: {num2}"); + + string str = "Hello world"; + byte[] buffer = Encoding.UTF8.GetBytes(str); + Console.WriteLine($"str byte length: {buffer.Length}"); // 8bit * 8 = 8byte + string str2 = Encoding.UTF8.GetString(buffer); + Console.WriteLine($"deserialized: {str2}"); + } +} \ No newline at end of file diff --git a/Socket/Serialize/Serialize.csproj b/Socket/Serialize/Serialize.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Socket/Serialize/Serialize.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Socket/Socket.sln b/Socket/Socket.sln index 658306e..1f0351c 100644 --- a/Socket/Socket.sln +++ b/Socket/Socket.sln @@ -7,6 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{5EEB261C-98C2-4467-9CC3-2129F0031114}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serialize", "Serialize\Serialize.csproj", "{E2C0CA13-3138-4CB3-9B41-2C97BA520309}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ByteOrder", "ByteOrder\ByteOrder.csproj", "{34446D97-4EA2-4FEA-8366-B730469A7A87}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +25,14 @@ Global {5EEB261C-98C2-4467-9CC3-2129F0031114}.Debug|Any CPU.Build.0 = Debug|Any CPU {5EEB261C-98C2-4467-9CC3-2129F0031114}.Release|Any CPU.ActiveCfg = Release|Any CPU {5EEB261C-98C2-4467-9CC3-2129F0031114}.Release|Any CPU.Build.0 = Release|Any CPU + {E2C0CA13-3138-4CB3-9B41-2C97BA520309}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E2C0CA13-3138-4CB3-9B41-2C97BA520309}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E2C0CA13-3138-4CB3-9B41-2C97BA520309}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E2C0CA13-3138-4CB3-9B41-2C97BA520309}.Release|Any CPU.Build.0 = Release|Any CPU + {34446D97-4EA2-4FEA-8366-B730469A7A87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34446D97-4EA2-4FEA-8366-B730469A7A87}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34446D97-4EA2-4FEA-8366-B730469A7A87}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34446D97-4EA2-4FEA-8366-B730469A7A87}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE