Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am writing code for to transfer one file from one computer to another computer, in my case i am able to transfer file client and server in single system,but i am not able do for between two different systems.
Please help me to resolve this issue,


Thanks in advance.

What I have tried:

Server:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            //IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
            //lbl_Status.Text = "My IP address is " + IPHost.AddressList[0].ToString();
            nSockets = new ArrayList();
            Thread thdListener = new Thread(new ThreadStart(ListenerThread));
            thdListener.Start();
        }

        public void ListenerThread()
        {
            TcpListener tcpListener = new TcpListener(IPAddress.Parse(GetIp()),8080);
            tcpListener.Start();
           
            while (true)
            {
                Socket handlerSocket = tcpListener.AcceptSocket();
                //TcpClient client = tcpListener.AcceptTcpClient();
                //MessageBox.Show(client.Client.RemoteEndPoint + ".");
                if (handlerSocket.Connected)
                {  
                    lb_Connections.Invoke((MethodInvoker)delegate 
                    {
                        //btn_SaveFile.PerformClick();
                        saveFileDialog_Server.ShowDialog();
                        txt_SaveFile_Location.Text = saveFileDialog_Server.FileName;
                        lb_Connections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " connected.");
                    });
                        
                    lock (this)
                    {
                        nSockets.Add(handlerSocket);
                    }
                    ThreadStart thdstHandler = new ThreadStart(handlerThread);
                    Thread thdHandler = new Thread(thdstHandler);
                    thdHandler.Start();
                }
            }
        }
        public void handlerThread()
        {
            Socket handlerSocket = (Socket)nSockets[nSockets.Count - 1];
            NetworkStream networkStream = new NetworkStream(handlerSocket);
            int thisRead = 0;
            int blockSize = 1024;
            Byte[] dataByte = new Byte[blockSize];
            lock (this)
            {
                // Only one process can access
                // the same file at any given time
                 Stream fileStream = File.OpenWrite(txt_SaveFile_Location.Text);
                while (true)
                {
                    thisRead = networkStream.Read(dataByte, 0, blockSize);
                    fileStream.Write(dataByte, 0, thisRead);
                    if (thisRead == 0) break;
                }
                fileStream.Close();
            }

            lb_Connections.Invoke((MethodInvoker)delegate
            {
                lb_Connections.Items.Add("File Written");
            });
            
            handlerSocket = null;
        }
        public string GetIp()
        {
            string name = Dns.GetHostName();
            IPHostEntry entry = Dns.GetHostEntry(name);
            IPAddress[] addr = entry.AddressList;
            if (addr[1].ToString().Split('.').Length == 4)
            {
                return addr[1].ToString();
            }
            return addr[2].ToString();
        }


Client:
C#
private void button1_Click(object sender, EventArgs e)
       {
           DialogResult dialogResult =folderBrowserDialog_openFiles.ShowDialog();
           txt_OpenFiles.Text = folderBrowserDialog_openFiles.SelectedPath;
           filepath = txt_OpenFiles.Text;
       }

       private void button2_Click(object sender, EventArgs e)
       {
           try
           {
               saveFileDialog_zip.Filter = @"Zip Files (.zip) | *.zip | All Files(*.*) | *.*";
               DialogResult dr = saveFileDialog_zip.ShowDialog();
               saveFile = saveFileDialog_zip.FileName;
               fileName = saveFile;
               ZipFile.CreateFromDirectory(filepath, fileName);
               MessageBox.Show("Zip File Created at your Saved Location");
           }

           catch (Exception exception)
           {
               MessageBox.Show("Zip file not created");
           }
       }

       private void btn_Browse_Zip_Click(object sender, EventArgs e)
       {

           DialogResult drbrowse_zip=openFileDialog_OpenZip.ShowDialog();
           openFileDialog_OpenZip.Filter = "Zip Files (.zip) | *.zip | All Files(*.*) | *.*";
           openFileDialog_OpenZip.FilterIndex = 1;
           if (drbrowse_zip == System.Windows.Forms.DialogResult.OK)
           {
                   txt_BrowseZip.Text = openFileDialog_OpenZip.FileName;
           }
       }

       private void btn_Transfer_Click(object sender, EventArgs e)
       {

           Stream fileStream = File.OpenRead(txt_BrowseZip.Text);
           // Alocate memory space for the file
           byte[] fileBuffer = new byte[fileStream.Length];
           fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
           // Open a TCP/IP Connection and send the data
           TcpClient clientSocket=new TcpClient(txt_Server.Text,8080);
           NetworkStream networkStream = clientSocket.GetStream();
           networkStream.Write(fileBuffer, 0, fileBuffer.GetLength(0));
           networkStream.Close();
       }
Posted
Updated 4-Aug-16 23:29pm
v2
Comments
Garth J Lancaster 4-Aug-16 8:29am    
>> but i am not able do for between two different systems.

what does that mean ? what happens ? what is the error ? where does the code stop/fail ? is there any connectivity between machine 1 and 2 ? are they on the same network ? can you ping from the 'client' to the 'server' ?

we cant see your screen - you'd be better off learning how to use the debugger - once you have a little more information please update your question using the 'Improve Question' widget
Richard MacCutchan 4-Aug-16 8:31am    
You will need the server to pass it on to the other system.
Member 12528561 4-Aug-16 8:38am    
i wrote code for server also, i am having doubt about that my code is work for transfer file from one system to other system.

1 solution

 
Share this answer
 

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