我晚上写了一个文件传输,比较简单。分发送方和接收方。在本机上测试之后,确实可以收到文件,但是会一直接收数据,导致获得的文件变得异常的大。请大家赐教,本人新手,可能写的地方有错误,请指出。谢谢。
//接收方代码。我把名称空间都删了,怕字数过多,发不了贴。
namespace rece
{
    public partial class Form1 : Form
    {
        private Thread rece;
        private TcpListener tcplisten;
        private TcpClient tcpc;
        private NetworkStream ne;
        private FileStream fi;        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {  
        }        private void button1_Click(object sender, EventArgs e)
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, int.Parse(textBox1.Text));
            tcplisten = new TcpListener(ip);
            tcplisten.Start();
            rece = new Thread(new ThreadStart(receive));
            rece.SetApartmentState(ApartmentState.STA);
            rece.Start();
          }
        private void receive()
        {
            tcpc = tcplisten.AcceptTcpClient();
            ne = tcpc.GetStream();
            byte[] name=new byte[128];
            ne.Read(name,0,name.Length);
            string filename = Encoding.Unicode.GetString(name);
            saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.FileName = filename;
            saveFileDialog1.ShowDialog();
            string path = saveFileDialog1.FileName;
            fi = new FileStream(path,FileMode.Create);
            byte[] re = new byte[1];
            int count = ne.Read(re, 0, re.Length);
            while (count!=0)
            {
                fi.Write(re, 0, re.Length);
                ne.Read(re, 0, re.Length);
 
            }
            fi.Close();
            ne.Close();
            tcpc.Close();
            tcplisten.Stop();
            rece.Abort();
            this.Close();
           
        }
    }
}//发送方
namespace CLIENT
{
    public partial class Form1 : Form
    {
        private NetworkStream s1;
        private TcpClient tcpclient=new TcpClient();        private Thread thr;
        private FileStream f1;        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {        }        private void button1_Click(object sender, EventArgs e)
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
            TcpClient tcpclient = new TcpClient();
            tcpclient.Connect(ip);
            s1 = tcpclient.GetStream();
        }        private void button2_Click(object sender, EventArgs e)
        {
            thr = new Thread(new ThreadStart(star));
            thr.SetApartmentState(ApartmentState.STA);
            thr.Start();        }
        private void star()
        {                 if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileInfo fi = new FileInfo(openFileDialog1.FileName);
                f1 = fi.OpenRead();
                string filename = fi.Name;
                byte[] se = new byte[128];
                se = Encoding.Unicode.GetBytes(filename.ToCharArray());
                s1.Write(se, 0, se.Length);
                int bytes;
                byte[] read = new byte[1];
                while (true)
                {
                  bytes = f1.Read(read, 0, read.Length);                    if (bytes == 0)
                    {
                        break;
                    }                     s1.Write(read, 0, read.Length);
                }                f1.Close();
                s1.Close();
                tcpclient.Close();
                thr.Abort();
              
            }
        }
    }
}

解决方案 »

  1.   

    发文件之前先把长度发过来,收到了这么多长度之后就直接写到文件里面去就行了。
    socket程序当然会一直接接收,没有数据到达的话,接收的线程会阻塞。
      

  2.   


    int count = ne.Read(re, 0, re.Length);
      while (count!=0)
      {
      fi.Write(re, 0, re.Length);
      //ne.Read(re, 0, re.Length);
    改:
    int count = ne.Read(re, 0, re.Length);
      
      }
    count永远不会为0啊。。
      

  3.   


    int readLength = 0;
    while ((readLength = response.Read(buffer, 0, buffer.Length)) > 0) {
    fs.Write(buffer, 0, readLength);
    }
      

  4.   


    while(ne.DataAvailable){//是否还有可读数据}