利用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 ...
随机推荐
- which---查找并显示给定命令的绝对路径
which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录.which指令会在环境变量$PATH设置的目录里查找符合条件的文件.也就是说,使用which命令,就可 ...
- SpringBoot @PathVariable 和 @requestParam区别
1.若获取的入参的 参数 是下面这种形式 就使用 @requestParam 去获取 参数‘1’ /user?id=1 // url:xxx/user?id=1 @RequestMapping(&qu ...
- Redisclient连接方式Hiredis简单封装使用,连接池、屏蔽连接细节
工作须要对Hiredis进行了简单封装,实现功能: 1.API进行统一,对外仅仅提供一个接口. 2.屏蔽上层应用对连接的细节处理: 3.底层採用队列的方式保持连接池,保存连接会话. 4.重连时採用时间 ...
- hdu5305 Friends(dfs+map/hash)
题目:pid=5305">http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给定N个人和M条朋友关系,是朋友关系的两个人之间有两种联系 ...
- apicloud中的sqlite操作模块db
db 模块封装了手机常用数据库 sqlite 的增删改查语句,可实现数据的本地存储,极大的简化了数据持久化问题. 1.执行 var db = api.require('db'); db.execute ...
- amaze ui响应式表格
amaze ui响应式表格 这里的div外嵌设置格式倒是不错的选择
- 73.node.js开发错误——TypeError: Cannot set property 'XXX' of undefined
转自:https://blog.csdn.net/fd214333890/article/details/53467429
- 兼容MIUI5和MIUI6的开启悬浮窗设置界面
前一段时间项目中需要对MIUI的悬浮窗开启设置界面进行了引导和跳转,MIUI6中又改变了开启悬浮窗设置的位置,在苦苦寻觅之后,找到了解决的方法,贴出来以方便大家参考和使用. @Override pub ...
- 经典的横线中间文字css布局---flex布局
html: <div class="title"> <div class="line"></div> <div cla ...
- 通过CURL抓取页面中的图片路径并下载到本地
1.首页是图片处理页面downpic.php <?phpfunction getImage($url,$filename="") { if($url=="" ...