使用networkstream.read()循环时无法启动

时间:2018-12-05 20:37:04

标签: c# while-loop tcpclient tcplistener networkstream

我正在编写一个小型控制台应用程序,该应用程序通过TCP以base64格式发送文件内容。看来我无法一次发送数据,所以我试图做一个while循环,该循环从NetworkStream接收所有数据,但是在执行过程中我总是会遇到此异常:

 Unhandled Exception: System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote hostat System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)   --- End of inner exception stack trace ---   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)   at File_Transfer_Server.Program.Main(String[] args) in D:\Files from PC\Visual Basic Projects - =&+Ivan+&=\Tesseract\Temp\File Transfer\File_Transfer_Server\Program.cs:line 31

这是我的客户代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace File_Transfer_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient();
            client.Connect("127.0.0.1", 6666);

            NetworkStream clientNetworkStream = client.GetStream();

            string fileContent = FileBase64Encoding(@"D:\Download\AtomSetup-x64.exe");

                        byte[] fileBytes = Encoding.ASCII.GetBytes(fileContent);
                        byte[] fileLength = Encoding.ASCII.GetBytes(fileBytes.Length.ToString());

            clientNetworkStream.Write(fileBytes, 0, fileBytes.Length);
            Console.WriteLine("Send");
        }

        static string FileBase64Encoding(string path)
        {
            byte[] fileBytes = File.ReadAllBytes(path);
            string fileBase64String = Convert.ToBase64String(fileBytes);

            return fileBase64String;
        }
    }
}

这是我的服务器代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace File_Transfer_Server
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener serverListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 6666);
            serverListener.Start();
            TcpClient tcpClient = serverListener.AcceptTcpClient();
            Console.WriteLine(">>> Receiving");

            byte[] clientBuffer = new byte[1024];

            using (NetworkStream clientNStream = tcpClient.GetStream())
            {

                int i;
                string received = "";
                string data = null;


                while ((i = clientNStream.Read(clientBuffer, 0, clientBuffer.Length)) != 0) // exception 
                {
                    data = Encoding.ASCII.GetString(clientBuffer, 0, i);

                    received += data;
                }
                File.WriteAllText(@"E:\receivedData.txt", received);
                Console.WriteLine(">>>Done");
            }

        }

    }
}

我尝试了几乎所有解决方案,包括内存流,但对我没有任何帮助。 我将不胜感激地感谢您的帮助。 预先感谢!

0 个答案:

没有答案