利用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 ...
随机推荐
- 记一次 CORS 跨域请求出现 OPTIONS 请求的问题及解决方法
今天前后端在联调接口的时候,发生了跨域请求资源获取不到的问题. 首先说明下跨域问题的由来.引自HTTP 访问控制 的一段话: 当 Web 资源请求由其它域名或端口提供的资源时,会发起跨域 HTTP 请 ...
- 洛谷 P1900 自我数
P1900 自我数 题目描述 在1949年印度数学家D. R. Daprekar发现了一类称作Self-Numbers的数.对于每一个正整数n,我们定义d(n)为n加上它每一位数字的和.例如,d(75 ...
- [Python] Handle Exceptions to prevent crashes in Python
Exceptions cause your application to crash. Handling them allows you to recover gracefully and keep ...
- poj 1087 A Plug for UNIX(字符串编号建图)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14862 Accepted: 5026 ...
- POJ 2458 DFS+判重
题意: 思路: 搜+判重 嗯搞定 (听说有好多人用7个for写得-.) //By SiriusRen #include <bitset> #include <cstdio>0 ...
- Strtus2学习
Struts 2 体系结构 : 1.Web浏览器请求一个资源. 2.过滤器Dispatcher查找方法,确定适当的Action. 3.拦截器自动对请求应用通用功能,如验证和文件上传操作. 4.Acti ...
- js中的组合模式
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java 第三方库
转载自http://www.cnblogs.com/lucus-jia/p/3483511.html java 第三方库 总结一下常用的java第三方库,方便查询和使用,欢迎补充. 1.核心库 A ...
- linux操作指令:
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- CTF编程题-三羊献瑞(实验吧)解题随记
题目如下.解题步骤参考的是https://cloud.tencent.com/developer/news/373865中作者的思路. 1.首先,两个四位数相加等于一个五位数,那么这个五位数的第一位必 ...