z自己写的

写了一个文件传输,比较简单。分发送方和接收方。在本机上测试之后,确实可以收到文件,但是会一直接收数据,导致获得的文件变得异常的大。请大家赐教,本人新手,可能写的地方有错误,请指出。谢谢。
//接收方代码。我把名称空间都删了,
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();
              
            }
        }
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27204811/viewspace-738165/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/27204811/viewspace-738165/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值