I have set up a server, and am trying to send a file to a client. The issue I'm having is that the file received on the client is sometimes either smaller, or bigger than the original file.
Here is the code I'm using:
Server:
class FileSendServer
{
Thread t1;
byte[] clientData;
public FileSendServer()
{
}
public void letsGo()
{
t1 = new Thread(new ThreadStart(StartListening));
t1.Start();
}
public void setFileLoc(string floc)
{
string filePath = "";
string fileName = floc;
fileName = fileName.Replace("\\", "/");
while (fileName.IndexOf("/") > -1)
{
filePath += fileName.Substring(0, fileName.IndexOf("/") + 1);
fileName = fileName.Substring(fileName.IndexOf("/") + 1);
}
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
byte[] fileData = File.ReadAllBytes(filePath + fileName);
clientData = new byte[4 + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
}
public static ManualResetEvent allDone = new ManualResetEvent(false);
public void StartListening()
{
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 9050);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(ipEnd);
listener.Listen(100);
while (true)
{
allDone.Reset();
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
allDone.WaitOne();
}
}
catch (Exception ex)
{
}
}
public void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
handler.Send(clientData);
handler.Close();
}
}
Client:
class FileReceiveClient
{
int flag = 0;
string receivedPath = "";
public delegate void MyDelegate();
Form1 myForm1;
public FileReceiveClient(Form1 x)
{
myForm1 = x;
ReceiveFile();
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
}
public void ReceiveFile()
{
try
{
IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("192.168.1.104"), 9050);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
clientSock.Connect(ipEnd);
StateObject state = new StateObject();
state.workSocket = clientSock;
clientSock.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
flag = 0;
}
catch (Exception ex)
{
}
}
public void ReadCallback(IAsyncResult ar)
{
int fileNameLen = 1;
String content = String.Empty;
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
if (flag == 0)
{
fileNameLen = BitConverter.ToInt32(state.buffer, 0);
string fileName = Encoding.UTF8.GetString(state.buffer, 4, fileNameLen);
receivedPath = "C:/RBParty/Songs/" + fileName;
myForm1.fileLoc = receivedPath;
flag++;
}
if (flag >= 1)
{
try
{
BinaryWriter writer = new BinaryWriter(File.Open(receivedPath, FileMode.Append));
if (flag == 1)
{
writer.Write(state.buffer, 4 + fileNameLen, bytesRead - (4 + fileNameLen));
flag++;
}
else
writer.Write(state.buffer, 0, bytesRead);
writer.Close();
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
catch (Exception ee)
{
}
}
}
else
{
Thread.CurrentThread.Abort();
}
}
}
Any help is appreciated. Thank you in advance.