13,000,179 members (73,237 online)
Rate this:
See more:
Hello Guy please i need your help, i get some Homework at school to create a relation between the Server and the Client using Interface.
The Client need only to give one value and the server will get this value to calculate the double Integral of the Function F(x)= X² + Y² and send the Result to
the Client.This part run perfectly. Look at the Code:
Server
```using System;
using System.Collections.Generic;
using System.Text;
using InterfaceIntegral;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
namespace Server
{
public class Program
{
static void Main(string[] args)
{
TcpChannel chan = new TcpChannel(4711);
Console.WriteLine("*                    Server Aktiviert                                *");           Console.WriteLine("**********************************************************************");
}
}
{
private double faktor = Math.Sqrt(3.0 / 5.0);
public double integral(double anf, double end, int N)
{
double epsilon = 0.0d;
double flacheninhalt = 0.0d;
double h = (end - anf) / ((double)N * 2.0);
epsilon = anf + h;
for (int j = 0; j < N; j++)
{
flacheninhalt += 5.0 * this.MyFunktion(epsilon - h * faktor, N);
flacheninhalt += 8.0 * this.MyFunktion(epsilon, N);
flacheninhalt += 5.0 * this.MyFunktion(epsilon + h * faktor, N);
epsilon = anf + (2.0 * (double)j + 1.0) * h;
}
flacheninhalt *= h / 9.0;
return flacheninhalt;
}
private double MyFunktion(double x, double N)
{
double epsilon, erg = 0.0d;
double end = Math.Sqrt(1.0 - Math.Pow(x, 2.0));
double anf = -end;
int j;
IFunction function = (IFunction)Activator.GetObject(typeof(IFunction), "tcp://localhost:4712/Function");
double h = (end - anf) / ((double)N * 2.0);
epsilon = anf + h;
for (j = 0; j < N; j++)
{
erg += 5.0 * function.func(x, (epsilon - h * faktor));
erg += 8.0 * function.func(x, epsilon);
erg += 5.0 * function.func(x, (epsilon + h * faktor));
epsilon = anf + (2.0 * (double)j + 1.0) * h;
}
erg *= h / 9.0;
return erg;
}
}
}```

Client
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InterfaceIntegral;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
namespace Client
{
class Program
{
static void Main(string[] args)
{
TcpChannel chan = new TcpChannel(4712);
ChannelServices.RegisterChannel(chan, true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Function), "Function", WellKnownObjectMode.Singleton);
Console.WriteLine("\n");
try
{
int variableN = 0;
string temp;
do
{
Console.WriteLine("Geben Sie eine Wert fuer N ein\n");
Console.Write("N:\t");
if (temp == "0")
{
Console.WriteLine("Falsche Eingabe Bitte N soll größer als 0 sein \n");
}
try
{
variableN = Convert.ToInt32(temp);
}
catch (Exception E)
{
Console.WriteLine(E.Message);
Console.WriteLine("falsche eingabe");
}

} while (!(variableN >=1));
double ergebnis = gaussQ.integral(-1, 1, variableN);
Console.WriteLine("\nIntegration in die Grenzen [{0}, {1}]:\nErgebnis: {2}", -1, 1, ergebnis);
}
catch (Exception error)
{
Console.WriteLine(" \n{0}\n",error.Message);
}
}
}
public class Function : MarshalByRefObject, IFunction
{
public double func(double x, double y)
{
return x * x + y * y;
}
}
}```

Interface
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceIntegral
{
public interface IFunction
{
double func(double x, double y);
}
{
double integral(double x1, double x2, int N);
}
}```

I just have a Problem now I want that when i Close a Client-Console that the Server-Console will automatically and simultaneously close Please can u help me I don't have any Idea how to proceed.
Please note zhat each part is a different Project.
Thx
Posted 19-Jan-13 12:35pm

Rate this:

## Solution 1

This would be simple enough, but…

Why would you need to have a server console at all? Also, by the nature of a service, it's perfectly fine to have it executing "forever".

After all, this is not a "real" service anyway. The "real" service could be the one which is made as a Windows Service. Then it will execute hosted by the service controller which will also be able to stop the service; and you will only need to handle the service stop event, in case you need to perform some post-mortal action. In this approach, your service will keep executing when the users log on and off, and will start before anyone logs on. On Windows, that is a civilized way to proceed.

So, first thing you need to think about is the sense of such console use and closing. Maybe, temporary, research or experimental character of your work will not justify this complication.

However, if you still want to develop some home-baked approach to "server stop", you should remember, that in your application, you are supposed to use not just TCP (or any transport) it its pure form and some ad-hoc use, but you are supposed to create some application-level protocol: http://en.wikipedia.org/wiki/Application_layer[^].

In fact, you always have such protocol de-facto, even if you don't just call it a protocol.

When you remember that, you will see that you can have some "application" messages, as well as "control" messages; and one of them will be the "stop the service" message. Another approach could be counting all the clients on server side. At first, the number of the client is 0, then it grows, but as soon as it becomes 0 again, the server terminates.

However, to do things properly, you should always use threading. On the server side, you need at least two threads: one listening and accepting the newly connected clients, and another one is implementing the application-level protocol by reading/writing from/to the network streams or sockets. (And I strongly advise to use async APIs; threads are way more straightforward, more importantly, thread synchronization is not application-specific, so you use the same approach without a need to review it per application.)

You can find further ideas in my past answer:
Multple clients from same port Number[^].

—SA
stefan from germany 19-Jan-13 20:36pm

thank you Sergey it's helpfull. but i have another Question please. When i run the server and the cleint, then when i stop the client and start it again i receive a wrting failure, interne exception. I need to stop the client again and start it one more time it run successfully. how can i fix this Problem? that why i try to sychronize the synchronisation closing of Console.
thx.
Sergey Alexandrovich Kryukov 19-Jan-13 21:40pm

From what I ever considered (and that was a lot), the best approach is to wrap the main loop in try-catch (inside loop), and re-connect again on all failures. Also, this is good method of handling of client's non-graceful disconnection on server side. Again, read my past answer, I explain it. As a client is not supposed to guarantee 100% fault-tolerance, in principle, no need to take care of graceful disconnection: it could happen by the fault or by user.

However, if you have some regular unexplained failure, you should not ignore it. Always debug it, well, using the debugger.

Does it makes sense for you?
—SA

Top Experts
Last 24hrsThis month
 RickZeeland 104 OriginalGriff 60 ppolymorphe 45 Michael_Davies 40 Richard Deeming 40
 OriginalGriff 4,597 ppolymorphe 2,292 RickZeeland 2,172 Kornfeld Eliyahu Peter 1,993 Richard Deeming 1,593