如何从服务器创建客户端应用程序?

问题描述:

您好,我有一个TCP客户端/服务器解决方案。



现在,我的客户端成功连接到我的服务器但是想从生成客户端服务器并传递给它一些参数(变量)。



客户端配置在服务器上完成,所以我想从服务器表单生成客户端(可能带一个按钮)以便通过这里选择的选项。



如果您对该请求有任何疑问,请写下来,我会尽力尽可能清楚。



谢谢。



我的尝试:



我在互联网上搜索了一个可能的解决方案,但遗憾的是我找不到任何可以提供帮助的东西。



无论如何这是我的服务器表单应用程序:



Hello, i have a TCP Client / Server solution.

Now, my clients connect successfully to my server but would like to "generate" the client from the Server and pass to it some "parameters"(variables).

The client configuration is done on the server and so I would like to generate the client from the server form (maybe with a button) in order to pass the options chosen here.

If you have any doubts about the request, please write it down and I will try to be as clear as possible.

Thank you.

What I have tried:

I searched on the internet for a possible solution but unfortunately i did not find anything that could help.

Anyway this is my server form application:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Server
{
    public partial class Form1 : Form
    {

        private static Socket serverSocket;
        private static List<Socket> clientSocket = new List<Socket>();

        private const int buff_size = 2000000;
        private static byte[] buff = new byte[buff_size];

        bool button_set_port_clicked;

        //VALUES TO PASS TO THE CLIENT (GENERATED ON THE FORM APPLICATION WITH 3 TEXTBOX)

        private static String FtpAddress = "";
        private static String FtpUser = "";
        private static String FtpPass = "";

        //-----------------------------------


        private void SetupServer(int port)
        {

            if (button_set_port_clicked)
            {
                label5.Text = textPort.Text;
            }

            if(textFtpAdd.Text == "..." || textFtpUser.Text == "..." || textFtpPass.Text == "...")
            {

                MessageBox.Show("FTP Configuration Incomplete!");
                Environment.Exit(1);

            }

            label1.Text = "Starting server on port " + port;

            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(IPAddress.Any, port));
            serverSocket.Listen(5);
            serverSocket.BeginAccept(AcceptCallBack, null);

            label1.Text = "Server running on port " + port;
        }

        private void AcceptCallBack(IAsyncResult AR)
        {
            Socket socket;

            try
            {
                socket = serverSocket.EndAccept(AR);
            }
            catch (ObjectDisposedException)
            {
                return;
            }

            clientSocket.Add(socket);

            socket.BeginReceive(buff, 0, buff_size, SocketFlags.None, ReceiveCallBack, socket);

            serverSocket.BeginAccept(AcceptCallBack, null);

        }

        private void ReceiveCallBack(IAsyncResult AR)
        {
            Socket current = (Socket)AR.AsyncState;

            int received;

            try
            {
                received = current.EndReceive(AR);
            }
            catch (SocketException)
            {
                Console.WriteLine("Client disconnesso!");
                current.Close();

                clientSocket.Remove(current);
                return;
            }

            byte[] recbuf = new byte[received];
            Array.Copy(buff, recbuf, received);

            String text = Encoding.ASCII.GetString(recbuf);

            current.BeginReceive(buff, 0, buff_size, SocketFlags.None, ReceiveCallBack, current);

        }

        /*

        private void CloseAllSockets()
        {
            foreach (Socket socket in clientSocket)
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }

            serverSocket.Close();
        }

        private void ListClients()
        {
            int i = 0;

            foreach (Socket socket in clientSocket)
            {
                Console.WriteLine("Server " + i + "\n");
                i = i + 1;
            }
        }

        */

        private void LocalIP()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());

            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    String IP = ip.ToString();
                    textLocal.Text = IP;
                }
            }

        }


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LocalIP();

            textLocal.ReadOnly = true;
            textDNS.ReadOnly = true;
            textPort.ReadOnly = true;
            textCheckPort.ReadOnly = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int port = 4444;

            if (button_set_port_clicked)
            {
                String _port = textPort.Text;

                Int32.TryParse(_port, out port);

                SetupServer(port);
            }

            SetupServer(port); 
        }

        private void radioLocal_CheckedChanged(object sender, EventArgs e)
        {
            textPort.ReadOnly = false;

            textDNS.ReadOnly = true;
            textDNS.Text = "DNS Address";

            textLocal.ReadOnly = true;
        }

        private void radioDNS_CheckedChanged(object sender, EventArgs e)
        {
            textDNS.ReadOnly = false;
            textDNS.Text = "";

            textPort.ReadOnly = false;
            textPort.Text = "4444";

            textLocal.ReadOnly = true;
           
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void button_check_port_Click(object sender, EventArgs e)
        {

        }

        private void button_set_port_Click(object sender, EventArgs e)
        {
            button_set_port_clicked = true; //PORTA SETTATA

            label5.Text = textPort.Text;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
        }
    }
}

为了在远程机器上生成客户端,远程机器需要轮询服务器以获取配置数据,然后生成。



如果没有首先在远程计算机上运行某种代理,则无法在远程计算机上启动进程(通过远程调用) 连接到服务器(除非客户端本身可以充当服务器;相同的区别)。
In order to "spawn" clients on a "remote" machine, the "remote" machine needs to "poll" the server for config data and then spawn.

It is impossible to start a process on a remote machine (via a "remote call") without some kind of "agent" running on the remote machine that has first "connected" to the server (unless the "client" itself can act as a server; same difference).