文件传输1Mb及以上通过TCP / IP或UDP [英] File transfer 1Mb and above Over TCP/IP or UDP

查看:104
本文介绍了文件传输1Mb及以上通过TCP / IP或UDP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题以下代码是我无法传输整个文件。如果文件大小为35kb。它只能传输10 kb或5 kb。





对于客户我使用

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

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

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();
               }
           }
       }

推荐答案

我觉得这行是问题:

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



因为我认为你期待收到在这一次通话中发送的整个数据。它不像那样工作(我已经被自己弄得一团糟)。这将以接收到的数据块的形式返回数据,因此您需要做的是在循环中重复调用它,每次将累积读取的内容复制到缓冲区中,直到接收到正确的字节数或(从内存中)返回0作为读取的字节数。



希望有意义 - 如果需要,将澄清。


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.


这篇关于文件传输1Mb及以上通过TCP / IP或UDP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