Thread: File Sharing application works well on local host but crashes on remote PC's

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    20

    Post File Sharing application works well on local host but crashes on remote PC's

    I wrote a code doing file sharing well on local host but fails to do so when a non-local IP's are fed to it.
    Here's the screen shot.
    File Sharing application works well on local host but crashes on remote PC's-ss-png
    when i gave a copy of it to my colleague and asked him to be my server (just listen on my IP).He entered my machine IP and a pre-decided port and a click of Listen_button made the program crush.
    I don't know why is this troubling me again and again.

    Here's the brief code,hope i have provided enough information and u may ask me for more (if needed).


    Server:
    Code:
     
     private void btnlisten_Click_1(object sender, EventArgs e)
            {
                ThreadStart threaddelegate = new ThreadStart(Thread);
                Thread newthread = new Thread(threaddelegate);
                newthread.Start();
    
    
            } 
    
    
    
    
            public void Thread()
            {
                IPAddress ipaddr = IPAddress.Parse(IPAddr_Serv.Text);
                var port = Convert.ToInt32(PortNo_serv.Text);
                MessageBox.Show(port + ipaddr.ToString());
                TcpListener tcpListener = new TcpListener(ipaddr, port);
                tcpListener.Start();
                MessageBox.Show("Listening on port" + port);
                TcpClient client = new TcpClient();
             
    
    
               // Accept client
                client = tcpListener.AcceptTcpClient();
                NetworkStream netStream = client.GetStream();
                string DirName = @"D:";
                string fileloc = DirName+FileName;
                Directory.CreateDirectory(Path.GetDirectoryName(fileloc));
                using (FileStream fs = new FileStream(fileloc, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    // do file receiving chores here
    
                 }

    Client:

    Code:
     TcpClient clientSocket = new TcpClient();
               // client.Connect(IPAddr_Client.Text, 8004);
                IPAddress ipaddrcl = IPAddress.Parse(IPAddr_Client.Text);
                int port = Convert.ToInt32(PortNo_serv.Text);
               // Connect to server
                try
                {
                    clientSocket.Connect(new IPEndPoint(ipaddrcl,port));
    
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
    
    
    
               NetworkStream netstream = clientSocket.GetStream();
                
                   using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                   {
    
                         //file receivng code goes here
    
                        }

    PS. A button_click makes server to run in a thread and listens to client.

    Suggestions please

  2. #2
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    Sorry to say but the View Count is increasing with every passing hour without any response

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Shouldn't the server be using IPAddress.Any? The IP address on the listener ends dictates which IP addresses it can accept connections from. In fact, I wouldn't have an IP address field for the server at all, just a port number.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,608
    I'm confused as well. Why is there an IP address for the server? The IP address of the server is the IP address of the current machine so IPAddress.Any should be used. I suspect your server is not actually running so when the client attempts to connect it fails. It works locally b/c if you use the IP address of the machine or use localhost they are the same thing. Remove the IP address for the server as it is not needed and is complicating a simple concept.

    Also WCF could be used here for file streaming. It handles all of the packet and low level socket stuff and you work with high level information and data. Unless your files are very large WCF would be my choice.

    I just finished a small app in WPF that uses WCF for this very thing so if you have questions fire away while its still fresh on my old mind. The mind starts to leak information in quantities proportional to ones age.
    Last edited by VirtualAce; 01-02-2015 at 02:49 PM.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    Quote Originally Posted by VirtualAce View Post
    I'm confused as well. Why is there an IP address for the server? The IP address of the server is the IP address of the current machine so IPAddress.Any should be used. I suspect your server is not actually running so when the client attempts to connect it fails. It works locally b/c if you use the IP address of the machine or use localhost they are the same thing. Remove the IP address for the server as it is not needed and is complicating a simple concept.

    Well,you're right and i realized it a day ago when i had never a chance to visit this sacred site after posting my Question :-).Actually it's self-learning and you'd expect such blunders.When i tested my Program using IPAddress.Any that freaking issue disappeared.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    Agreed!

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    Quote Originally Posted by VirtualAce View Post

    Also WCF could be used here for file streaming. It handles all of the packet and low level socket stuff and you work with high level information and data. Unless your files are very large WCF would be my choice.

    I just finished a small app in WPF that uses WCF for this very thing so if you have questions fire away while its still fresh on my old mind. The mind starts to leak information in quantities proportional to ones age.
    I wouldn't prefer using WPF as i would use it for movie files as large as 5 Gb too. But the issue is a new one now.My program on client side informs me "Data transferred" and the program using thread (for server) on another PC says "connected" .It should have been "connected" before "Data transferred" .I think I should do connection stuff in another button to ensure btn_send does print only "Data transferred" after completely transfer of data.Right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 10 possible ports for remote application
    By Verdagon in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-06-2011, 06:41 PM
  2. Replies: 41
    Last Post: 12-16-2010, 10:17 PM
  3. How to keep local and remote DB in sync
    By nicksnels in forum Tech Board
    Replies: 0
    Last Post: 09-10-2007, 02:17 AM
  4. Sharing a folder to remote peers
    By xErath in forum Windows Programming
    Replies: 1
    Last Post: 07-05-2006, 05:36 PM
  5. Intercepting Data Bound for Local Application from Remote Server
    By maththeorylvr in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-29-2005, 01:57 AM