Mercurial > hg > Database > Christie-sharp
changeset 32:3a7a71ee8738
Add TCPListener & Client test
author | riono <e165729@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 16 Apr 2021 01:32:08 +0900 |
parents | 6399d784c6d1 |
children | 7575980bffc9 |
files | Christie_net.csproj Test/RewritingTest/TCPClient.cs Test/RewritingTest/TCPListener.cs |
diffstat | 3 files changed, 90 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/Christie_net.csproj Tue Apr 13 18:25:15 2021 +0900 +++ b/Christie_net.csproj Fri Apr 16 01:32:08 2021 +0900 @@ -3,7 +3,7 @@ <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> - <StartupObject>Christie_net.Test.Example.FizzBuzz.StartFizzBuzz</StartupObject> + <StartupObject>TCPListener</StartupObject> </PropertyGroup> <ItemGroup>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/RewritingTest/TCPClient.cs Fri Apr 16 01:32:08 2021 +0900 @@ -0,0 +1,31 @@ +using System; +using System.Net.Sockets; +using System.Text; + +namespace Christie_net { +public class TCPClient { + public static void Main() { + TcpClient client = null; + + client = new TcpClient("127.0.0.1", 11000); + + Console.WriteLine("connect!"); + + NetworkStream stream = client.GetStream(); + + Byte[] data = Encoding.ASCII.GetBytes("HelloWorld"); + stream.Write(data, 0, data.Length); + Console.WriteLine("Sent"); + + data = new Byte[256]; + + Int32 bytes = stream.Read(data, 0, data.Length); + string receiveData = Encoding.ASCII.GetString(data, 0, bytes); + Console.WriteLine("received:" + receiveData); + + stream.Close(); + client.Close(); + + } +} +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/RewritingTest/TCPListener.cs Fri Apr 16 01:32:08 2021 +0900 @@ -0,0 +1,58 @@ +using System; +using System.Net; +using System.Net.Sockets; + +public class TCPListener { + + public static void Main() { + TcpListener server = null; + try { + IPAddress localAddress = IPAddress.Parse("127.0.0.1"); + + server = new TcpListener(IPAddress.IPv6Any, 11000); + // ipv4/v6対応 + server.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0); + + // 開始 + server.Start(); + + string data = null; + Byte[] bytes = new byte[256]; + + while (true) { + Console.Write("Waiting for a connection... "); + + TcpClient client = server.AcceptTcpClient(); + + data = null; + + NetworkStream stream = client.GetStream(); + + int i; + + // Loop to receive all the data sent by the client. + while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { + // Translate data bytes to a ASCII string. + data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); + Console.WriteLine("Received: {0}", data); + + // Process the data sent by the client. + data = data.ToUpper(); + + byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); + + // Send back a response. + stream.Write(msg, 0, msg.Length); + Console.WriteLine("Sent: {0}", data); + } + + // Shutdown and end connection + client.Close(); + } + } catch (SocketException e) { + Console.WriteLine("SocketException: {0}", e); + } finally { + server.Stop(); + } + } +} \ No newline at end of file