C# Winform局域网传送文件
2016-12-29 00:00
183 查看
//发送文件 private void btn_sendFile_Click(object sender, EventArgs e) { //打开文件 OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() == DialogResult.OK) { //初始化接受套接字:寻址方案,以字符流方式和Tcp通信 socketSent = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //设置服务器IP地址和端口 ipSent = new IPEndPoint(IPAddress.Parse(ip), 8001); //与服务器进行连接 ClassSocket socketConnet = new ClassSocket(socketSent, ipSent); Thread tConnection = new Thread(new ThreadStart(socketConnet.SocketConnect)); tConnection.Start(); Thread.Sleep(100); //将要发送的文件加上"DAT"标识符 ClassSentFile sentFile = new ClassSentFile(dlg, socketSent); Thread tSentFile = new Thread(new ThreadStart(sentFile.SentFile)); tSentFile.Start(); } }
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; using System.Windows.Forms; namespace Message { class ClassSentFile { private OpenFileDialog dlg; private Socket socketSent; public ClassSentFile(OpenFileDialog dlg, Socket socketSent) { this.dlg = dlg; this.socketSent = socketSent; } public void SentFile() { string msg = "DAT " + dlg.FileName; //将 "msg" 转化为字节流的形式进行传送 socketSent.Send(Encoding.Default.GetBytes(msg)); //定义一个读文件流 FileStream read = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read); //设置缓冲区为1024byte byte[] buff = new byte[1024]; int len = 0; while ((len = read.Read(buff, 0, 1024)) != 0) { //按实际的字节总量发送信息 socketSent.Send(buff, 0, len, SocketFlags.None); } //将要发送信息的最后加上"END"标识符 msg = "END"; //将 "msg" 发送 socketSent.Send(Encoding.Default.GetBytes(msg)); socketSent.Close(); read.Close(); } } }
相关文章推荐
- C# Winform局域网传送文件
- 局域网内传送文件
- C# Winform局域网传送文件
- [转载]让QQ在局域网内快速传送文件
- 基于Qt的P2P局域网聊天及文件传送软件设计
- linux 局域网内文件传送
- linux 局域网内文件传送
- 免费局域网聊天传送文件软件—飞秋
- linux 局域网内文件传送
- 基于Qt的P2P局域网聊天及文件传送软件设计
- Linux局域网内文件传送
- 基于TCP的局域网多用户通信、文件传送程序详解
- 基于Qt的P2P局域网聊天及文件传送软件设计
- 局域网内基于WEB的文件传输解决方案详解(转)
- 内网用户之间使用MSN Messenger快速传送文件的小窍门
- ASP.NET使aspx页面能接受HTML,asp的页面传送的文件
- 买了本微软的Windows网络编程一书,看了看,写我第一个网络程序:文件传送程序
- Webservice传送文件的实现(AXIS2 MTOM)
- MIME文件在邮件中传送多种文件格式的文件
- SERV-U使用局域网中共享文件夹作FTP文件服务(部分解决,还存在问题待解决)