下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、利用socket 套接字来进行文件传输速度还行,有需要完整版的可以联系我,免费公布。具体代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.IO;using System.Threading;namespace 发送文件
2、 public partial class Form1 : Form public Form1() CheckForIllegalCrossThreadCalls = false; InitializeComponent(); private Socket socket; private Thread a, b; private string filename; private TcpListener listen; private void start() listen = new TcpListener(14); listen.Start(); while (true) Socket s
3、= listen.AcceptSocket(); socket = s; b = new Thread(new ThreadStart(this.Receive); b.Start(); b.IsBackground = true; private void Receive() try Socket s = socket; NetworkStream ns = new NetworkStream(s); byte bytes = new byte1024 * 1024; int x = ns.Read(bytes, 0, bytes.Length); string mes = Encoding
4、.Unicode.GetString(bytes, 0, x).Split('|'); int length = int.Parse(mes0); SaveFileDialog sf = new SaveFileDialog(); sf.FileName = mes1; if (sf.ShowDialog() = DialogResult.OK) FileStream fs = new FileStream(sf.FileName, FileMode.Create, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs
5、); int total = 0; x = 0; while (total < length) x = ns.Read(bytes, 0, bytes.Length); bw.Write(bytes,0,x); total += x; bw.Close(); ns.Close(); fs.Close(); s.Close(); MessageBox.Show("ok"); catch (Exception ee) MessageBox.Show(ee.ToString(); private void button1_Click(object sender, Event
6、Args e) TcpClient client = new TcpClient(textBox1.Text.Trim(), 14); NetworkStream ns = client.GetStream(); OpenFileDialog aa = new OpenFileDialog(); if(aa.ShowDialog() = DialogResult.OK) string ss = aa.FileName.Split(''); filename = ssss.Length - 1; FileStream fs = new FileStream(aa.FileName
7、,FileMode.Open,FileAccess.Read); BinaryReader br = new BinaryReader(fs); Int64 size = fs.Length; MessageBox.Show(size.ToString(); byte bytes = new byte1024*1024; bytes = Encoding.Unicode.GetBytes(size.ToString()+'|'+filename+'|'); ns.Write(bytes,0,bytes.Length); Int64 total = 0; while (total < size) int x = br.Read(bytes,0,bytes.Length); ns.Write(bytes,0,x); total += x; private void F
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 护理课件制作软件的用户评价分析
- 初中2025非遗文化主题班会说课稿
- 小学尊重他人2025说课稿
- 初中专题探究2025年生物多样性说课稿
- 高中2025“传统文化”主题班会说课稿
- 二、用SmartArt图形制作导览图说课稿2025学年小学信息技术粤教版四年级下册-粤教版
- 2026年湖北省荆州市企业“直通车”专项职称评审(兽医畜牧+畜牧)综合练习题及答案
- 养老护理员中级练习题及答案
- 注册公用设备工程师给水排水基础考试题库和答案
- 生产安全管理英语讲解
- 2026信息安全行业市场发展分析及前景趋势与投融资发展机会研究报告
- 2026山东临沂市郯城县城镇公益性岗位招聘41人备考题库附答案详解(考试直接用)
- 物流园区安全生产风险分级管控清单
- 北京市2025文化和旅游部恭王府博物馆应届毕业生招聘笔试历年参考题库典型考点附带答案详解
- 陕西省宝鸡市2026届中考语文全真模拟试卷含解析
- 2026湖南益阳桃江县产业发展投资集团有限公司招聘4人笔试备考题库及答案详解
- 产品包装、运输、装卸方案
- 2026届湖北省武汉市高三四调英语试题(含答案和音频)
- 2025年河北唐山市八年级地理生物会考考试题库(附含答案)
- T-SZRCA 011-2025 人形机器人专用线缆技术规范
- 内江市东兴区2025年网格职员考试题及答案
评论
0/150
提交评论