利用Socket进行大文件传输
分类: WINDOWS

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
{
public partial class FormChat : Form
{
public FormChat()
{
InitializeComponent();
}
private IPAddress localIP = null;
private Socket socketReceiveMsg = null;
private int portReceiveMsg;
private Thread threadReceiveMsg = null;
private Socket socketWaiting = null;
private int portWaiting;
private Thread threadWaiting = null;
private Socket socketResponse = null;
private int portResponse;
private Thread threadResponse = null;
private Socket socketReceiveData;
private int portReceiveData;
private Thread threadReceiveData;
{
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
this.localIP = hostEntry.AddressList[1];
this.txtLocalIP.Text = this.localIP.ToString();
this.portWaiting = random.Next(20000, 30000);
this.portReceiveMsg = random.Next(10000,20000);
this.portReceiveData = random.Next(20000, 40000);
this.portResponse = random.Next(10000,30000);
this.txtLocalReceiveMsgPort.Text = this.portReceiveMsg.ToString();
this.txtLocalWaitingPort.Text = this.portWaiting.ToString();
this.socketReceiveMsg = new Socket(endPointReceiveMsg.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
this.socketReceiveMsg.Bind(endPointReceiveMsg);
this.threadReceiveMsg = new Thread(new ThreadStart(ReceiveMsgListener));
this.threadReceiveMsg.Start();
this.socketWaiting = new Socket(endPointWaiting.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
this.socketWaiting.Bind(endPointWaiting);
this.threadWaiting = new Thread(new ThreadStart(WaitingListener));
this.threadWaiting.Start();
this.socketResponse = new Socket(endPointResponse.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
this.socketResponse.Bind(endPointResponse);
this.threadResponse = new Thread(new ThreadStart(ResponseListener));
this.threadResponse.Start();
this.socketReceiveData = new Socket(endPointReceiveData.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
this.socketReceiveData.Bind(endPointReceiveData);
this.threadReceiveData = new Thread(new ThreadStart(ReceiveDataListener));
this.threadReceiveData.Start();
}
{
this.socketReceiveMsg.Listen(10);
{
Socket socketTemp = this.socketReceiveMsg.Accept();
int byteCount = socketTemp.Receive(buffer);
this.rtbHistory.AppendText(message);
}
}
private void WaitingListener()
{
this.socketWaiting.Listen(10);
{
Socket socketTemp = this.socketWaiting.Accept();
}
}
{
byte[] buffer = new byte[1024];
int byteCount = socketTemp.Receive(buffer);
string request = Encoding.Default.GetString(buffer, 0, byteCount);
string ip = request.Substring(request.IndexOf("[fileName]") + 10, request.IndexOf("[ip]") - (request.IndexOf("[fileName]") + 10));
string port = request.Substring(request.IndexOf("[ip]") + 4, request.IndexOf("[port]") - (request.IndexOf("[ip]") + 4));
int remoteResponsePort = int.Parse(port);
{
isReceive = true;
}
{
isReceive = false;
this.txtSaveAs.Text = "";
}
//向发送方发出回复
IPEndPoint endPointRemote = new IPEndPoint(IPAddress.Parse(this.txtRemoteIP.Text), remoteResponsePort);
Socket socektResponse = new Socket(endPointRemote.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
{
string messageResponse = string.Format("{0}[isReceive]{1}[port]",isReceive,this.portReceiveData);
}
}
private void ResponseListener()
{
this.socketResponse.Listen(10);
{
Socket socketTemp = this.socketResponse.Accept();
int byteCount = socketTemp.Receive(buffer);
string portReceiveRemote = messageResponse.Substring(messageResponse.IndexOf("[isReceive]") + 11, messageResponse.IndexOf("[port]") - (messageResponse.IndexOf("[isReceive]") + 11));
{
//对方愿意接收文件,准备发送
IPEndPoint endPointRemote = new IPEndPoint(IPAddress.Parse(this.txtRemoteIP.Text), int.Parse(portReceiveRemote));
Socket socketTransmint = new Socket(endPointRemote.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
{
string filePath = this.txtFilePath.Text;
FileStream fileStream = new FileStream(filePath, FileMode.Open);
//得到文件流的长度
//int fileSize = (int)fileStream.Length;
long fileSize = fileStream.Length;
//先将要发送的文件长度发送给接收方,让对方准备缓冲区
socketTransmint.Send(BitConverter.GetBytes(fileSize));
//定义一个每次要发送的文件的大小(10M)
//int partSize = 10 * 1024 * 1024;
long partSize = 50 * 1024 * 1024;
long partCount = fileSize / partSize;
long rest = fileSize % partSize;
{
byte[] bufferData = new byte[partSize];
}
if (rest != 0)
{
byte[] bufferData = new byte[rest];
}
fileStream.Close();
}
}
else
{
MessageBox.Show("对方拒绝了您的发送请求!");
}
}
}
private void ReceiveDataListener()
{
this.socketReceiveData.Listen(10);
{
Socket socketTemp = this.socketReceiveData.Accept();
// byte[] bufferSize = new byte[4];
byte[] bufferSize =new byte[8];
int byteCount = socketTemp.Receive(bufferSize);
long fileSize = BitConverter.ToInt64(bufferSize, 0);
//int finishSize = 0;
long finishSize = 0;
{
byte[] bufferData = new byte[8096];
}
fileStream.Close();
}
}
private void btnSend_Click(object sender, EventArgs e)
{
IPEndPoint endPointRemote = new IPEndPoint(IPAddress.Parse(this.txtRemoteIP.Text), int.Parse(this.txtRemoteReceiveMsgPort.Text));
Socket socketClient = new Socket(endPointRemote.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
if (socketClient.Connected)
{
string message = this.txtMessage.Text;
byte[] buffer = Encoding.Default.GetBytes(message);
}
}
private void btnBrowser_Click(object sender, EventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
{
this.txtFilePath.Text = openDlg.FileName;
string filePath = this.txtFilePath.Text;
string ip = this.localIP.ToString();
string port = this.portResponse.ToString();
{
socketTemp.Send(buffer);
}
}
}
private void FormChat_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.socketReceiveMsg != null)
{
this.socketReceiveMsg.Close();
}
if (this.threadReceiveMsg != null)
{
if (this.threadReceiveMsg.IsAlive)
{
this.threadReceiveMsg.Abort();
}
}
{
this.socketReceiveData.Close();
}
if (this.threadReceiveData != null)
{
if (this.threadReceiveData.IsAlive)
{
this.threadReceiveData.Abort();
}
}
{
this.socketResponse.Close();
}
if (this.threadResponse != null)
{
if (this.threadResponse.IsAlive)
{
this.threadResponse.Abort();
}
}
{
this.socketWaiting.Close();
}
if (this.threadWaiting != null)
{
if (this.threadWaiting.IsAlive)
{
this.threadWaiting.Abort();
}
}
}
}
利用Socket进行大文件传输的更多相关文章
- WCF大文件传输服务
由于项目需要,自己写一个基于WCF的大文件传输服务雏形.觉得有一定的参考价值,因此放在网上分享. 目前版本为v1.1特点如下: 1.文件传输端口为18650 2.上传和下载文件 3.支持获取文件传输状 ...
- php 利用socket上传文件
php 利用socket上传文件 张映 发表于 2010-06-02 分类目录: php 一,利用fsockopen来上传文件 以前我写过一篇关于socket通信原理的博文http://blog.51 ...
- 转:wcf大文件传输解决之道(2)
此篇文章主要是基于http协议应用于大文件传输中的应用,现在我们先解析下wcf中编码器的定义,编码器实现了类的编码,并负责将Message内存中消息转变为网络发送的字节流或者字节缓冲区(对于发送方而言 ...
- 转:wcf大文件传输解决之道(1)
首先声明,文章思路源于MSDN中徐长龙老师的课程整理,加上自己的一些心得体会,先总结如下: 在应对与大文件传输的情况下,因为wcf默认采用的是缓存加载对象,也就是说将文件包一次性接受至缓存中,然后生成 ...
- WCF大文件传输【转】
http://www.cnblogs.com/happygx/archive/2013/10/29/3393973.html WCF大文件传输 WCF传输文件的时候可以设置每次文件的传输大小,如果是小 ...
- AetherUpload大文件传输
AetherUpload-Laravel是laravel框架下的一个大文件传输组件 github:https://github.com/peinhu/AetherUpload-Laravel 文件传输 ...
- 基于socket实现大文件上传
import socket 1.客户端: 操作流程: 先拿到文件--->获取文件大小---->创建字典 1.制作表头 header 如何得到 他是一个二进制字符串 序列化得到 字典字符串 ...
- 大文件传输 分片上传 上传id 分片号 授权给第三方上传
https://www.zhihu.com/question/39593108 作者:ZeroOne链接:https://www.zhihu.com/question/39593108/answer/ ...
- python tcp黏包和struct模块解决方法,大文件传输方法及MD5校验
一.TCP协议 粘包现象 和解决方案 黏包现象让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd)执行远程命令的模块 需要用到模块subprocess sub ...
随机推荐
- linux下支持托盘的邮件客户端Sylpheed
在网上搜索了很多客户端想支持系统托盘,发现一个很不错的邮件客户端Sylpheed.设置方式和foxmail很像,最为重要的是支持系统托盘,很方便,默认没有开启,简单设置下:配置->通用首选项-& ...
- MongoDB常用操作整理
Mongodb:是一种NoSQL数据库,NoSQL:Not Only SQLSQL: 数据表->JDBC读取->POJO(VO.PO)->控制层转化为JSON数据->客户端 这 ...
- 学习《概率机器人》中英文PDF+Probabilistic Robotics
研究机器人时,使机器人能够应对环境.传感器.执行机构.内部模型.近似算法等所带来的不确定性是必须面对的问题. <概率机器人>对概率机器人学这一新兴领域进行了全面的介绍.概率机器人学依赖统计 ...
- gitlab-ce-11.0.1 安装及汉化
1.添加gitlab源(我这里使用了清华大学的源)cat <<EOF> /etc/yum.repos.d/gitlab-ce.repo[gitlab-ce]name=gitlab-c ...
- 03012_预处理对象executeQuery方法(实现数据库的查询)
1.概述 (1)通过预处理对象的executeQuery方法,完成记录的select语句的执行: (2)操作格式统一如下: ①注册驱动: ②获取连接: ③获取预处理对象: ④SQL语句占位符设置实际参 ...
- [51Nod]NOIP2018提高组省一冲奖班模测训练(三) 题解
链接 A.Anan的派对 题意:Anan想举办一个派对.Anan的朋友总共有 n 人.第i个人如果参加派对会得到 \(c_i\) 的快乐值,除他自己外每多一个人参加他会减少 \(d_i\) 的快乐值. ...
- SQL Server字符串分割函数
- 最大子矩阵和 51Nod 1051 模板题
一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 例如:3*3的矩阵: -1 3 -1 2 -1 3 -3 1 2 和最大的子矩阵是: 3 - ...
- Python 3 与"Hello World!"
Python 3 版本 Python的3.0版本,常被称为Python 3000,或简称Py3k.相对于Python的早期版本,这是一个较大的升级.为了不带入过多的累赘,Python 3.0在设计的时 ...
- 03011_预处理对象executeUpdate方法(实现数据库的增、删、改)
1.概述 (1)通过预处理对象的executeUpdate方法,完成记录的insert\update\delete语句的执行: (2)操作格式统一如下: ①注册驱动: ②获取连接: ③获取预处理对象: ...