diff --git a/TCP/Example1/client.cs b/TCP/Example1/client.cs new file mode 100644 index 0000000..7ecb80f --- /dev/null +++ b/TCP/Example1/client.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.Net.Sockets; +using System.Threading; + +namespace PongExample1Client +{ + public static class ByteExtension + { + public static string GetString(this byte[] arr, int k) + { + return Encoding.ASCII.GetString(arr, 0, k); + } + } + public static class StringExtension + { + public static byte[] ToByteArray(this string str) + { + return Encoding.ASCII.GetBytes(str); + } + } + class Client + { + TcpClient client; + int count = 0; + public Client(IPAddress ip, int port = 5000) + { + client = new TcpClient(); + client.Connect(ip, port); + } + + public Client(int port = 5000) : this(IPAddress.Loopback, port) { } + public Client() : this(IPAddress.Loopback, 5000) { } + + public bool Respond() + { + byte[] buffer = new byte[1024]; + byte[] pong = "ping".ToByteArray(); + + NetworkStream clientStream = client.GetStream(); + clientStream.Write(pong, 0, pong.Length); + Console.WriteLine("Sent {0} to the server - {1}", pong.GetString(pong.Length), count); + Thread.Sleep(2000); + int k = clientStream.Read(buffer, 0, 1024); + Console.WriteLine("Received {0} from the server - {1}", buffer.GetString(k), count); + count++; + return true; + } + } + class Program + { + static void Main(string[] args) + { + Client c = new Client(); + + while (c.Respond()) ; + } + } +} diff --git a/TCP/Example1/server.cs b/TCP/Example1/server.cs new file mode 100644 index 0000000..0ac039b --- /dev/null +++ b/TCP/Example1/server.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.Net.Sockets; +using System.Threading; + +namespace PongExample1 +{ + public static class ByteExtension + { + public static string GetString(this byte[] arr, int k) + { + return Encoding.ASCII.GetString(arr, 0, k); + } + } + public static class StringExtension + { + public static byte[] ToByteArray(this string str) + { + return Encoding.ASCII.GetBytes(str); + } + } + class Server + { + TcpListener server; + Socket s; + int count = 0; + public Server(IPAddress ip, int port = 5000) + { + server = new TcpListener(ip, port); + server.Start(); + Console.WriteLine("Listening on {0} on port {1}", ip.ToString(), port); + s = server.AcceptSocket(); + } + + public Server(int port = 5000) : this(IPAddress.Loopback, port) { } + public Server () : this(IPAddress.Loopback, 5000) { } + + public bool Respond() + { + byte[] buffer = new byte[1024]; + byte[] pong = "pong".ToByteArray(); + + int k = s.Receive(buffer); + Console.WriteLine("Client sent - {0} - {1}", buffer.GetString(k), count); + Thread.Sleep(2000); + s.Send(pong); + Console.WriteLine("Sent to client - {0} - {1}", pong.GetString(pong.Length), count); + count++; + return true; + } + } + class Program + { + static void Main(string[] args) + { + Server s = new Server(); + while (s.Respond()) ; + } + } +} diff --git a/TCP/Example2/client.cs b/TCP/Example2/client.cs new file mode 100644 index 0000000..7ecb80f --- /dev/null +++ b/TCP/Example2/client.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.Net.Sockets; +using System.Threading; + +namespace PongExample1Client +{ + public static class ByteExtension + { + public static string GetString(this byte[] arr, int k) + { + return Encoding.ASCII.GetString(arr, 0, k); + } + } + public static class StringExtension + { + public static byte[] ToByteArray(this string str) + { + return Encoding.ASCII.GetBytes(str); + } + } + class Client + { + TcpClient client; + int count = 0; + public Client(IPAddress ip, int port = 5000) + { + client = new TcpClient(); + client.Connect(ip, port); + } + + public Client(int port = 5000) : this(IPAddress.Loopback, port) { } + public Client() : this(IPAddress.Loopback, 5000) { } + + public bool Respond() + { + byte[] buffer = new byte[1024]; + byte[] pong = "ping".ToByteArray(); + + NetworkStream clientStream = client.GetStream(); + clientStream.Write(pong, 0, pong.Length); + Console.WriteLine("Sent {0} to the server - {1}", pong.GetString(pong.Length), count); + Thread.Sleep(2000); + int k = clientStream.Read(buffer, 0, 1024); + Console.WriteLine("Received {0} from the server - {1}", buffer.GetString(k), count); + count++; + return true; + } + } + class Program + { + static void Main(string[] args) + { + Client c = new Client(); + + while (c.Respond()) ; + } + } +} diff --git a/TCP/Example2/server.cs b/TCP/Example2/server.cs new file mode 100644 index 0000000..35cdfe1 --- /dev/null +++ b/TCP/Example2/server.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.Net.Sockets; +using System.Threading; + +namespace PongExample1 +{ + public static class ByteExtension + { + public static string GetString(this byte[] arr, int k) + { + return Encoding.ASCII.GetString(arr, 0, k); + } + } + public static class StringExtension + { + public static byte[] ToByteArray(this string str) + { + return Encoding.ASCII.GetBytes(str); + } + } + class Server + { + TcpListener server; + Socket s; + int count = 0; + public Server(IPAddress ip, int port = 5000) + { + server = new TcpListener(ip, port); + server.Start(); + Console.WriteLine("Listening on {0} on port {1}", ip.ToString(), port); + + while (true) + { + s = server.AcceptSocket(); + Thread t = new Thread(Respond); + t.Start(s); + } + } + + public Server(int port = 5000) : this(IPAddress.Loopback, port) { } + public Server() : this(IPAddress.Loopback, 5000) { } + + public void Respond(object socket) + { + Socket s = (Socket)socket; + Console.WriteLine("Accepted connection from {0}", s.RemoteEndPoint); + while (true) + { + byte[] buffer = new byte[1024]; + byte[] pong = "pong".ToByteArray(); + + int k = s.Receive(buffer); + Console.WriteLine("Client sent - {0} - {1}", buffer.GetString(k), count); + Thread.Sleep(2000); + s.Send(pong); + Console.WriteLine("Sent to client - {0} - {1}", pong.GetString(pong.Length), count); + count++; + } + + } + } + class Program + { + static void Main(string[] args) + { + Server s = new Server(); + } + } +} diff --git a/TCP/Example3/client.cs b/TCP/Example3/client.cs new file mode 100644 index 0000000..604b9f2 --- /dev/null +++ b/TCP/Example3/client.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.Net.Sockets; + +namespace ServerExample11 +{ + class Program + { + static void Main(string[] args) + { + try + { + TcpClient client = new TcpClient(); + client.Connect(IPAddress.Loopback, 5000); + + Console.WriteLine("Connected to server {0} on port 5000", IPAddress.Loopback); + Console.WriteLine("Sending data..."); + Console.WriteLine("Enter the string to send:"); + string input = Console.ReadLine(); + + byte[] msg = Encoding.ASCII.GetBytes(input); + byte[] buffer = new byte[1024]; + NetworkStream stream = client.GetStream(); + stream.Write(msg, 0, msg.Length); + + int k = stream.Read(buffer, 0, 1024); + Console.WriteLine("Received from server: {0}", Encoding.ASCII.GetString(buffer, 0, k)); + } catch (Exception e) + { + Console.WriteLine(e); + } + } + } +} diff --git a/TCP/Example3/server.cs b/TCP/Example3/server.cs new file mode 100644 index 0000000..592f2c4 --- /dev/null +++ b/TCP/Example3/server.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.Net.Sockets; +using System.Threading; + +namespace ClientExample11 +{ + class Program + { + static void Main(string[] args) + { + try + { + TcpListener server = new TcpListener(IPAddress.Loopback, 5000); + server.Start(); + + Console.WriteLine("Server is listening on port 5000..."); + Console.WriteLine("The local End point is: {0}", server.LocalEndpoint); + Console.WriteLine("Waiting for a connection...."); + + while (true) + { + Thread t = new Thread(handleConnection); + Socket client = server.AcceptSocket(); + t.Start(client); + + } + + + server.Stop(); + } catch (Exception e) + { + Console.WriteLine(e); + } + } + static void handleConnection(object socket) + { + Socket client = (Socket)socket; + + Console.WriteLine("Connection accepted from {0}", client.RemoteEndPoint); + byte[] buffer = new byte[1024]; + + int k = client.Receive(buffer); + Console.WriteLine("Received...."); + Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, k)); + client.Send(Encoding.ASCII.GetBytes("The string was received by the server.")); + Console.WriteLine("\nSent Acknowledgement"); + + client.Close(); + } + } +}