oo-70-245-1

oo-70-245-1 Commit Details


Date:2019-06-18 21:57:54 (5 years 3 months ago)
Author:Natalie Adams
Branch:master
Commit:d6b38a9b4b710a778d408d7b6e9f133719a6b751
Parents: a7e8a25570d3b4dd264e1ade50bb17b7d676ae77
Message:adding examples

Changes:

File differences

Class Examples/TCP-client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
}
}
}
}
Class Examples/TCP-multi-client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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();
}
}
}
Class Examples/TCP-server.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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);
}
}
}
}
Class Examples/thread1-example.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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<Thread> threads = new List<Thread>();
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);
}
}
}
Class Examples/thread2-example.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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--;
}
}
}
Class Examples/thread3-example.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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--;
}
}
}
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.07572s using 14 queries.