我下载了一个C#开发的tftp的客户端,然后用它上传文件,时常文件传不全,就是说1M的文件只传了500K,但不是所有的文件,而且同一个文件每次传的情况不一样,有时候能传全有时候不行,不知道为什么,请各位大虾帮忙!
public bool Put(string LocalFile, string RemoteFile, string Host,
Modes Mode, int BlockSize, int Timeout)
{
int[] block = new int[2];
int bufferSize = BlockSize;
long fileSize, bytesSent = 0; BinaryReader BReader = new BinaryReader(File.Open(LocalFile, FileMode.Open));
FileInfo sendFile = new FileInfo(LocalFile); TFTPSession.OpCodes opCode = new TFTPSession.OpCodes(); IPHostEntry hostInfo = Dns.GetHostEntry(Host);
IPAddress address = hostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(address, 69);
EndPoint localEP = (remoteEP);
Socket UDPSock = new Socket
(remoteEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp); // Retrieve filesize for tsize option
fileSize = sendFile.Length; // Create initial request and buffer for response
byte[] sendData = _packetBuilder.Request (TFTPSession.OpCodes.WRQ,
RemoteFile, Mode, BlockSize, fileSize, Timeout);
byte[] recvData = new byte[bufferSize]; UDPSock.ReceiveTimeout = Timeout * 1000; // Send request and wait for response
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP); //Get TID
remoteEP.Port = ((IPEndPoint)localEP).Port; // Invoke Connected Event
Connected.Invoke(); while (true)
{
// Read opcode
opCode = _packetReader.ReadOpCode(recvData); // ACK packet
if (opCode == TFTPSession.OpCodes.ACK)
{
block = _packetBuilder.IncrementBock(recvData, block); sendData = BReader.ReadBytes(bufferSize);
bytesSent += sendData.Length; // Invoke Transferring Event
Transferring.Invoke(bytesSent, fileSize); sendData = _packetBuilder.Data(sendData, block[0], block[1]); // Check if this packet is the last
if (sendData.Length < bufferSize + 4)
{
// Send final data packet and wait for ack
while (true)
{
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP);
remoteEP.Port = ((IPEndPoint)localEP).Port; // Check the blocks and break free if equal
if (_packetReader.CompareBlocks(sendData, recvData))
break;
} // Invoke TransferFinished Event
TransferFinished.Invoke();
break;
}
} // OACK packet
else if (opCode == TFTPSession.OpCodes.OACK)
{
sendData = BReader.ReadBytes(bufferSize);
sendData = _packetBuilder.Data(sendData, 0, 1);
bytesSent += sendData.Length - 4; // Invoke Transferring Event
Transferring.Invoke(bytesSent, fileSize); if (fileSize == 0)
{
// Invoke TransferFinished Event
TransferFinished.Invoke();
break;
}
else
{ // Check if this packet is the last
if (sendData.Length < bufferSize + 4)
{
// Send final data packet and wait for ack
while (true)
{
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP);
remoteEP.Port = ((IPEndPoint)localEP).Port; // Check the blocks and break free if equal
if (_packetReader.CompareBlocks(sendData, recvData))
break;
}
// Invoke TransferFinished Event
TransferFinished.Invoke();
break;
}
}
}
else if (opCode == TFTPSession.OpCodes.ERROR)
{
ErrorPacket transferError = _packetReader.ReadError(recvData);
TransferFailed.Invoke(transferError.Code, transferError.Message);
break;
} // Send next packet
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP);
remoteEP.Port = ((IPEndPoint)localEP).Port;
}
BReader.Close();
UDPSock.Close(); // Invoke Disconnected Event
Disconnected.Invoke(); return true;
}
public bool Put(string LocalFile, string RemoteFile, string Host,
Modes Mode, int BlockSize, int Timeout)
{
int[] block = new int[2];
int bufferSize = BlockSize;
long fileSize, bytesSent = 0; BinaryReader BReader = new BinaryReader(File.Open(LocalFile, FileMode.Open));
FileInfo sendFile = new FileInfo(LocalFile); TFTPSession.OpCodes opCode = new TFTPSession.OpCodes(); IPHostEntry hostInfo = Dns.GetHostEntry(Host);
IPAddress address = hostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(address, 69);
EndPoint localEP = (remoteEP);
Socket UDPSock = new Socket
(remoteEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp); // Retrieve filesize for tsize option
fileSize = sendFile.Length; // Create initial request and buffer for response
byte[] sendData = _packetBuilder.Request (TFTPSession.OpCodes.WRQ,
RemoteFile, Mode, BlockSize, fileSize, Timeout);
byte[] recvData = new byte[bufferSize]; UDPSock.ReceiveTimeout = Timeout * 1000; // Send request and wait for response
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP); //Get TID
remoteEP.Port = ((IPEndPoint)localEP).Port; // Invoke Connected Event
Connected.Invoke(); while (true)
{
// Read opcode
opCode = _packetReader.ReadOpCode(recvData); // ACK packet
if (opCode == TFTPSession.OpCodes.ACK)
{
block = _packetBuilder.IncrementBock(recvData, block); sendData = BReader.ReadBytes(bufferSize);
bytesSent += sendData.Length; // Invoke Transferring Event
Transferring.Invoke(bytesSent, fileSize); sendData = _packetBuilder.Data(sendData, block[0], block[1]); // Check if this packet is the last
if (sendData.Length < bufferSize + 4)
{
// Send final data packet and wait for ack
while (true)
{
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP);
remoteEP.Port = ((IPEndPoint)localEP).Port; // Check the blocks and break free if equal
if (_packetReader.CompareBlocks(sendData, recvData))
break;
} // Invoke TransferFinished Event
TransferFinished.Invoke();
break;
}
} // OACK packet
else if (opCode == TFTPSession.OpCodes.OACK)
{
sendData = BReader.ReadBytes(bufferSize);
sendData = _packetBuilder.Data(sendData, 0, 1);
bytesSent += sendData.Length - 4; // Invoke Transferring Event
Transferring.Invoke(bytesSent, fileSize); if (fileSize == 0)
{
// Invoke TransferFinished Event
TransferFinished.Invoke();
break;
}
else
{ // Check if this packet is the last
if (sendData.Length < bufferSize + 4)
{
// Send final data packet and wait for ack
while (true)
{
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP);
remoteEP.Port = ((IPEndPoint)localEP).Port; // Check the blocks and break free if equal
if (_packetReader.CompareBlocks(sendData, recvData))
break;
}
// Invoke TransferFinished Event
TransferFinished.Invoke();
break;
}
}
}
else if (opCode == TFTPSession.OpCodes.ERROR)
{
ErrorPacket transferError = _packetReader.ReadError(recvData);
TransferFailed.Invoke(transferError.Code, transferError.Message);
break;
} // Send next packet
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP);
remoteEP.Port = ((IPEndPoint)localEP).Port;
}
BReader.Close();
UDPSock.Close(); // Invoke Disconnected Event
Disconnected.Invoke(); return true;
}
解决方案 »
- 界面上对datagridView新增、修改、删除后如何一次性更新到数据库
- C# socket 客户端和服务器端 发送接收图片
- 谁有关于多线程操作数据库的例子...?求..
- Arraylist 程序集是什么
- ===============c# 100分 如何生成和调用exe???=================
- 代码中执行存储过程参数化查询,在代码中设定的参数的长度与数据库中的字段的长度不一,有影响吗?
- 我是初学C#都,问一个简单的问题
- CSC.EXE 如何编译解决方案文件(*.SLN)
- 有问题讨教:在listbos中,如何绑定多个字段????请各位帮忙,急呀
- ▁▂▃▄▅▆▇█▉哪里有可免费下载的MCSE和MCSD试题库?高分求教!!过了以后,工资一般能到(涨)多少?▉█▇▆▅▄▃▂▁
- 任务管理中进程不能退出,请教!!!!!
- ListView的问题换行
public bool Put(string LocalFile, string RemoteFile, string Host,
Modes Mode, int BlockSize, int Timeout)
{
int[] block = new int[2];
int bufferSize = BlockSize;
long fileSize, bytesSent = 0; BinaryReader BReader = new BinaryReader(File.Open(LocalFile, FileMode.Open));
FileInfo sendFile = new FileInfo(LocalFile); TFTPSession.OpCodes opCode = new TFTPSession.OpCodes(); IPHostEntry hostInfo = Dns.GetHostEntry(Host);
IPAddress address = hostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(address, 69);
EndPoint localEP = (remoteEP);
Socket UDPSock = new Socket
(remoteEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp); // Retrieve filesize for tsize option
fileSize = sendFile.Length; // Create initial request and buffer for response
byte[] sendData = _packetBuilder.Request (TFTPSession.OpCodes.WRQ,
RemoteFile, Mode, BlockSize, fileSize, Timeout);
byte[] recvData = new byte[bufferSize]; UDPSock.ReceiveTimeout = Timeout * 1000; // Send request and wait for response
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP); //Get TID
remoteEP.Port = ((IPEndPoint)localEP).Port; // Invoke Connected Event
Connected.Invoke(); while (true)
{
// Read opcode
opCode = _packetReader.ReadOpCode(recvData); // ACK packet
if (opCode == TFTPSession.OpCodes.ACK)
{
block = _packetBuilder.IncrementBock(recvData, block); sendData = BReader.ReadBytes(bufferSize);
bytesSent += sendData.Length; // Invoke Transferring Event
Transferring.Invoke(bytesSent, fileSize); sendData = _packetBuilder.Data(sendData, block[0], block[1]); // Check if this packet is the last
if (sendData.Length < bufferSize + 4)
{
// Send final data packet and wait for ack
while (true)
{
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP);
remoteEP.Port = ((IPEndPoint)localEP).Port; // Check the blocks and break free if equal
if (_packetReader.CompareBlocks(sendData, recvData))
break;
} // Invoke TransferFinished Event
TransferFinished.Invoke();
break;
}
} // OACK packet
else if (opCode == TFTPSession.OpCodes.OACK)
{
sendData = BReader.ReadBytes(bufferSize);
sendData = _packetBuilder.Data(sendData, 0, 1);
bytesSent += sendData.Length - 4; // Invoke Transferring Event
Transferring.Invoke(bytesSent, fileSize); if (fileSize == 0)
{
// Invoke TransferFinished Event
TransferFinished.Invoke();
break;
}
else
{ // Check if this packet is the last
if (sendData.Length < bufferSize + 4)
{
// Send final data packet and wait for ack
while (true)
{
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP);
remoteEP.Port = ((IPEndPoint)localEP).Port; // Check the blocks and break free if equal
if (_packetReader.CompareBlocks(sendData, recvData))
break;
}
// Invoke TransferFinished Event
TransferFinished.Invoke();
break;
}
}
}
else if (opCode == TFTPSession.OpCodes.ERROR)
{
ErrorPacket transferError = _packetReader.ReadError(recvData);
TransferFailed.Invoke(transferError.Code, transferError.Message);
break;
} // Send next packet
UDPSock.SendTo(sendData, remoteEP);
UDPSock.ReceiveFrom(recvData, ref localEP);
remoteEP.Port = ((IPEndPoint)localEP).Port;
}
BReader.Close();
UDPSock.Close(); // Invoke Disconnected Event
Disconnected.Invoke(); return true;
}