下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 速冻米面制品制作工岗前品质考核试卷含答案
- 硅片研磨工操作管理强化考核试卷含答案
- 儿童发育指导师安全知识竞赛水平考核试卷含答案
- 紫胶蒸发工操作管理能力考核试卷含答案
- 微波铁氧体器件调测工岗前岗中技能考核试卷含答案
- 化学合成制药工安全风险竞赛考核试卷含答案
- 叉车司机操作技能测试考核试卷含答案
- 变压器互感器制造工岗前工作技能考核试卷含答案
- 纺丝原液制备工岗前岗位水平考核试卷含答案
- 涂料油漆工程施工技术方案
- 山西省煤矿安全b类题库及答案解析
- 信息学考试题及答案
- 2025湖北省重点高中自主招生数学试卷试题(含答案详解)
- 输液泵和静推泵课件
- 渔业经济与管理课件
- 湛江科技学院《高等数学Ⅱ》2025-2026学年期末试卷(A卷)
- 信息化工作专班管理办法
- 2024年延长石油招聘笔试真题
- 2025至2030中国沥青基碳纤维行业发展趋势分析与未来投资战略咨询研究报告
- PE管材项目可行性研究报告范文
- 脾胃病辨证护理
评论
0/150
提交评论