返回首页


{S0}简介
,我们有很多很多的服务器,在我的工作。企业应用程序,如SQL Server,Oracle等,安装在他们的系统管理员。但是,当我们(开发商)创建一个新的应用程序或一个新的组件,我们在每个服务器上部署新的程序集。我们不喜欢这样,我们很讨厌这种,所以我试图使一个应用程序,使这项任务对我们来说更容易...背景
有两个应用程序。他们都使用套接字相互沟通和转移的文件名和数据内。这些应用程序: ReceiveFiles
这是一个控制台应用程序监听新的要求。当一个请求发送到服务器,该应用程序得到它,将文件复制到所需​​的最终路径。这是不是一种Windows服务,是聆听所有的时间。稍后我会解释如何远程启动这个东西。SendFiles
这是一个Windows应用程序,一个小的图形用户界面(GUI),用于启动远程ReceiveFiles应用程序和发送文件到服务器。
PsExec的是一个Windows应用程序,你可以用它来启动远程进程。它是用来启动远程ReceiveFiles。在这里我可以不重视,因为它禁止。但是你可以免费下载,从这里:{A}。 如何使用它在服务器上,你要部署的文件复制ReceiveFiles。一点点运气,这将是最后一次,你将不得不复制自己什么。你必须下载微软的网页PsExec的,以远程启动ReceiveFiles。一旦它被下载,你必须复制相同的路径,安装SendFiles PsExec的填充的服务器名称,用户和密码的配置文件。启动SendFiles应用程序。在第一个文本框,quot;路径ReceiveFile是... ... quot;,你写的远程路径ReceiveFiles安装。建议在所有机器上的路径是相同的。选择所需的服务器,单击quot;启动远程listenersquot;启动远程ReceiveFiles应用程序。一旦远程应用程序开始(您可以检查它在MS - DOS窗口),你可以选择要复制的文件,通过点击的quot;获取Filesquot;按钮。选择你想要什么。点击quot;发送Filesquot;按钮,选中的文件复制到所有服务器。一旦这个过程完成后,您可以通过点击quot关闭远程应用程序;关闭远程listenersquot。
这就是全部... ...使用代码
的代码是在网上找到一些代码的修改;到目前为止,它的工作原理确定!
ReceiveFiles应用启动后,它会打开一个无限循环的两个插座,直到手动关闭应用程序或收到一个关闭请求。
插座是开放的ReceiveFilename方法。它会等待一个文件名和路径启动进程。当接收数据(文件名),它会创建路径的情况下,它不存在。后来,ReceiveFile方法将打开另一个套接字以等待的二进制数据,这是该文件的内容。创建文件后,它会等待任何其他请求。如果文件名是"关闭APPquot,应用程序将关闭,因为这是远程的方式来关闭应用程序。
的ReceiveFiles应用程序的核心是:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.IO;

using System.Threading;

namespace RecepcionFicherosSocket

{

    public class SocketsFileTransfer

    {

        private const string STRING_TO_CLOSE = "CLOSE APP";

        public void ReceiveFile(int port, string filepathandname)

        {

            string methodname = "ReceiveFile";

            try

            {

                IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, port);

                Socket sock = new Socket(AddressFamily.InterNetwork, 

                                  SocketType.Stream, ProtocolType.IP);

                sock.Bind(ipEnd);

                sock.Listen(1);

                Socket serverSocket = sock.Accept();

                byte[] data = new byte[1000000];

                int received = serverSocket.Receive(data);

                int filenameLength = BitConverter.ToInt32(data, 0);

                string filename = Encoding.ASCII.GetString(data, 4, filenameLength);

                this.CreateDirectoryFromPath(filepathandname);

                BinaryWriter bWrite = new BinaryWriter(

                              File.Open(filepathandname, FileMode.Create));

                bWrite.Write(data, filenameLength + 4, received - filenameLength - 4);

                int received2 = serverSocket.Receive(data);

                while (received2 > 0)

                {

                    bWrite.Write(data, 0, received2);

                    received2 = serverSocket.Receive(data);

                }

                bWrite.Close();

                serverSocket.Close();

                sock.Close();

                MyLogs.WriteLog(methodname, "File copied ok: " + 

                                filepathandname, false);

            }

