NetworkSream使用尝试及困惑

来源:互联网 发布:sql output 编辑:程序博客网 时间:2024/05/21 17:10

     最近在写一个Ftp的实验,使用TcpClient类来进行的连接。在文件的上传下载时用到了NetworkStream的read和write方法来读取和写入数据及read(byte[] buffer, int offset, int count),write(byte[] buffer, int offset, int count)。结果遇到了了一个很大的问题,因为TcpClient的NetworkStream使用了标准的Tcp协议,所以在每次发送数据报文之后都要等待对端确认之后,才能继续发送!因此我每次向netstream里write时候,就会在写进去一定数据后,停在那儿了,根据调试我发现他是在等待对端的应答,来确认先前发的报文已经被正确的接收了。但是,不知道为什么对端好像一直也不应答,所以我的程序总卡在write那,走不了。为此我困惑了好几天,为此,我特地把文件传输拿出来做了次测试。情况如下:

接收数据端:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Net;using System.Net.Sockets;namespace TcpTestClient{    class Program    {        static void Main(string[] args)        {                        IPAddress[] ips = Dns.GetHostAddresses("");            string localIp = ips[2].ToString();            Console.WriteLine("发起连接");            TcpClient dataClient = new TcpClient(localIp, 20000);            //dataClient.ReceiveTimeout = 3000;            NetworkStream data = dataClient.GetStream();            //Stream data = dataClient.GetStream();            StreamReader reader=new StreamReader (data, Encoding.Default);            string path = reader.ReadLine();            FileStream fs = File.Create(path);            try            {                byte[] buffer = new byte[1024*20];                int count = buffer.Length;                while (count!=0)                 {                    count = data.Read(buffer, 0, 1024*20 );                    fs.Write(buffer, 0 ,count);                    fs.Flush();                }             }            catch(Exception ex)            {                Console.WriteLine(ex.Message);                Console.WriteLine("下载失败!!!");            }            fs.Close( );            data.Close( );           dataClient.Close( );            Console.ReadKey();        }    }}


发送数据端:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net;using System.IO;using System.Net.Sockets;namespace TcpTest{    class Program    {                static void Main(string[] args)        {             TcpClient dataClient=null;             IPAddress[] ips = Dns.GetHostAddresses("");             IPAddress localIp = ips[2];             TcpListener listener = new TcpListener(localIp, 20000);             listener.Start();             while (true)             {                 Console.WriteLine("等待连接");                 dataClient = listener.AcceptTcpClient();                dataClient.SendTimeout =10000;                 NetworkStream netstream = dataClient.GetStream();                 //Stream netstream =dataClient.GetStream();                 StreamReader reader = new StreamReader(netstream, Encoding.Default);                 StreamWriter writer = new StreamWriter(netstream, Encoding.Default);                 string filepath = "E:\\音乐\\萧亚轩 - 类似爱情.mp3";                 FileStream fs = File.Open(filepath, FileMode.Open, FileAccess.Read);                 filepath = "F:\\萧亚轩 - 类似爱情.mp3";                 writer.WriteLine(filepath);                 writer.Flush();                 long length = fs.Length;                 long len = 0;                 double progress = 0.00;                 try                 {                     byte[] buffer = new byte[1024*4];                     int count = buffer.Length;                     while (count!=0)                     {                                                count = fs.Read(buffer, 0, count);                             netstream.Write(buffer, 0, count);                             netstream.Flush();                             len += count;                                                         progress = Convert.ToDouble(len)/Convert.ToDouble(length)*100;                             Console.WriteLine(progress+ "%");                                              }                 }                 catch(Exception ex)                 {                     Console.WriteLine(ex.Message);                     Console.WriteLine("发送失败!!!");                 }                 fs.Close();                 netstream.Close();                 dataClient.Close();             }        }    }}


昨天在测试的时候,总是catch到远程主机无应答,write一定数据后就报异常了,于是我NetworkStream data改成了  Stream  data,数据就正常的发送了。也算是解决了这几天没搞定的问题。但在解决了之后,我在写这篇文章的时候,我把data又改成NetworkStream类型了,为了向大家展示异常,结果!!!!!!居然成功了, 卧了个槽啊!!!!!!!!!!!!!!!!!困扰了我几天的问题,到最后我又不知道是怎么了,路过的大神如果有知道原因的,希望给个解释啊。。。同时把NetworkStream类型改成Stream类型也算是解决这种问题的一种方法吧!




0 0
原创粉丝点击