Having Trouble building a distributed calculator system with C sharp - c#

Can someone help me with this? This code is intended to to take 2 numbers from a client system and use the Clientserver Code to add the two numbers and return the number to the client, but as it is , the when the numbers are entered, there is simply no output
Code for The Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalcService;
namespace CalcClient
{
class Program
{
static void Main(string[] args)
{
Type requiredType = typeof(ICalculator);
ICalculator proxyRemoteObject =
(ICalculator)Activator.GetObject(requiredType, "tcp://10.10.10.10:500/Serverone");
Console.WriteLine("Number 1: ");
int number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Number 2: ");
int number2 = Convert.ToInt32(Console.ReadLine());
int answer = proxyRemoteObject.AddNumbers(number1, number2);
Console.WriteLine("Answer: " + answer);
Console.ReadLine();
}
}
}
Code for The Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalcService
{
public interface ICalculator
{
int AddNumbers(int number1, int number2);
}
}
Code for The Server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using CalcService;
namespace CalcServer
{
class Program
{
static void Main(string[] args)
{
TcpChannel channel = new TcpChannel(500);
ChannelServices.RegisterChannel(channel,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyCalculator), "Serverone", WellKnownObjectMode.SingleCall);
Console.WriteLine("Server is running");
Console.ReadLine();
}
}
public class MyCalculator: MarshalByRefObject, ICalculator
{
public int AddNumbers(int num1, int num2)
{
return num1 + num2;
}
}
}

Changing The server to 127.0.0.1 solved the problem! Sorry for wasting anyones time. Thank you user Henk!

Related

Namespace error while defining a BitVector32 collection in C#

I have written the following code in visual studio 2019, but it gives me an error saying that BitVector32 is a namespace but is used as a type here and CreateMask() method is not existing in the BitVector32 namespace
using System;
using System.Collections.Specialized;
namespace BitVector32
{
class Program
{
static void Main(string[] args)
{
basicVector();
}
public static void basicVector()
{
BitVector32 b = new BitVector32(0);
int myBit1 = BitVector32.CreateMask();
int myBit2 = BitVector32.CreateMask(myBit1);
int myBit3 = BitVector32.CreateMask(myBit2);
int myBit4 = BitVector32.CreateMask(myBit3);
int myBit5 = BitVector32.CreateMask(myBit4);
}
}
}
i referred the Microsoft doc at https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.bitvector32?view=netcore-3.1 and did the same but it gives the above mentioned errors
This is due to your namespace at the top being BitVector32. Change the namespace to another name other than BitVector32:
using System;
using System.Collections.Specialized;
namespace SomethingOtherThanBitVector32
{
class Program
{
static void Main(string[] args)
{
basicVeector();
}
public static void basicVeector()
{
BitVector32 b = new BitVector32(0);
int myBit1 = BitVector32.CreateMask();
int myBit2 = BitVector32.CreateMask(myBit1);
int myBit3 = BitVector32.CreateMask(myBit2);
int myBit4 = BitVector32.CreateMask(myBit3);
int myBit5 = BitVector32.CreateMask(myBit4);
}
}
}

Exception in simple program using opentk.openal

Where the code initially came from
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Audio.OpenAL;
namespace OpenTK_OpenAL
{
class Program
{
static unsafe void Main(string[] args)
{
IntPtr device = Alc.OpenDevice(null);
ContextHandle context = Alc.CreateContext(device, (int*)null);
Alc.MakeContextCurrent(context);
string version = AL.Get(ALGetString.Version);
string vendor = AL.Get(ALGetString.Vendor);
string renderer = AL.Get(ALGetString.Renderer);
Console.WriteLine(version);
Console.WriteLine(vendor);
Console.WriteLine(renderer);
Console.ReadKey();
}
}
}
I am looking at the link above and wrote a program, but the program does not work. Can I get a suggestion of why this doesn't work? There is an exception at the very beginning of the code, IntPtr device = Alc.OpenDevice(null);.

Looking for the wrong namespace?

I am having some namespace issue that is confusing me why it is happening.
In the below code, System.IO & System.Reflection is attempting to reference abc.System instead of using the System namespace I declared at the top. Why is that?
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace abc.Data
{
public sealed class Access
{
public static void Open(string dbPath)
{
// error here referencing abc.System in System.IO, and System.Reflection.
string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); }
}
I then have another namespace in a separate file as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace abc.System
{
public static class DateTimeExtensions
{
// Implemented from
// http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-1 * diff).Date;
}
public static DateTime EndOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(diff).Date;
}
}
}
Because there's the namespace collision, you'll need to use the global keyword to make it clear what you're trying to access.
string appPath = global::System.IO.Path.GetDirectoryName(global::System.Reflection.Assembly.GetExecutingAssembly().Location);
Or, change your namespaces if you can, because that's going to get annoying really fast!

I am having trouble overlaying graphics on another window

Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test {
class Program {
static void Main(string[] args) {
for (int i = 0; i < 1000000000; i++) {
WindowHandler.testOverlay();
}
}
}
}
WindowHandler.cs:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test {
static class WindowHandler {
private const String WINDOW_TITLE = "Minesweeper";
private static IntPtr windowHandle = IntPtr.Zero;
public static void testOverlay() {
if (windowHandle == IntPtr.Zero) {
windowHandle = getWindowHandle();
}
Graphics g = Graphics.FromHwnd(windowHandle);
g.FillRectangle(new SolidBrush(Color.White), 0, 0, 10000, 10000);
}
private static IntPtr getWindowHandle() {
foreach (Process proc in Process.GetProcesses()) {
if (proc.MainWindowTitle == WINDOW_TITLE) {
return proc.MainWindowHandle;
}
}
MessageBox.Show("Error: Unable to find window.");
return IntPtr.Zero;
}
}
}
I am not sure what I am doing wrong. I am writing a Minesweeper Solver and I am trying to overlay graphics on the Minesweeper window to provide debugging information. Unfortunately, it doesn't seem to work at all as I do not see any change on my screen. I am looping 1000000000 times in Program.cs just in case it erases my overlay on every frame refresh. I prefer to not try to hook DirectX.

How do I capture the Screensaver Event?

I'm trying to capture a Screensaver event, however when I run my application it throws the following exception;
Unable to find an entry point named 'SystemParametersinfo' in DLL 'user32.dll'.
this is my code so far, any and all help would be greatly appreciated =D
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NLog;
using Topshelf;
using OsWatch;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace NotifyIcon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersinfo(int uAction, int uParam, ref int ipvParam, int fuWinini);
const int SPI_GETSCREENSAVERRUNNING = 114;
static int screenSaverRunning = -1;
int ok = SystemParametersinfo(SPI_GETSCREENSAVERRUNNING, 0, ref screenSaverRunning, 0);
private void ScreenSaver()
{
if (ok == 0)
{
Logger.Trace("SCREENSAVER OFF");
}
if (screenSaverRunning != 0)
{
Logger.Trace("SCREENSAVER ON");
}
}

Categories

Resources