            catch (Exception ex)

            {

                MyLogs.WriteLog(methodname, "Port: " + port + 

                                " file: " + filepathandname + 

                                " " + ex.ToString(), true);

            }

        }

        

        public string ReceiveFilename(IPAddress ipAddress, int port)

        {

            string filename = string.Empty;

            string methodname = "ReceiveFilename";

            try

            {

                

                TcpListener tcpListener = new TcpListener(ipAddress, port);

                //Starting the listening

                tcpListener.Start();

                //Wait for a client

                Socket socketForClient = tcpListener.AcceptSocket();

                if (socketForClient.Connected)

                {       

                    // If connected

                    MyLogs.WriteLog(methodname,"Connected",false);

                    NetworkStream networkStream = new NetworkStream(socketForClient);

                    StreamWriter streamWriter = new StreamWriter(networkStream);

                    StreamReader streamReader = new StreamReader(networkStream);            



                    string theString = "Ok so far";

                    //Wait for the client request

                    filename = streamReader.ReadLine();

                    MyLogs.WriteLog(methodname,theString + 

                                    " " + filename,false);

                    //Answering the client

                    streamWriter.WriteLine(theString);

                    streamWriter.Flush();

                    //Close

                    streamReader.Close();

                    streamWriter.Close();

                    networkStream.Close();

                    socketForClient.Close();

                    tcpListener.Stop();

                    //If the client has requested the close of the server

                    if (filename == STRING_TO_CLOSE)

                    {

                        MyLogs.WriteLog(methodname, "Closing requested", false);

                        Environment.Exit(999);

                    }

                }

            }

            catch (Exception ex)

            {

                MyLogs.WriteLog("ReceiveFilename", ex.ToString(), true);

            }

            return filename;

        }

        public IPAddress GetIpFromHostName(string servername)

        {

            IPAddress ip = null;

            String strHostName = string.Empty;

            if (servername == string.Empty)

            {

                strHostName = Dns.GetHostName();

                MyLogs.WriteLog("GetIpFromHostName", 

                       "Machine name: " + strHostName,false);

            }

            else

            {

                strHostName = servername;

            }

            // Then using host name, get the IP address list..

            IPHostEntry ipEntry = Dns.GetHostByName(strHostName);

            IPAddress[] addr = ipEntry.AddressList;

            ip = addr[0];

            return ip;

        }

        private void CreateDirectoryFromPath(string path)

        {

            string directoryPath = System.IO.Path.GetDirectoryName(path);

            if (Directory.Exists(directoryPath) == false)

            {

                try

                {

                    Directory.CreateDirectory(directoryPath);

                    MyLogs.WriteLog("CreateDirectoryFromPath", 

                                    "Directory: " + directoryPath + 

                                    " created", false);

                }

                catch (Exception ex)

                {

                    MyLogs.WriteLog("CreateDirectoryFromPath", 

                          "Cant create directory for: " + path + 

                          " " + ex.ToString(), true);

                }

            }

        }

    }

}

的ReceiveFiles应用程序的主要点的应用程序不停止执行,直到它关闭或收到一个结束请求。
的ReceiveFiles应用程序的主要观点是:{C}
SendFiles应用程序使用两个插座(如ReceiveFiles相同的号码)发送文件名和文件中的数据(二进制)。首先,将文件名发送到远程应用程序,并等待一个ReceiveFile确认。已经到来,一旦确认该文件以二进制模式打开,读取数据,并通过另一个套接字发送到ReceiveFiles。
的SendFiles应用程序的核心是:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.IO;

using System.Threading;

namespace SendFiles

{

    public class SocketsFileTransfer

    {

        private const string STRING_TO_CLOSE = "CLOSE APP";

        public void SendFileName(string server, int port, string filename)

        {

            TcpClient socketForServer;

            try

            {

                //Creamos un TcpCliente y le pasamos el server y el puerto.

                socketForServer = new TcpClient(server, port);

            }

            catch (Exception ex)

            {

                Console.WriteLine("No se pudo conectar a " + port + 

                                  ":" + server + " " + ex.ToString());

                return;

            }//aqui es lo mismo que en el server. Usamos StreamWriter y Reader.

            NetworkStream networkStream = socketForServer.GetStream();

            StreamReader streamReader = new System.IO.StreamReader(networkStream);

            StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);

