尝试将文件客户端传输到同一台电脑上的服务器时出现异常

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

好的,所以我写了一些非常简单的客户端-服务器程序(在 c# 中使用 .net)。

当我尝试将字符串从客户端传输到服务器时,它工作正常,但是当我尝试将文件从客户端传输到服务器时,出现异常:

System.IO.IOException: 'Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.' SocketException: 现有连接被远程主机强行关闭

该文件是一个 txt 文件 (1KB)。

如您所见,客户端和服务器都在同一台计算机上:

客户:

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);
        }
    }
}
}

服务器:

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();
    }
}
}

我有一个好消息要告诉你,你的程序似乎运行得很好。
我复制它并执行并得到 "success.txt".

这意味着您这边唯一的问题必须是您的 IDE 授权写入文件或锁定您的文件的东西。 我建议你重新启动你的电脑——用管理员权限打开IDE("Run as Administrator" visual studio)看看你是否还有问题。

从代码的角度来看 - 所有作品。