I want to write a Tcp file transfer program , the problem is that when i send the file size and file name the client doesn't receive the as two separate packages but as a package witch contains both the file name , size and the file content.
This is my code:
Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe= new IPEndPoint((Dns.GetHostByName(Dns.GetHostName())).AddressList[0],8000);
sk.Bind(ipe);
sk.Listen(5);
Socket sc = sk.Accept();
FileInfo fi = new FileInfo(line);
long size = fi.Length,sent = 0;
string name = fi.Name;
System.IO.StreamReader sr = new StreamReader(fi.OpenRead());
byte[] buffer = new byte[1024];
char[] char_buffer = new char[1024];
try
{
buffer = System.Text.Encoding.ASCII.GetBytes(fi.Name);
sc.Send(buffer);
buffer = System.Text.Encoding.ASCII.GetBytes(size.ToString());
sc.Send(buffer);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
while (sr.EndOfStream != true)
{
sr.Read(char_buffer, 0, 1024);
sent += 1024;
sc.Send(System.Text.Encoding.ASCII.GetBytes(char_buffer));
textBox1.Text += "Trimis cu succes ramas:" + (((decimal)sent / size) * 100).ToString() + Environment.NewLine;
}
sc.Shutdown(SocketShutdown.Both);
sk.Shutdown(SocketShutdown.Both);
sk.Close();
sr.Close();
sc.Close();