利用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 ...
随机推荐
- javaScript 预编译过程浅尝
javaScript 预编译过程 1.创建AO对象(Activation Object) AO{ a: } 2.找形参和变量声明,将变量和形参作为AO属性名,值为undefined AO{ a:und ...
- Scrapy中将数据保存至数据库
一.在settings.py文件中配置数据库连接参数 # 数据库连接参数 DB_HOST = '192.168.183.1' DB_PORT = 3306 DB_USER = 'root' DB_PA ...
- java中锁的理解
在并发编程中,经常遇到多个线程访问同一个 共享资源 ,这时候作为开发者必须考虑如何维护数据一致性,在java中synchronized关键字被常用于维护数据一致性.synchronized机制是给共享 ...
- Python基础班培训视频课程
课程目录:│ ├─第01天视频│ │ 01-课程介绍.avi│ │ 02-什么是操作系统.avi│ │ 03-生活中的操作系统.avi│ │ 04-操 ...
- Nginx模型 & 惊群问题
这篇写的不错 http://www.cnblogs.com/linguoguo/p/5511293.html Nginx为啥性能高-多进程异步IO模型 1. 对于每个worker进程来说,独立的进程, ...
- ios in-house 公布整个过程(startssl认证)
首先大体说一下步骤: 1.申请苹果enterprise 账号 为应用生成app id,provision profile等 详见:http://www.th7.cn/Program/IOS/20131 ...
- android中9-patch图片的使用
看了非常多文章的介绍,9.png图片有两种区域:可扩展区和内容显示区. 弄了半天才明确什么叫做可扩展区,什么叫做内容显示区. 分享一下自己的理解. 下图是某博客的截图: 图片来自:http://blo ...
- 第一个WPF
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- dfs算法中求数列的组合
/* 从13个书中挑选5个值,他们的组合可能是 什么, 如下代码 dfs深度遍历, 和全排列是一种方法,但是思路不同 */ public class Main { static int count = ...
- POJ 2185 正解 KMP
题意: 思路: 把每一行压成一个数 求一下 KMP 把每一列压成一个数 求一下KMP 答案就是两个周期之积 网上的好多题解都是错的---------.. //By SiriusRen #include ...