欢迎来到天天文库
浏览记录
ID:10238855
大小:29.00 KB
页数:3页
时间:2018-06-12
《c# socket 传输文件》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、利用socket套接字来进行文件传输速度还行,有需要完整版的可以联系我,免费公布。具体代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.IO;usingSystem.Thread
2、ing;namespace发送文件{publicpartialclassForm1:Form{publicForm1(){CheckForIllegalCrossThreadCalls=false;InitializeComponent();}privateSocketsocket;privateThreada,b;privatestringfilename;privateTcpListenerlisten;privatevoidstart(){listen=newTcpListener(14);listen.Start();whil
3、e(true){Sockets=listen.AcceptSocket();socket=s;b=newThread(newThreadStart(this.Receive));b.Start();b.IsBackground=true;}}privatevoidReceive(){try{Sockets=socket;NetworkStreamns=newNetworkStream(s);byte[]bytes=newbyte[1024*1024];intx=ns.Read(bytes,0,bytes.Length);string[
4、]mes=Encoding.Unicode.GetString(bytes,0,x).Split('
5、');intlength=int.Parse(mes[0]);SaveFileDialogsf=newSaveFileDialog();sf.FileName=mes[1];if(sf.ShowDialog()==DialogResult.OK){FileStreamfs=newFileStream(sf.FileName,FileMode.Create,FileAccess.Write);BinaryWriterbw=newBina
6、ryWriter(fs);inttotal=0;x=0;while(total7、ntArgse){TcpClientclient=newTcpClient(textBox1.Text.Trim(),14);NetworkStreamns=client.GetStream();OpenFileDialogaa=newOpenFileDialog();if(aa.ShowDialog()==DialogResult.OK){string[]ss=aa.FileName.Split('\');filename=ss[ss.Length-1];FileStreamfs=newFileStream(aa.FileName8、,FileMode.Open,FileAccess.Read);BinaryReaderbr=newBinaryReader(fs);Int64size=fs.Length;MessageBox.Show(size.ToString());byte[]bytes=newbyte[1024*1024];bytes=Encoding.Unicode.GetBytes(size.ToString()+'9、'+filename+'10、');ns.Write(bytes,0,bytes.Length);Int64total=0;while(tot11、al
7、ntArgse){TcpClientclient=newTcpClient(textBox1.Text.Trim(),14);NetworkStreamns=client.GetStream();OpenFileDialogaa=newOpenFileDialog();if(aa.ShowDialog()==DialogResult.OK){string[]ss=aa.FileName.Split('\');filename=ss[ss.Length-1];FileStreamfs=newFileStream(aa.FileName
8、,FileMode.Open,FileAccess.Read);BinaryReaderbr=newBinaryReader(fs);Int64size=fs.Length;MessageBox.Show(size.ToString());byte[]bytes=newbyte[1024*1024];bytes=Encoding.Unicode.GetBytes(size.ToString()+'
9、'+filename+'
10、');ns.Write(bytes,0,bytes.Length);Int64total=0;while(tot
11、al
此文档下载收益归作者所有