title: Half file transferring in c# via socket?
body: <p>I am developing the file transfer application via middle level greedy routing in which file sends to greedy and greedy sends file again to router
  but he problem is some time client receives complete file and some time it receive some part of file</p>
  
  <p>Here my code goes
  Server Side</p>
  
  <pre><code>IPAddress[] ipAddress = Dns.GetHostAddresses("127.0.0.1");
              IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5655);
              Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  
  
              string filePath = "";
  
  
              fileDes = fileDes.Replace("\\", "/");
              while (fileDes.IndexOf("/") &gt; -1)
              {
                  filePath += fileDes.Substring(0, fileDes.IndexOf("/") + 1);
                  fileDes = fileDes.Substring(fileDes.IndexOf("/") + 1);
              }
  
  
              byte[] fileNameByte = Encoding.ASCII.GetBytes(fileDes);
  
              lblError.Text = "";
              lblError.Text = "Buffering ...";
              byte[] fileData = File.ReadAllBytes(filePath + fileDes);
              byte[] 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);
  
              lblError.Text = "";
              lblError.Text = "Connection to server ...";
              clientSock.Connect(ipEnd);
  
              lblError.Text = "";
              lblError.Text = "File sending...";
           //   System.Threading.Thread.Sleep(1000);
              clientSock.Send(clientData);
              label3.Text = clientData.Length.ToString();
  
              lblError.Text = "";
              lblError.Text = "Disconnecting...";
              clientSock.Close();
  
              lblError.Text = "";
              lblError.Text = "File transferred.";
  </code></pre>
  
  <p>In client</p>
  
  <pre><code>class DestCode
  {
      IPEndPoint ipEnd;
      Socket sock;
      public DestCode()
      {
         ipEnd = new IPEndPoint(IPAddress.Any, 5656);
         sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
         sock.Bind(ipEnd);
      }
      public static string receivedPath;
      public static string curMsg = "Stopped";
      public static int res;
      public  void StartServer()
      {
          try
          {
              curMsg = "Starting...";
              sock.Listen(100);
  
              curMsg = "Running and waiting to receive file.";
              Socket clientSock = sock.Accept();
  
              byte[] clientData = new byte[1024 * 5000];
  
              int receivedBytesLen = clientSock.Receive(clientData);
  
              curMsg = "Receiving data...";
  
              int fileNameLen = BitConverter.ToInt32(clientData, 0);
              string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
  
              BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath +"/"+ fileName, FileMode.Append)); ;
              bWrite.Write(clientData,4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
              res = receivedBytesLen;
              if (receivedPath == "")
              {
                  MessageBox.Show("No Path was selected to Save the File");
              }
              curMsg = "Saving file...";
  
              bWrite.Close();
              clientSock.Close();
              curMsg = "File Received ...";
  
              StartServer();
  
          }
          catch (Exception ex)
          {
              curMsg = "File Receving error.";
          }
      }
  }
  </code></pre>
  
  <p>In Greedy</p>
  
  <pre><code>class ReceiverCode
  {
      IPEndPoint ipEnd;
      Socket sock;
      public ReceiverCode()
      {
          ipEnd = new IPEndPoint(IPAddress.Any, 5655);
          sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
          sock.Bind(ipEnd);
      }
      public static string receivedPath;
      public static string curMsg = "Stopped";
      public static string Rout = "";
      public static int rlength = 0;
      public static string MsgStatus = "";
      public static byte[] send;
      public void StartServer()
      {
          try
          {
              curMsg = "Starting...";
              sock.Listen(100);
  
              curMsg = "Running and waiting to receive file.";
              Socket clientSock = sock.Accept();
  
              byte[] clientData = new byte[1024 * 5000];
  
              int receivedBytesLen = clientSock.Receive(clientData);
             System.Threading.Thread.Sleep(5000);
              rlength = receivedBytesLen;
              curMsg = "Receiving data...";
  
              int receive = clientSock.Receive(clientData);
  
  
              send = new byte[receivedBytesLen];
              Array.Copy(clientData, send, receivedBytesLen);
              Rout = "Start";
              clientSock.Close();
              curMsg = "Reeived &amp; Saved file; Server Stopped.";
              StartServer();
  
          }
          catch (Exception ex)
          {
              curMsg = "File Receving error.";
          }
      }
  
  
  }
  </code></pre>
  
  <p>Please somebody help me</p>