//接受对方响应&发送文件
private void ReceiveResponseListening()
{
this.socketReceiveResponse.Listen(10);
while (true)
{
FileStream fileStream = null;
try
{
Socket socketTemp = this.socketReceiveResponse.Accept();
//接受对象长度
byte[] buffer = new byte[4];
int bufferLength = socketTemp.Receive(buffer);
int bufferSize = BitConverter.ToInt32(buffer, 0);
//接受文件类的对象
byte[] bufferData = new byte[bufferSize];
int dataCount = socketTemp.Receive(bufferData);
MemoryStream memStream = new MemoryStream(bufferData);
BinaryFormatter formatter = new BinaryFormatter();
FileInfo fileInfo = (FileInfo)formatter.Deserialize(memStream);
memStream.Close();
string ip = fileInfo.Ip;//对方提供的接受文件的"仓库"IP
int port = fileInfo.Port;//对方提供的接受文件的"仓库"Port
int portResponseOnTransmiting = fileInfo.PortOnTransmiting;//对方提供接收传输时的端口
bool response = fileInfo.IsReceive;//对方给予响应是否同意接受
if (response)//对方确定要接受数据,我方开始发送数据
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
Socket socketToSend = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socketToSend.Connect(endPoint);
if (socketToSend.Connected)
{
fileStream = new FileStream(this.pathOfFileToSend, FileMode.Open);
long fileSize = fileStream.Length;
if (this.BeginToSend != null)
{
string fileNameToSend = this.pathOfFileToSend.Substring(this.pathOfFileToSend.LastIndexOf(@"\") + 1);
this.BeginToSend(this, new BeginToSendEventArgs(fileNameToSend, fileSize));
}
//首先将文件大小发送给接收方,以方便接收方接收数据
socketToSend.Send(BitConverter.GetBytes(fileSize));
int packageSize = 2 * 1024 * 1024;//每个包2M
int packageCount = (int)(fileSize / packageSize);
int rest = (int)(fileSize % packageSize);
byte[] bufferOfData = null;
for (int index = 0; index < packageCount; index++)
{
if (this.isReceiveGlobal == 1) return;//全局监控对方响应 对方取消发送或接收时 置为1
bufferOfData = new byte[packageSize];
//将文件数据读入内存
fileStream.Read(bufferOfData, 0, packageSize);
int byteCount=socketToSend.Send(bufferOfData);
if (this.TransmitSending != null)
{
this.TransmitSending(this, new TransmitSendingEventArgs(byteCount, fileSize, portResponseOnTransmiting, ip));
}
bufferOfData = null;
}
if (rest != 0)//不整除 还剩最后一个包
{
if (this.isReceiveGlobal == 1) return;
bufferOfData = new byte[rest];
fileStream.Read(bufferOfData, 0, rest);
socketToSend.Send(bufferOfData);
if (this.TransmitSending != null)
{
this.TransmitSending(this, new TransmitSendingEventArgs(rest, fileSize, portResponseOnTransmiting, ip));
}
}
}
}
}
catch
{
//throw new Exception("文件传输出错");
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
//传输完成或中断 移除控件
if (this.RemoveCtrl != null)
{
this.RemoveCtrl(this, new RemoveCtrlEventArgs(null));
}
}
}
}
//接受文件的监听
private void ReceiveDataListening()
{
this.socketReceiveData.Listen(10);
while (true)
{
FileStream fileStream = null;
try
{
Socket socketTemp = this.socketReceiveData.Accept();
//接受文件长度
byte[] buffer = new byte[8];
int bufferLength = socketTemp.Receive(buffer);
long fileSize = BitConverter.ToInt64(buffer, 0);
long byteReceived = 0;
//另存为还是默认保存
string fullFilePath = (string.IsNullOrEmpty(this.saveAsPathOfReceive)) ? (string.Format(@"{0}\{1}", this.defaultPathOfReceive, this.fileNameOfReceive)) : this.saveAsPathOfReceive;
fileStream = new FileStream(fullFilePath, FileMode.Create);
if (this.BeginToReceive != null)
{
string fileNameToReceive = this.fileNameOfReceive;
this.BeginToReceive(this, new BeginToReceiveEventArgs(fileNameOfReceive, fileSize));
}
int packageSize = 1 * 1024 * 1024;//每个包1M
//分包接受文件
while (byteReceived < fileSize)
{
if (this.isReceiveGlobal == 1) return;//全局监控对方响应 对方取消发送或接收时 置为1
buffer = new byte[packageSize];
int byteCount = socketTemp.Receive(buffer);
byteReceived += byteCount;
fileStream.Write(buffer, 0, byteCount);
if (this.TransmitReceiving != null)
{
this.TransmitReceiving(this, new TransmitReceivingEventArgs(byteCount, fileSize));
}
}
}
catch
{
//throw new Exception("对方取消文件传输");
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
//传输完成或中断 移除控件
if (this.RemoveCtrl != null)
{
this.RemoveCtrl(this, new RemoveCtrlEventArgs(null));
}
}
}
}
//接收正在传输文件时的对方取消响应(eg.发送方突然不想发了或接收方不想接了)
private void ReceiveResponseOnTransmitingListening()
{
this.socketReceiveResponseOnTransmiting.Listen(10);
while (true)
{
try
{
Socket socketTemp = this.socketReceiveResponseOnTransmiting.Accept();
byte[] buffer = new byte[1];//专门接收bool(占1个字节)类型的数组
socketTemp.Receive(buffer);
bool isReceive = BitConverter.ToBoolean(buffer, 0);
if (false == isReceive)//对方取消接收或发送了
{
this.isReceiveGlobal = 1;//置1
}
}
finally
{
//传输完成或中断 移除控件
if (this.RemoveCtrl != null)
{
this.RemoveCtrl(this, new RemoveCtrlEventArgs(null));
}
}
}
}
}
}
这是我写的文件传输组件中3个核心方法 觉得应该没什么问题啊 可是每次传输文件 占用CPU60% 我快崩溃了
请使用BeginXXX方法来实现你的需求.
试下在循环里加上 Thread.Sleep(int)
哈哈 我刚遇到一样的问题 自己解决了。看了下你的代码, while(true)的问题 应该先判断缓冲区有没有数据 如果有数据 一直不停的接收 没有数据,线程sleep()一下 不然一直while的话CPU占用率肯定一直很高。