error in sending file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • koneyji
    New Member
    • Mar 2010
    • 4

    error in sending file

    hi i have following code to send file and its giving file io error

    Code:
    namespace client_server
    {
        class Program
        {
            static void Main(string[] args)
            {
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                TcpListener tcp = new TcpListener(ip, 1234);
                tcp.Start();
                Console.WriteLine("server started");
                while (true)
                {
                    Socket socket = tcp.AcceptSocket();
                    if (socket.Connected)
                    {
                        CHandler clH = new CHandler(socket);
                        Thread t = new Thread(new ThreadStart(clH.Task));
                        t.Start();
                    }
                }
            }
        }
    
    
        class CHandler
        {
            Socket socket;
            public CHandler(Socket s) { socket = s; }
            public void Task()
            {
                string path = "C:/";
                NetworkStream net = new NetworkStream(socket);
                StreamReader nIn = new StreamReader(net);
                StreamWriter nOut = new StreamWriter(net);
                string fName = nIn.ReadLine();
                path = path + fName;
    
                try
                {
                    StreamReader fIn = new StreamReader(path);
                    string nextLine;
                    Console.WriteLine("sending file");
                    nextLine = fIn.ReadLine();
                    while (nextLine != null)
                    {
    
                        nOut.WriteLine(nextLine);
                        nOut.Flush();
                        nextLine = fIn.ReadLine();
                    }
                    Console.WriteLine("file sent");
                    fIn.Close();
                    net.Close();
                    nOut.Close();
                    socket.Close();
                }
                catch { Console.WriteLine("file IO error"); }
            }
        }
    }
    
    
    
    namespace clientside
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("enter the file name");
                string name = Console.ReadLine();
                TcpClient serSocket;
                try
                {
                    serSocket = new TcpClient("127.0.0.1", 1234);
                }
                catch
                {
                    Console.WriteLine("Failed to connect");
                    return;
                }
                NetworkStream net = serSocket.GetStream();
                StreamWriter sOut = new StreamWriter(net);
                StreamReader sIn = new StreamReader(net);
    
                try
                {
                    sOut.WriteLine(name);
                    sOut.Flush();
                    string nextLine = sIn.ReadLine();
                    while (nextLine != null)
                    {
                        Console.WriteLine(nextLine);
                        nextLine = sIn.ReadLine();
                    }
                    sIn.Close();
                    net.Close();
                }
                catch
                {
                    Console.WriteLine("error reading from server");
                }
    
    
                Console.ReadKey();
            }
        }
    }
    Last edited by tlhintoq; Mar 26 '10, 06:51 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      On what line is the error?
      What exactly is the full error?

      Comment

      • koneyji
        New Member
        • Mar 2010
        • 4

        #4
        can't find the exactly where the error is?
        i think the error is somewhere here
        Code:
         try
                    {
                        StreamReader fIn = new StreamReader(path);
                        string nextLine;
                        Console.WriteLine("sending file");
                        nextLine = fIn.ReadLine();
                        while (nextLine != null)
                        {
         
                            nOut.WriteLine(nextLine);
                            nOut.Flush();
                            nextLine = fIn.ReadLine();
                        }
                        Console.WriteLine("file sent");
                        fIn.Close();
                        net.Close();
                        nOut.Close();
                        socket.Close();
        thanks for the reply
        Last edited by tlhintoq; Mar 27 '10, 05:43 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            To have this break on the exact line producing the code you need to make a change to your settings. Next time you debug this, Visual Studio will stop softly on any exception when it is Thrown instead of only when it goes unhandled.

            Debug menu
            Exceptions
            Check on "CLR"
            [imgnothumb]http://files.me.com/tlhintoq/d9gnrh[/imgnothumb]

            Comment

            • koneyji
              New Member
              • Mar 2010
              • 4

              #7
              thank you

              the error is in
              StreamReader fIn = new StreamReader(pa th);

              it says
              {"Access to the path 'D:\\datafiles\ \data' is denied."}

              i think it is a folder privacy issue..

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Well there ya go. Glad we could help you track down the exact nature of the problem: You don't have permissions in that folder or to that item. Mystery solved.

                Comment

                Working...