无法建立连接,因为目标计算机在.NET套接字编程窗口应用程序中主动拒绝它 [英] No connection could be made because the target machine actively refused it in .NET socket programming windows application

查看:69
本文介绍了无法建立连接,因为目标计算机在.NET套接字编程窗口应用程序中主动拒绝它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



Hello,

No connection could be made because the target machine actively refused it in .net socket programming windows application





我想在本地服务器上传输zip文件,但是它收到错误。



如何解决这个问题?

请帮帮我。

先谢谢。



Ankit Agarwal

软件工程师



我尝试过:





I want to transfer zip file in local server but its getting error.

How to resolve this issue?
Please help me.
Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

private void btnBrowse_Click_1(object sender, EventArgs e)
        {
            
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                TextBoxValue = folderBrowserDialog1.SelectedPath;

            int count = panel1.Controls.OfType<Label>().ToList().Count;
            TextBox textbox = new TextBox();
            count = panel1.Controls.OfType<TextBox>().ToList().Count;
            textbox.Location = new Point(3, 25 * count);
            textbox.Size = new Size(188, 20);
            textbox.Name = "textbox_" + (count + 1);
            textbox.Text = TextBoxValue;
            
            arrayList.Add(TextBoxValue);
            //textbox.TextChanged += new System.EventHandler(this.TextBox_Changed);
            panel1.Controls.Add(textbox);
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            //int count = panel1.Controls.OfType<Label>().ToList().Count;
            try
            {
                string ipAddress = txtIP.Text;
                int port = int.Parse(txtPort.Text);
                for (int i = 0; i < arrayList.Count; i++)
                {
                    using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
                    {
                        zip.AddDirectory(arrayList[i].ToString());
                        //zip.Save(@"D:\abc" + "_" + (i + 1) + ".zip");
                        
                        //zip.Save(@"D:\abc" + "_" + (i + 1) + ".zip");
                        fileName = @"D:\abc" + "_" + (i + 1) + ".zip";
                        Task.Factory.StartNew(() => SendFile(ipAddress, port, fileName, shortFileName));
                        MessageBox.Show("Files zipped");
                    }
                }
            }
            catch (Ionic.Zip.ZipException ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (checkBox1.Checked == true)
            {
                var psi = new ProcessStartInfo("shutdown", "/s /t 0");
                psi.CreateNoWindow = true;
                psi.UseShellExecute = false;
                Process.Start(psi);
                //ProcessStartInfo startinfo = new ProcessStartInfo("shutdown.exe", "-s");
                //Process.Start(startinfo);
            }
        }
        public void SendFile(string remoteHostIP, int remoteHostPort, string longFileName, string shortFileName)
        {
            try
            {
                if (!string.IsNullOrEmpty(remoteHostIP))
                {
                    byte[] fileNameByte = Encoding.ASCII.GetBytes(shortFileName);
                    byte[] fileData = File.ReadAllBytes(longFileName);
                    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);
                    TcpClient clientSocket = new TcpClient(remoteHostIP,remoteHostPort); // Error in this line.
                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Write(clientData, 0, clientData.GetLength(0));
                    networkStream.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

推荐答案

如果是本地服务器,只需使用系统。 IO.File.Copy 复制文件。根据运行代码的机器,您将需要到其他机器系统/驱动器/文件夹的完整DNC路径。当然,如果你没有正确的权限,这将无效。



你的另一个选择是使用内置的.Net FTP对象进行传输文件。
If it's a local server, just use System.IO.File.Copy to copy the file. Depending on the machine running the code, you'll need the full DNC path to the other machine system/drive/folder. Of course, that won't work if you don't have the correct permissions.

Your other option is to use the built-in .Net FTP objects to transfer the file.


这篇关于无法建立连接,因为目标计算机在.NET套接字编程窗口应用程序中主动拒绝它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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