Search code examples
c#.netclient-serversystem.io.file

getting exception trying to transfer file client to server on the same pc


Ok, so I wrote some really simple client-server program (using .net in c#).

When I'm trying to transfer a string from the client to the server it works fine but when I'm trying to transfer a file from the client to the server I'm getting the exception:

System.IO.IOException: 'Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.' SocketException: An existing connection was forcibly closed by the remote host

The file is a txt file (1KB).

Both the client and the server are on the same computer as you can see:

client:

using System;
using System.Net.Sockets;
using System.IO;
namespace client2
{
    class Program
    {
    static void Main(string[] args)
    {
        try
        {
            // Create a TcpClient.
            Int32 port = 13000;
            TcpClient client = new TcpClient("127.0.0.1", port);


            FileStream file = new FileStream("./amir.txt", FileMode.Open, FileAccess.Read);
            byte[] data = System.IO.File.ReadAllBytes("./amir.txt");

            //this one with a string works
                    //Byte[] data = System.Text.Encoding.ASCII.GetBytes("bla bla");
            // Get a client stream for reading and writing.

            Stream stream = client.GetStream();
            stream.Write(data, 0, data.Length);

        }
        catch (IOException e)
        {
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: {0}", e);
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }
    }
}
}

server:

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace serverSide
{

  class MyTcpListener
  {
    public static void Main()
    {
        TcpListener server = null;
        try
        {
            // Set the TcpListener on port 13000.
            Int32 port = 13000;
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");

            // TcpListener server = new TcpListener(port);
            server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            Byte[] bytes = new Byte[1000];

            // Enter the listening loop.
            while (true)
            {
                Console.Write("Waiting for a connection... ");

                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Connected!");


                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();


                //here I'm getting the exception
                int i = stream.Read(bytes, 0, bytes.Length);

                //this one with a string works!
                        //data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        //Console.WriteLine("Received: {0}", data);

                System.IO.File.WriteAllBytes("./success.txt", bytes);
                client.Close();
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }
        catch (IOException e)
        {
            Console.WriteLine("IOException: {0}", e);
        }
        finally
        {
            // Stop listening for new clients.
            server.Stop();
        }


        Console.WriteLine("\nHit enter to continue...");
        Console.Read();
    }
}
}

Solution

  • I have very good news for you, your program seem to work just great.
    I copied it and executed and got the "success.txt".

    This means that the only problem in your side must be something with authorization of your IDE to write to file or something that locks your file. I suggest you restart your computer - open the IDE with administrator privileges ("Run as Administrator" the visual studio) and see if you still have problem.

    From code point of view - all works.