【问题标题】:Client_Server two .cs in one project issueClient_Server 两个 .cs 在一个项目问题
【发布时间】:2014-12-19 07:21:09
【问题描述】:

我必须开始为我的应用程序使用客户端服务器通信。首先,我想连接到本地主机。

代码如下:

服务器

public class serv 
{
    public static void Main() 
    {
        try 
        {
            IPAddress ipAd = IPAddress.Parse("127.0.0.1"); //use local m/c IP address, and use the same    in the client

            /* Initializes the Listener */
            TcpListener myList=new TcpListener(ipAd,1025);

            /* Start Listeneting at the specified port */       
            myList.Start();

            Console.WriteLine("The server is running at port 1025..."); 
            Console.WriteLine("The local End point is  :" + myList.LocalEndpoint );
            Console.WriteLine("Waiting for a connection.....");

            Socket s=myList.AcceptSocket();
            Console.WriteLine("Connection accepted from "+s.RemoteEndPoint);

            byte[] b=new byte[100];
            int k=s.Receive(b);
            Console.WriteLine("Recieved...");
            for (int i=0;i<k;i++)
                Console.Write(Convert.ToChar(b[i]));

            ASCIIEncoding asen=new ASCIIEncoding();
            s.Send(asen.GetBytes("The string was recieved by the server."));
            Console.WriteLine("\nSent Acknowledgement");

            /* clean up */          
            s.Close();
            myList.Stop();
            Console.ReadKey();
        }
        catch (Exception e) 
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }   
    }
}

客户

public class clnt 
{
    public static void Main() 
    {
        try 
        {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");

            tcpclnt.Connect("127.0.0.1",1025); // use the ipaddress as in the server program

            Console.WriteLine("Connected");
            Console.Write("Enter the string to be transmitted : ");

            String str=Console.ReadLine();
            Stream stm = tcpclnt.GetStream();

            ASCIIEncoding asen= new ASCIIEncoding();
            byte[] ba=asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");

            stm.Write(ba,0,ba.Length);

            byte[] bb=new byte[100];
            int k=stm.Read(bb,0,100);

            for (int i=0;i<k;i++)
                Console.Write(Convert.ToChar(bb[i]));

            tcpclnt.Close();
            Console.ReadKey();
        }
        catch (Exception e) 
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }
}

该项目有两个Main() 函数。因此,为了避免冲突,我将serv.cs 设置为StartupObject,但导致无法访问客户端的控制台窗口以发送消息。

1).如何在本地主机上使用/运行此类程序?

我实际上需要一个良好的起点来使用 Sockets,但网络上可用的大多数应用程序要么已经过时,要么更高级。我已经使用 Linux 处理过 Sockets,但对这个环境还是陌生的。

2).除此之外还有什么好的例子吗?

我已经搜索了很多,但这是我最后的希望!CodeProject 上的项目正在使用 UI,并且需要一个简单的控制台应用程序来启动。

【问题讨论】:

  • 您需要在一个解决方案中创建两个项目。一个项目应该包含服务器。另一个项目应该包含客户端。
  • 您应该检查 WCF 服务。我不确定您的程序要达到的目的是什么,但是从您的代码看来,这正是您所需要的,因为它让您可以通过网络调用方法,并且基本上可以在低端为您完成所有工作(不一定) 一些配置的价格。我建议您在继续目前的方法之前先阅读一下。
  • 我的应用是本地网络上的文件共享@Phoenix

标签: c# winforms wcf sockets client-server


【解决方案1】:

除了您的代码之外,没有必要。 你开始这两个项目了吗? 您必须先启动服务器,然后再启动客户端,这样客户端才能连接到等待的服务器。

【讨论】:

  • 是否有任何方法可以在整个操作过程中保持两个控制台窗口打开,即服务器运行后显示的控制台仍然在客户端运行之后,但使用带有客户端运行的新控制台?
猜你喜欢
  • 2013-05-07
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 2010-10-05
  • 1970-01-01
  • 2020-06-16
相关资源
最近更新 更多