            try

            {

                streamWriter.WriteLine(filename);

                streamWriter.Flush();

                string outputString = streamReader.ReadLine();

                Console.WriteLine(outputString);

            }

            catch

            {

                Console.WriteLine("Exception reading from Server");

            }

            finally

            {

                networkStream.Close();

            }

        }

        public void SendFile(IPAddress ipAddress, int port, string filenameToUse)

        {

            //send file:

            IPEndPoint ipEnd = new IPEndPoint(ipAddress, port);

            Socket clientSocket = new Socket(AddressFamily.InterNetwork, 

                                             SocketType.Stream, ProtocolType.IP);

            filenameToUse = filenameToUse.Replace("\\", "/");

            byte[] filenameData = Encoding.ASCII.GetBytes(filenameToUse);

            byte[] output = new byte[4 + filenameData.Length];

            BitConverter.GetBytes(filenameData.Length).CopyTo(output, 0);

            filenameData.CopyTo(output, 4);

            clientSocket.Connect(ipEnd);

            clientSocket.SendFile(filenameToUse, output, null, 

                                  TransmitFileOptions.UseDefaultWorkerThread);

            clientSocket.Close();

        }

        public IPAddress GetIpFromHostName(string servername)

        {

            IPAddress ip = null;

            String strHostName = string.Empty;

            if (servername == string.Empty)

            {

                strHostName = Dns.GetHostName();

                MyLogs.WriteLog("GetIpFromHostName", 

                                "Machine name: " + strHostName, false);

            }

            else

            {

                strHostName = servername;

            }

            // Then using host name, get the IP address list..

            IPHostEntry ipEntry = Dns.GetHostByName(strHostName);

            IPAddress[] addr = ipEntry.AddressList;

            ip = addr[0];

            return ip;

        }

    }

}

