Dears
i have the following method that sends files using sockets
public static void SendFile(string path, string fName,int nbOfFiles ,string strCommand, string strIPadd, int iPortNb,ref Socket clientSock)
{
try
{
clientSock.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendBuffer, 0);
byte[] clientData;
byte[] fileName = Encoding.UTF8.GetBytes(fName);
byte[] fileData = File.ReadAllBytes(path);
byte[] fileNameLen = BitConverter.GetBytes(fileName.Length);
byte[] bCommand = Encoding.UTF8.GetBytes(strCommand);
byte[] bComandLength = BitConverter.GetBytes(bCommand.Length);
byte[] bNBofFiles = { Convert.ToByte(nbOfFiles), };
clientData = new byte[12 + bCommand.Length + fileName.Length + fileData.Length];
bComandLength.CopyTo(clientData, 0);
bCommand.CopyTo(clientData, 4);
bNBofFiles.CopyTo(clientData, 4 + bCommand.Length);
fileNameLen.CopyTo(clientData, 8 + bCommand.Length);
fileName.CopyTo(clientData, 12 + bCommand.Length);
fileData.CopyTo(clientData, 12 + fileName.Length + bCommand.Length);
clientSock.Send(clientData);
strBuild.AppendEntry("Sending Files to Server Succeeded");
}
catch (Exception e)
{
strBuild.AppendEntry(e.Message);
}
}
and the method for receiving is
receivedBytesLen = clientSock.Receive(clientDataFiles);
int commandLength = BitConverter.ToInt32(clientDataFiles, 0);
strCommandReceived = Encoding.ASCII.GetString(clientDataFiles, 4, commandLength);
iNbofFiles = BitConverter.ToInt32(clientDataFiles, 4 + commandLength);
int fileNameLen = BitConverter.ToInt32(clientDataFiles, 8 + commandLength);
fileName = Encoding.ASCII.GetString(clientDataFiles, 12 + commandLength, fileNameLen);
strDirectory = terminal.TerminalPath + "\\" + terminal.TerminalID + "\\" + command.CommandPath;
if (!Directory.Exists(strDirectory))
Directory.CreateDirectory(strDirectory);
if (iNbofFiles > 1)
{
filePath = terminal.TerminalPath + "\\" + terminal.TerminalID + "\\" + command.CommandPath + "\\" + fileName;
}
else
{
filePath = terminal.TerminalPath + "\\" + terminal.TerminalID + "\\" + command.CommandPath + "\\" + command.FileName;
}
if (!System.IO.File.Exists(filePath))
System.IO.File.Create(filePath).Close();
BinaryWriter bWrite = new BinaryWriter(File.Open(filePath, FileMode.Create)); ;
bWrite.Write(clientDataFiles, 12 + fileNameLen + commandLength, receivedBytesLen - 12 - fileNameLen - commandLength);
bWrite.Close();
The problem "Index and count must refer to a location within the buffer" is taking place when i send more than one large files. If i send 3 small files this problem doesn't occur. Any ideas of how i can clean the buffer before sending the next file
i tried to use
clientSock.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendBuffer, 0);
and i used 1 instead of 0 and nothing changed
Regards,