简体   繁体   English

listener.AcceptTcpClient()在C#中不返回任何东西

[英]listener.AcceptTcpClient() doesn't return any thing in c#

Here us my code . 这是我们的代码。 i want to ftp the file in a loop ? 我想以循环方式将文件ftp传送吗? please suggest the appropriate way to do so ? 请建议这样做的适当方法? Your answers will be highly appreciated. 您的回答将受到高度赞赏。 Some times it work perfectly but often it is unable to FTP the file to the FTP server . 有时它可以正常工作,但通常无法将文件通过FTP发送到FTP服务器。

   delegate void mThread(ref bool isRunning);
        delegate void AccptTcpClnt(ref TcpClient client, TcpListener listener);
        public static void AccptClnt(ref TcpClient client, TcpListener listener)
        {
            if (client == null)
                client = listener.AcceptTcpClient();
        }
        public void SendStream(Stream stream, string remoteFileName, FTPFileTransferType type, lblStatusDelegate olblStatusDelegate)
  {
            bool isRunning = true;
   LockTcpClient();
   TcpListener listner = null;
   TcpClient client = null;
   NetworkStream networkStream = null;
   ArrayList tempMessageList = new ArrayList();
   int returnValue = 0;
   string returnValueMessage = "";
   tempMessageList = new ArrayList();

   SetTransferType(type);

   if(this.mode == FTPMode.Active)
   {
    listner = CreateDataListner();
    listner.Start();
   }
   else
   {
    client = CreateDataClient();
   }

   tempMessageList = SendCommand("STOR " + remoteFileName);
   returnValue = GetMessageReturnValue((string)tempMessageList[0]);
   if(!(returnValue == 150 || returnValue == 125))
   {
    throw new Exception((string)tempMessageList[0]);
   }

   if(this.mode == FTPMode.Active)
   {
                AccptTcpClnt t = new AccptTcpClnt(AccptClnt);
                Thread tt = new Thread(() => t(ref client, listner));
                tt.IsBackground = true;
                tt.Start(); 
                while (isRunning && tt.IsAlive && client== null)
                Thread.Sleep(500); //change the time as you prefer
   }
            if (client != null)
            {
                networkStream = client.GetStream();

                Byte[] buffer = new Byte[BLOCK_SIZE];
                int bytes = 0;
                int totalBytes = 0;
                string TotalLength = BytesFormat(stream.Length, "kb");
                while (totalBytes < stream.Length)
                {
                    bytes = (int)stream.Read(buffer, 0, BLOCK_SIZE);
                    totalBytes = totalBytes + bytes;
                    networkStream.Write(buffer, 0, bytes);
                    if (olblStatusDelegate != null)
                    {
                        olblStatusDelegate.Invoke("Uploaded " + BytesFormat(totalBytes, "kb") + " of " + TotalLength);
                    }
                }

                networkStream.Close();
                client.Close();

                if (this.mode == FTPMode.Active)
                {
                    listner.Stop();
                }

                if (tempMessageList.Count == 1)
                {
                    tempMessageList = Read();
                    returnValue = GetMessageReturnValue((string)tempMessageList[0]);
                    returnValueMessage = (string)tempMessageList[0];
                }
                else
                {
                    returnValue = GetMessageReturnValue((string)tempMessageList[1]);
                    returnValueMessage = (string)tempMessageList[1];
                }

                if (!(returnValue == 226))
                {
                    throw new Exception(returnValueMessage);
                }
            }
   UnlockTcpClient();
  }

It seems that it is giving connection timeout exception. 似乎它给连接超时异常。 Have you checked by debugging? 您是否已通过调试检查?
Normally the timeout exception is occur in 通常,超时异常发生在

listener.AcceptTcpClient();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM