如何关闭客户端控制台和服务器控制台自动关闭。 [英] How to close a Client-Console and Server-Console close automaticaly.

查看:76
本文介绍了如何关闭客户端控制台和服务器控制台自动关闭。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好Guy我需要您的帮助,我在学校接受一些家庭作业,使用界面在服务器和客户端之间建立关系。

客户端只需提供一个值,服务器将需要得到这个值来计算函数F(x)=X²+Y²的双积分,并将结果发送给客户。
客户端。这部分运行完美。查看代码:

服务器

 使用系统; 
使用 System.Collections.Generic;
使用 System.Text;
使用 InterfaceIntegral;
使用 System.Runtime.Remoting.Channels.Tcp;
使用 System.Runtime.Remoting.Channels;
使用 System.Runtime.Remoting;
命名空间服务器
{
public class 计划
{
静态 void Main ( string [] args)
{
TcpChannel chan = new TcpChannel(< span class =code-digit> 4711 );
ChannelServices.RegisterChannel(chan, true ); RemotingConfiguration.RegisterWellKnownServiceType( typeof (GaussQuadratur), GaussQuadratur ,WellKnownObjectMode.Singleton); Console.WriteLine( ********************* ******************* );
Console.WriteLine( * Server Aktiviert *); Console.WriteLine( ********************* ******************* );
Console.ReadKey();
}
}
public class GaussQuadratur:MarshalByRefObject,IQuadratur
{
private double faktor = Math.Sqrt( 3 0 / 5 0 );
public double 整数( 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;
}
私有 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://本地主机:4712 /功能);
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);
尔格+ = 5 0 * function.func(x,(epsilon + h *) FAKTOR));
epsilon = anf +( 2 0 *( double )j + 1 0 )* h;
}
erg * = h / 9 0 ;
return erg;
}
}
}



客户

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 InterfaceIntegral;
使用 System.Runtime.Remoting.Channels.Tcp;
使用 System.Runtime.Remoting;
使用 System.Runtime.Remoting.Channels;
命名空间客户
{
class 计划
{
静态 void Main( string [] args)
{
TcpChannel chan = new TcpChannel( 4712 ) ;
ChannelServices.RegisterChannel(chan, true );
RemotingConfiguration.RegisterWellKnownServiceType( typeof (Function), Function,WellKnownObjectMode.Singleton);
IQuadratur gaussQ =(IQuadratur)Activator.GetObject( typeof (IQuadratur), TCP://本地主机:4711 / GaussQuadratur);
Console.WriteLine( \ n);
尝试
{
int variableN = 0 ;
string temp;
do
{
Console.WriteLine( Geben Sie eine Wert fuer N ein \ n);
Console.Write( N:\t);
temp = Console.ReadLine();
if (temp == 0
{
Console.WriteLine( Falsche Eingabe Bitte Nsollgrößersals 0 sein \\\
);
}
尝试
{
variableN = Convert.ToInt32(temp);
}
catch (例外E)
{
Console.WriteLine(E.Message);
Console.WriteLine( falsche eingabe);
}

} while (!(variableN > = 1 ));
double ergebnis = gaussQ.integral(-1, 1 ,variableN);
Console.WriteLine( \ n在Grenzen [{0},{1}]中的集成:\\ \\ nErgegnis:{2},-1, 1 ,ergebnis);
}
catch (异常错误)
{
Console.WriteLine( \ n {0} \ n,error.Message);
}
}
}
public class 功能:MarshalByRefObject,IFunction
{
public double func( double x, double y)
{
return x * x + y * y;
}
}
}



界面

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
命名空间 InterfaceIntegral
{
public interface IFunction
{
double func( double x, double y);
}
public interface IQuadratur
{
< span class =code-keyword> double integral( double x1, double x2, int N);
}
}



我现在遇到问题当我关闭服务器控制台将关闭客户端控制台时我想要自动同时关闭请帮助我,我没有任何想法如何继续。

请注意每个部分是不同的项目。

Thx

解决方案

这很简单,但是......



为什么你需要一个服务器控制台呢?此外,根据服务的性质,让它永远执行是完全没问题的。



毕竟,这不是一个真正的服务无论如何。 真正的服务可以是作为Windows服务的服务。然后它将由服务控制器执行,该服务控制器也将能够停止服务;并且您只需要处理服务停止事件,以防您需要执行一些后期操作。在这种方法中,您的服务将在用户登录和注销时继续执行,并将在任何人登录之前启动。在Windows上,这是一种文明的进行方式。



所以,首先要考虑的是这种控制台使用和关闭的感觉。也许,你的工作的临时性,研究性或实验性不会证明这种复杂性是合理的。



但是,如果你还想开发一些自制的方法来停止服务器 ,你应该记住,在你的应用程序中,你不应该只使用TCP(或任何传输)它的纯粹形式和一些临时使用,但你应该创建一些应用程序级协议 http://en.wikipedia.org/wiki/Application_layer [ ^ ]。



实际上,你总是有这样的协议事实,即使你不只是把它称为协议。



当你记得那个,你会发现你可以有一些应用程序消息,以及控制消息;其中一个将是停止服务的消息。另一种方法可能是计算服务器端的所有客户端。首先,客户端的数量是0,然后它会增长,但是一旦它再次变为0,服务器就会终止。



然而,要正确地做事情,你应该总是使用线程。在服务器端,您至少需要两个线程:一个是侦听和接受新连接的客户端,另一个是通过从/向网络流或套接字读取/写入来实现应用程序级协议。 (我强烈建议使用异步API;线程更直接,更重要的是,线程同步不是特定于应用程序的,因此您使用相同的方法而无需根据应用程序进行审查。)



您可以在我过去的答案中找到更多想法:

来自同一端口号码的多个客户端 [ ^ ]。



-SA

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);
                ChannelServices.RegisterChannel(chan, true);              RemotingConfiguration.RegisterWellKnownServiceType(typeof(GaussQuadratur), "GaussQuadratur", WellKnownObjectMode.Singleton);             Console.WriteLine("**********************************************************************");
                Console.WriteLine("*                    Server Aktiviert                                *");           Console.WriteLine("**********************************************************************");
                Console.ReadKey();
        }
    }
    public class GaussQuadratur : MarshalByRefObject, IQuadratur
    {
        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);
            IQuadratur gaussQ = (IQuadratur)Activator.GetObject(typeof(IQuadratur), "tcp://localhost:4711/GaussQuadratur");
            Console.WriteLine("\n");
            try
            {
                int variableN = 0;
                string temp;
                do
                {
                    Console.WriteLine("Geben Sie eine Wert fuer N ein\n");
                    Console.Write("N:\t");
                    temp = Console.ReadLine();
                    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);
    }
    public interface IQuadratur
    {
        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

解决方案

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


这篇关于如何关闭客户端控制台和服务器控制台自动关闭。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