Professional Documents
Culture Documents
201014
BSCYS 7B
Question 1:
Part A:
Write a C# program that uses two threads to increment a shared counter. Ensure that the counter is updated
Code:
using System;
using System.Threading;
class Program
{
static int counter = 0;
static object lockObject = new object();
{
// Lock critical section
lock (lockObject)
{
counter++;
}
}
}
}
ScreenShot:
Part B:
i. Write a C# program containing a multi-client chat server allows multiple clients to connect, send
messages, and receive messages from all other connected clients in a chat room. (client server
communication)
Code
Server:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
while (true)
{
TcpClient client = listener.AcceptTcpClient();
lock (clients)
{
clients.Add(client);
}
Task.Run(() => HandleClient(client));
}
}
lock (clients)
{
clients.Remove(client);
}
client.Close();
}
Client:
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
try
{
client.GetStream().Write(Encoding.ASCII.GetBytes(message));
}
catch (Exception ex)
{
Console.WriteLine($"Error sending data: {ex.Message}");
}
}
Screenshot:
Question 2:
Part A:
Write a program to find and print prime numbers in a given range using OMNeT++.
Code:
using System;
class Program
{
static void Main()
{
Console.Write("Enter the lower bound of the range: ");
int lowerBound = int.Parse(Console.ReadLine());
Console.WriteLine();
}
Screenshot:
Part B:
i. Write a program that adds two matrices in parallel (using TPL Library).
Code:
using System;
using System.Threading.Tasks;
class Program
{
static void Main()
{
Console.Write("Enter the number of rows: ");
int rows = int.Parse(Console.ReadLine());
Screenshot: