我下载了一个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;
}
解决方案 »
- 打印凭条
- WPF命名空间报错
- 100分求《.NET CLI》 (第3版) 中文版 下载地址
- winform中webbrowser的打印问题
- word文档与rpc服务器的关系
- 简单文字显示问题。解决立即接贴。
- 怎样将"097,098,099,100,101,005"这些数字前面的"0"去掉啊?
- [急急急]一个常见又容易被忽视的现象。
- C#如何操作IIS,检查一个站点的虚拟目录是否存在?在虚拟目录下新建虚拟目录及其修改和删除???不要让小妹被老板炒了啊!帮个忙哦!
- 请问验证整数的正则表达式怎样写??有没有网上可以查的资源,是做参考的,找了半天没找见
- 任务管理中进程不能退出,请教!!!!!
- 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;
}