【问题标题】:getting exception trying to transfer file client to server on the same pc尝试将文件客户端传输到同一台 PC 上的服务器时出现异常
【发布时间】:2018-04-19 22:07:56
【问题描述】:

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

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

System.IO.IOException: '无法从传输连接读取数据:现有连接被远程主机强行关闭。' 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();
    }
}
}

【问题讨论】:

  • 在结束客户端程序之前尝试稍等片刻。
  • 是的,就是这样,现在我做到了,服务器将发送给客户端,它工作正常..也许客户端在他认为之前完成了连接

标签: c# .net client-server system.io.file


【解决方案1】:

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

这意味着您这边唯一的问题必须是您的 IDE 授权写入文件或锁定您的文件的东西。 我建议您重新启动计算机 - 以管理员权限打开 IDE(“以管理员身份运行”Visual Studio),看看您是否仍有问题。

从代码的角度来看 - 一切正常。

【讨论】:

  • 感谢您的评论!好的,所以我做到了,但它仍然不起作用。我还打开了一个新项目,仅用于读取文件字节,然后将字节写入新的 txt 文件,它工作正常,所以我认为它与通过客户端服务器发送和读取有关
猜你喜欢
  • 1970-01-01
  • 2018-04-27
  • 2012-10-07
  • 2019-04-08
  • 2016-05-16
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多