Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

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


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:

C#
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);
            }
        }
Posted
Updated 27-Dec-16 3:37am
v2
Comments
ZurdoDev 27-Dec-16 8:14am    
That's because nothing is listening on the port you are trying to send to. Or you have a firewall blocking it, or something else. Not sure what you want from us.
Agarwal1984 27-Dec-16 8:19am    
my antivirus is disabled and firewall blocked already.
[no name] 27-Dec-16 9:05am    
https://www.google.com/search?q=No+connection+could+be+made+because+the+target+machine+actively+refused+it+in+.net+socket+programming+windows+application&oq=No+connection+could+be+made+because+the+target+machine+actively+refused+it+in+.net+socket+programming+windows+application

1 solution

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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900