多个文件发送到服务器

时间:2011-05-11 09:06:30

标签: c# windows file service transfer

我制作了一个Windows服务,其背后的动机是将多个文件发送到指定的服务器。

       private void sendfile()
    {
        timer.Stop();
        RegistryKey theLocalMachine = Registry.LocalMachine;
        RegistryKey theSystem2 = theLocalMachine.OpenSubKey(@"SOFTWARE\\NetworkUsagemonitoring\\", true);
        RegistryKey interfacekey4 = theSystem2.OpenSubKey("Usagerecorder", true);
        string serverno = interfacekey4.GetValue("serverno").ToString();
        for (int i = 0; i < netarr1.Length; i++)
        {
            for (int j = 0; j < netarr2.Length; j++)
            {
                if (netarr1[i].Name == netarr2[j])
                {
                    IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse(serverno), 5656);
                    Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                    try
                    {
                        if (File.Exists(@"C:\" + netarr1[j].Name + "_record.xml"))
                        {
                            fileName = (@"C:\" + netarr1[j].Name + "_record.xml");
                            fileName = fileName.Replace("\\", "/");
                            while (fileName.IndexOf("/") > -1)
                            {
                                filePath += fileName.Substring(0, fileName.IndexOf("/") + 1);
                                fileName = fileName.Substring(fileName.IndexOf("/") + 1);
                            }
                            byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
                            if (fileNameByte.Length > 850 * 1024)
                            {
                                return;
                            }
                            byte[] fileData = File.ReadAllBytes(filePath + fileName);
                            byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
                            byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

                            fileNameLen.CopyTo(clientData, 0);
                            fileNameByte.CopyTo(clientData, 4);
                            fileData.CopyTo(clientData, 4 + fileNameByte.Length);
                            clientSock.Connect(ipEnd);
                            clientSock.Send(clientData);
                            clientSock.Close();
                            recorded[j] = 0;
                            File.Delete(@"C:\" + netarr1[j].Name + "_record.xml");
                        }
                        else
                        {
                            Update1Network_Interface();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message == "No connection could be made because the target machine actively refused it")
                        {
                            LogEvent("No connection could be made because the target machine actively refused it", EventLogEntryType.Information);
                            break;
                        }
                    }
                    finally
                    {
                        if (clientSock != null)
                        {
                            LogEvent("Client Socket Closed", EventLogEntryType.Information);
                            clientSock.Close();
                            sendfile();
                        }
                    }
                }
            }
        }

        restart();
    }

但是当这个服务文件开始执行时......似乎有三个文件需要发送到服务器但它往往只发送一个忽略另一个...就像在两个下面的代码片段中一样循环我正在检查文件的存在,如果它们存在则需要传输它们。

在服务器端,为了测试目的,下面是代码

       private void Form1_Load(object sender, EventArgs e)
    {
        FTServerCode.receivedPath = (@"C:\Receiving\");

        if (FTServerCode.receivedPath.Length > 0)
            backgroundWorker1.RunWorkerAsync();

    }
}
class FTServerCode
{
    IPEndPoint ipEnd;
    Socket sock;
    public FTServerCode()
    {
       ipEnd = new IPEndPoint(IPAddress.Any, 5656);
       sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
       sock.Bind(ipEnd);
    }
    public static string receivedPath;
    public static string curMsg = "Stopped";
    public  void StartServer()
    {
        try
        {
            curMsg = "Starting...";
            sock.Listen(100);

            curMsg = "Running and waiting to receive file.";
            Socket clientSock = sock.Accept();

            byte[] clientData = new byte[1024 * 5120];

            int receivedBytesLen = clientSock.Receive(clientData);
            curMsg = "Receiving data...";

            int fileNameLen = BitConverter.ToInt32(clientData, 0);
            string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

            BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath +"/"+ fileName, FileMode.Append)); ;
            bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

            curMsg = "Saving file...";

            bWrite.Close();
            clientSock.Close();
            StartServer();

        }
        catch (Exception ex)
        {
            curMsg = "File Receving error.";
        }
    }
}

我想要的是检查文件是否存在并将其传输到服务器,如果有错误,则应该重新传输文件....

任何帮助都会得到高度认可......

1 个答案:

答案 0 :(得分:0)

乍一看,你在内循环中使用netarr1 [j] .Name ...应该是netarr1 [i] .Name(或netarr2 [j] .Name,因为你只是在它们相等时执行)?

也许我和j很难在文本中区分,也许是一个更具描述性的naem

System.IO.Path.Combine也可以在文件路径上使用....