diff --git a/Class Examples/TCP-client.cs b/Class Examples/TCP-client.cs new file mode 100644 index 0000000..604b9f2 --- /dev/null +++ b/Class Examples/TCP-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/Class Examples/TCP-multi-client.cs b/Class Examples/TCP-multi-client.cs new file mode 100644 index 0000000..592f2c4 --- /dev/null +++ b/Class Examples/TCP-multi-client.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(); + } + } +} diff --git a/Class Examples/TCP-server.cs b/Class Examples/TCP-server.cs new file mode 100644 index 0000000..eef52b8 --- /dev/null +++ b/Class Examples/TCP-server.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.Net.Sockets; + +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...."); + + Socket client = server.AcceptSocket(); + 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(); + server.Stop(); + } catch (Exception e) + { + Console.WriteLine(e); + } + } + } +} diff --git a/Class Examples/thread1-example.cs b/Class Examples/thread1-example.cs new file mode 100644 index 0000000..de0fef2 --- /dev/null +++ b/Class Examples/thread1-example.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace threading1 +{ + class Program + { + static void Main(string[] args) + { + List threads = new List(); + for (int i = 0; i < 10; i++) + { + Thread t = new Thread(t1); + t.Start(i); + threads.Add(t); + } + + for (int i = 0; i < threads.Count; i++) + threads[i].Join(); + + } + + public static void t1(object tn) + { + for (int i = 0; i < 10; i++) + Console.WriteLine("Thread {0} - {1}", (int)tn, i); + } + } +} diff --git a/Class Examples/thread2-example.cs b/Class Examples/thread2-example.cs new file mode 100644 index 0000000..4f4674f --- /dev/null +++ b/Class Examples/thread2-example.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace threading1 +{ + class Program + { + static volatile int counter = 0; + static void Main(string[] args) + { + Thread thread1 = new Thread(new ThreadStart(t1)); + + Thread thread2 = new Thread(new ThreadStart(t2)); + thread1.Start(); + thread2.Start(); + thread1.Join(); + thread2.Join(); + Console.WriteLine(Program.counter); + + } + + public static void t1() + { + for (int i = 0; i < 99999; i++) + Program.counter++; + } + + public static void t2() + { + for (int i = 0; i < 99999; i++) + Program.counter--; + } + } +} diff --git a/Class Examples/thread3-example.cs b/Class Examples/thread3-example.cs new file mode 100644 index 0000000..539c254 --- /dev/null +++ b/Class Examples/thread3-example.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace threading1 +{ + class Program + { + const int BUFFER_SIZE = 20; + static volatile int counter = -1; + static volatile double []buffer = new double[BUFFER_SIZE]; + static void Main(string[] args) + { + for (int i = 0; i < BUFFER_SIZE; i++) + buffer[i] = -1; + Thread thread1 = new Thread(new ThreadStart(produce)); + Thread thread2 = new Thread(new ThreadStart(consume)); + thread1.Start(); + thread2.Start(); + thread1.Join(); + thread2.Join(); + //Console.WriteLine(Program.counter); + + } + + public static void produce() + { + Random num = new Random(); + while (true) + { + while (counter == BUFFER_SIZE - 1) ; + Program.buffer[counter + 1] = Convert.ToInt32((num.NextDouble() % 10) * 10) + 1; + Console.WriteLine("Producer value = {0} counter = {1}", Program.buffer[counter + 1], counter + 1); + counter++; + } + } + + public static void consume() + { + while (true) + { + while (counter == -1) ; + Console.WriteLine("Consumer value = {0} counter = {1}", buffer[counter], counter); + Program.buffer[counter] = 0; + counter--; + } + } + } +}