这将是很烦人的开始ReceiveFiles远程应用服务器,由服务器发送文件之前,手动。所以,我使用微软PsExec的应用远程启动ReceiveFiles。基本上,我使用Process类启动PsExec的,并通过必要的命令参数来启动远程服务器ReceiveFiles。
Send方法循环每一个文件,并调用SendFileName和发送文件,以数据传输。
Close方法发送一个特定的消息,作为一个文件名("关闭APPquot;)要求ReceiveFiles的应用程序关闭; PsExec的是这里没有必要,因为以前的通信已建立两者之间的应用。
现在让我们来看看如何启动ReceiveFiles应用程序,远程传送文件,然后关闭应用程序的ReceiveFiles:
启动应用程序:
private void btnStartRemoteListeners_Click(object sender, RoutedEventArgs e)

{

    bool bcontinue = true;

    bool wasanyerror = false;

    string psexecPath = System.Windows.Forms.Application.StartupPath + "\\psexec.exe</a />";

    string remoteExecutablePath = txtPathWhereReceiveFilesIs.Text;

    string parameters = string.Empty;

    bool showFinalMessage = true;

    progressBar1.Maximum = lstServers.SelectedItems.Count;

    progressBar1.Value = 0;

    txtLog.Text = String.Empty;

    if (ValidateStart()==true)

    {

        foreach (string serverName in lstServers.SelectedItems)

        {

            Server server = null;

            if (ServersList.TryGetValue(serverName, out server) == true)

            {

                server.User = server.User == "" ? "Error" : server.User;

                server.Password = server.Password == "" ? "Error" : server.Password;

                try

                {

                    if (bcontinue == true)

                    {

                        if (server.MyProcess == null)

                        {

                            Process p = new Process();

                            ProcessStartInfo psinfo = new ProcessStartInfo(psexecPath);

                            parameters = @"\\" + server.ServerName + 

                               " -u " + server.User + " -p " + 

                               server.Password + " -i 0 " + remoteExecutablePath;

                            psinfo.Arguments = parameters;

                            txtLog.AppendText(psexecPath + " " + 

                               parameters + Environment.NewLine);

                            psinfo.CreateNoWindow = true;

                            psinfo.WindowStyle = ProcessWindowStyle.Minimized;

                            p = Process.Start(psinfo);

                            server.MyProcess = p;

                            System.Threading.Thread.Sleep(1000);

                            if (p.HasExited == true)

                            {

                                server.MyProcess = null;

                                throw new Exception();

                            }

                        }

                        else

                        {

                            System.Windows.Forms.MessageBox.Show("The process " + 

                              "is already running at the server: " + 

                              server.ServerName, "Start Listener", 

                              MessageBoxButtons.OK, MessageBoxIcon.Information);

                            showFinalMessage = false;

                        }

                    }

                }

                catch (Exception ex)

                {

                    string msg = "Error starting the remote listener at " + 

                                 server + Environment.NewLine + ex.Message + 

                                 Environment.NewLine + "Path: " + 

                                 psexecPath + Environment.NewLine +

                                 "Parameters: " + parameters + Environment.NewLine +

                                 "Maybe the remote path does not exists, " + 

                                 "the password is incorrect or the remote " + 

                                 "listener is still running";

                    txtLog.AppendText(msg + Environment.NewLine);

                    System.Windows.Forms.MessageBox.Show(msg, "Error", 

                           MessageBoxButtons.OK, MessageBoxIcon.Error);

                    wasanyerror = true;

                    if (System.Windows.Forms.MessageBox.Show(

                        "Do you want to continue?", 

                        "Continue", MessageBoxButtons.YesNo, 

                        MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)

                    {

                        bcontinue = false;

                    }

                }

                progressBar1.Value++;

            }

        }

        if (showFinalMessage == true)

        {

            if (wasanyerror == false)

            {

                System.Windows.Forms.MessageBox.Show(

                  "Remote listeners started ok", "Ok", 

                  MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            else

            {

                System.Windows.Forms.MessageBox.Show(

                  "Remote listener started with errors", 

                  "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

    }

}

复制文件:
private void btnSendFiles_Click(object sender, RoutedEventArgs e)

{

    SocketsFileTransfer socketsFileTransfer = new SocketsFileTransfer();

    int filenamePort = 9999;  //port number for the file name

    int filedataPort = 9998;  //port number for the file data

    string serversWithError = string.Empty;

    bool bcontinue = true;

    bool wasanerror = false;

    if (this.ValidateSending() == true)

    {

        this.Cursor = System.Windows.Input.Cursors.Wait;

        progressBar1.Maximum = lstServers.SelectedItems.Count;

        progressBar1.Value = 0;

        txtLog.Text = String.Empty;

        foreach (string serverName in lstServers.SelectedItems)

        {

            //Get the IP of the remote server

            System.Net.IPAddress ipAddress = 

              socketsFileTransfer.GetIpFromHostName(serverName);

            txtLog.AppendText(serverName + " ip: " + 

              ipAddress.ToString() + Environment.NewLine);

            foreach (string filePath in filesToSend)

            {

                string fileName = System.IO.Path.GetFileName(filePath);

                string finalPath = cboRemotePaths.Text + "\\" + fileName;

                txtLog.AppendText("Uploading " + fileName + 



                  " to " + serverName + "::" + 

                  finalPath + Environment.NewLine);

                try

                {

                    if (bcontinue == true)

                    {

                        //Send the file name

                        socketsFileTransfer.SendFileName(serverName, filenamePort, finalPath);

                        txtLog.AppendText("File name sent" + Environment.NewLine);

                        //Wait

                        System.Threading.Thread.Sleep(250);

                        //Send the file data

                        socketsFileTransfer.SendFile(ipAddress, filedataPort, filePath);

                        txtLog.AppendText("Data sent" + Environment.NewLine);

                    }

                }

                catch (Exception ex)

                {

                    string msg = "Error sending: " + fileName + 

                      " to " + serverName + Environment.NewLine + ex.Message;

                    txtLog.AppendText(msg);

                    System.Windows.Forms.MessageBox.Show(msg, "Error at " + 

                      serverName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    wasanerror = true;

                    serversWithError += serverName + Environment.NewLine;

                    if (System.Windows.Forms.MessageBox.Show("Do you want " + 

                        "to continue with the send process?", "Continue", 

                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == 

                        System.Windows.Forms.DialogResult.No)

                    {

                        bcontinue = false;

                    }

                }

            }

            progressBar1.Value++;

            System.Windows.Forms.Application.DoEvents();

        }

        if (wasanerror == false)

        {

            System.Windows.Forms.MessageBox.Show("Process finished ok", 

                   "Ok", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        else

        {

            System.Windows.Forms.MessageBox.Show("Process finished " + 

              "with errors at: " + Environment.NewLine + serversWithError, 

              "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        this.Cursor = System.Windows.Input.Cursors.Arrow;

    }

}

关闭应用程序:
private void btnCloseRemoteListeners_Click(object sender, RoutedEventArgs e)

{

    bool bcontinue = true;

    bool wasanyerror = false;



    progressBar1.Maximum = lstServers.SelectedItems.Count;

    progressBar1.Value = 0;

    txtLog.Text = String.Empty;

    foreach (string serverName in lstServers.SelectedItems)

    {

        if (bcontinue == true)

        {

            if (this.CloseServer(serverName) == false)

            {

                wasanyerror = true;

                if (System.Windows.Forms.MessageBox.Show(

                    "Do you want to close the remote listeners?", 

                    "Continue", MessageBoxButtons.YesNo, 

                    MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)

                {

                    bcontinue = false;

                }

            }

        }

        progressBar1.Value++;

    }

    if (wasanyerror == false)

    {

        System.Windows.Forms.MessageBox.Show("Remote listeners closed", 

           "Ok", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

    else

    {

        System.Windows.Forms.MessageBox.Show(

          "Remote listeners closed with errors", 

          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

}
配置文件
在这个文件中,您可以指定将工作与您的服务器名称,域,用户名,和密码。这个数据是在左侧SendFiles形式列表中所示:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <Server>

    <ServerName>machine1.domain.xxx</ServerName>

    <User>domain\user</User>

    <Password>password</Password>

  </Server>

  <Server>

    <ServerName>machine2.domain.xxx</ServerName>

    <User>domain\user</User>

    <Password>password</Password>

  </Server>

</configuration>
兴趣点
不要毫不犹豫地发表评论,反正你想要的。我在这里学习。历史版本1.0。

回答

评论会员:qpon 时间:2012/01/25

我很感兴趣,更改发送的文件远程路径。要做到这一点,我发现在Window1.xaml.cs文件
以下部分
私人无效LoadRemotePaths()
{
/ /也许从另一个配置文件。
cboRemotePaths.Items.Add("C:\ \ MyRemotePath \ \ Test1的");
cboRemotePaths.Items.Add("C:\ \ MyRemotePath \ \ TEST2");
cboRemotePaths.Items.Add("C:\ \ MyRemotePath \ \ TEST3");

}

我已经更改为:

私人无效LoadRemotePaths()
{
/ /也许从另一个配置文件。 cboRemotePaths.Items.Add("D:\ \ NewPath");}

从那以后,我重新启动应用程序,但在选择领域,但我仍然认为自己的intead这些预定义的路径!你能帮助我这?

关于
雅各布
评论会员:圣地亚哥桑切斯 时间:2012/01/25
您好,
你的代码对我的作品是完美的! 有你试图重建项目?你能检查,如果文件在只读模式?

关于
评论会员:qpon 时间:2012/01/25

我怎样才能重建项目?我不是一个程序员,所以请给一些提示的所有文件在读取模式。

/ Jakub
评论会员:圣地亚哥桑切斯 时间:2012/01/25
您好,

首先,从Windows浏览器,你必须按一下项目文件夹,点击鼠标右键看属性,并取消选中"只读"复选框。所以,在项目中做的每一个变化将被保存。

重建项目,你有可能的方式,按F6(当VS2008中打开)键或右单击项目资源管理器并选择"重建项目"

希望有帮助。

关于
评论会员:Srinath摹纳特 时间:2012/01/25
好文章

访问我的网站:

{A3}
评论会员:圣地亚哥桑切斯 时间:2012/01/25
!非常感谢
评论会员:萨沙理发 时间:2012/01/25
呀看起来好,我公之于众,并赋予它一个4

萨沙理发
微软的Visual C#MVP 2008/2009Codeproject MVP 2008/2009Your最好的朋友就是你。
我是我最好的朋友。我们有着共同的看法,并未落认为

我的博客:{A4}
评论会员:圣地亚哥桑切斯 时间:2012/01/25
谢谢你!