| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 866 人关注过本帖
标题:C# SOcket 文件传输
只看楼主 加入收藏
hwb1992
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-7-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
C# SOcket 文件传输
我晚上写了一个文件传输,比较简单。分发送方和接收方。在本机上测试之后,确实可以收到文件,但是会一直接收数据,导致获得的文件变得异常的大。请大家赐教,本人新手,可能写的地方有错误,请指出。谢谢。
//接收方代码。我把名称空间都删了,
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();
              
            }
        }
    }
}
搜索更多相关主题的帖子: 测试 private public 
2012-07-19 23:50
hwb1992
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-7-19
收藏
得分:0 
自己顶一下,这不是水帖。
2012-07-20 00:00
a663537687
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2012-7-21
收藏
得分:10 
内阁请问你有网络编程这方面的视频吗?我想学学socket做局域网通信~发文字都没做明白~自学的这方面、现在只懂监听端口、以及链接服务端~~
2012-07-21 19:46
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:10 
接收方代码
int count = ne.Read(re, 0, re.Length);
while (count!=0)
{
    fi.Write(re, 0, re.Length);
    ne.Read(re, 0, re.Length);
}
当count不为0时进入while循环,这时count不为0,之后永远都不会为0,因为没有再为count赋过值了,while将永远执行下去。
2012-07-22 16:27
hwb1992
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-7-19
收藏
得分:0 
回复 3楼 a663537687
那个,我也是自学的。看书的。看别人的代码。
2012-07-22 18:13
hwb1992
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-7-19
收藏
得分:0 
回复 4楼 yhlvht
恩,我知道了,谢了。
2012-07-22 18:13
快速回复:C# SOcket 文件传输
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023655 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved