Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Issue With Below code is I'm unable to transfer whole file. If the file size 35kb. It'll only transfer 10 kb or 5 kb only.


For Client Im using
C#
class FTClientCode
      {
          public static string curMsg = "Idle";
          public static void SendFile(string fileName,string add)
          {
              try
              {

                  IPAddress test1 = IPAddress.Parse(add);
                  //IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
                  // IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
                  IPEndPoint ipEnd = new IPEndPoint(test1, 5656);
                  Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);


                  string filePath = "";

                  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);
                  if (fileNameByte.Length > 131072)
                  {
                      curMsg = "Unable To Send....";
                      return;
                  }

                  curMsg = "Buffering ...";
                  byte[] fileData = File.ReadAllBytes(filePath + fileName);
                  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);

                  curMsg = "Connection to server ...";
                  clientSock.Connect(ipEnd);

                  curMsg = "sending...";
                  clientSock.Send(clientData);

                  curMsg = "Disconnecting...";
                  clientSock.Close();
                  curMsg = "SENT!";

              }
              catch (Exception ex)
              {
                  if (ex.Message == "No connection could be made because the target machine actively refused it")
                      curMsg = "Sending failed.";
                  else
                      curMsg = "Sending failed.";
              }

          }
      }

For Server I'm using
C#
class FTServerCode
       {
           IPEndPoint ipEnd;
           Socket sock;
           public FTServerCode()
           {
               try
               {
                   //  IPAddress test1 = IPAddress.Parse("192.168.1.8");
                   ipEnd = new IPEndPoint(IPAddress.Any, 5656);
                   //ipEnd = new IPEndPoint(test1, 5656);
                   sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                   sock.Bind(ipEnd);
               }
               catch (System.Exception)
               {

               }

           }
           public static string receivedPath;
           public static string curMsg = "Stopped";
           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);

                   curMsg = "Saving file...";

                   bWrite.Close();
                   clientSock.Close();
                   curMsg = "Received & Saved file; Server Stopped.";

                   StartServer();
               }
               catch (Exception)
               {
                   curMsg = "Receiving error.";
                   StartServer();
               }
           }
       }
Posted
Updated 3-Jan-14 0:02am
v2

1 solution

I think this line is the problem:
int receivedBytesLen = clientSock.Receive(clientData);

In that I think you are expecting to receive the whole data sent in this single call. It doesn't work like that (I've been caught out myself with this). This will return the data in chunks as it is received thus what you need to do is call this repeatedly in a loop each time copying what's read accumulatively into a buffer until the correct number of bytes have been received or (from memory) it returns 0 as the number of bytes read.

Hope that makes sense - will clarify if need be.
 
Share this answer
 
Comments
sharathkumar27 5-Jan-14 23:57pm    
thank you Soooo Much.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900