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

时间:2018-04-19 22:07:57

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

好的,所以我写了一些非常简单的客户端服务器程序(在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();
    }
}
}

1 个答案:

答案 0 :(得分:1)

我有一个非常好的消息,你的程序看起来效果很好。
我复制并执行并获得了" success.txt"。

这意味着您身边的唯一问题必须是您的IDE授权才能写入文件或锁定文件的内容。 我建议你重新启动计算机 - 用管理员权限打开IDE(#34;以管理员身份运行"可视工作室),看看你是否还有问题。

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