/// <summary>
/// Waits for a specific string to be found in the stream from the server
/// </summary>
/// <param name="DataToWaitFor">The string to wait for</param>
/// <returns>Always returns 0 once the string has been found</returns>
public int WaitFor(string DataToWaitFor)
{
// Get the starting time
long lngStart = DateTime.Now.AddSeconds(this.timeout).Ticks;
long lngCurTime = 0; while (strWorkingData.ToLower().IndexOf(DataToWaitFor.ToLower()) == -1)
{
// Timeout logic
lngCurTime = DateTime.Now.Ticks;
if (lngCurTime > lngStart)
{
throw new Exception("Timed Out waiting for : " + DataToWaitFor);
}
Thread.Sleep(1);
}
strWorkingData = "";
return 0;
}


/// <summary>
/// Waits for one of several possible strings to be found in the stream from the server
/// </summary>
/// <param name="DataToWaitFor">A delimited list of strings to wait for</param>
/// <param name="BreakCharacters">The character to break the delimited string with</param>
/// <returns>The index (zero based) of the value in the delimited list which was matched</returns>
public int WaitFor(string DataToWaitFor,string BreakCharacter)
{
// Get the starting time
long lngStart = DateTime.Now.AddSeconds(this.timeout).Ticks;
long lngCurTime = 0; string[] Breaks = DataToWaitFor.Split(BreakCharacter.ToCharArray());
int intReturn = -1; while (intReturn == -1)
{
// Timeout logic
lngCurTime = DateTime.Now.Ticks;
if (lngCurTime > lngStart)
{
throw new Exception("Timed Out waiting for : " + DataToWaitFor);
}

Thread.Sleep(1);
for (int i = 0 ; i < Breaks.Length ; i++)
{
if (strWorkingData.ToLower().IndexOf(Breaks[i].ToLower()) != -1)
{
intReturn = i ;
}
}
}
return intReturn; }
/// <summary>
/// Sends a message to the server
/// </summary>
/// <param name="Message">The message to send to the server</param>
/// <param name="SuppressCarriageReturn">True if you do not want to end the message with a carriage return</param>
public void SendMessage(string Message, bool SuppressCarriageReturn)
{
strFullLog += "\r\nSENDING DATA ====> " + Message.ToUpper() + "\r\n";
this.mainForm.ShowPanelText("SENDING DATA ====> " + Message.ToUpper()); if (! SuppressCarriageReturn)
{
DoSend(Message + "\r");
}
else
{
DoSend(Message);
}
}
/// <summary>
/// Sends a message to the server, automatically appending a carriage return to it
/// </summary>
/// <param name="Message">The message to send to the server</param>
public void SendMessage(string Message)
{
strFullLog += "\r\nSENDING DATA ====> " + Message.ToUpper() + "\r\n"; DoSend(Message + "\r");
}
/// <summary>
/// Waits for a specific string to be found in the stream from the server.
/// Once that string is found, sends a message to the server
/// </summary>
/// <param name="WaitFor">The string to be found in the server stream</param>
/// <param name="Message">The message to send to the server</param>
/// <returns>Returns true once the string has been found, and the message has been sent</returns>
public bool WaitAndSend(string WaitFor,string Message)
{
this.WaitFor(WaitFor);
SendMessage(Message);
return true;
}
/// <summary>
/// Sends a message to the server, and waits until the designated
/// response is received
/// </summary>
/// <param name="Message">The message to send to the server</param>
/// <param name="WaitFor">The response to wait for</param>
/// <returns>True if the process was successful</returns>
public int SendAndWait(string Message, string WaitFor)
{
SendMessage(Message);
this.WaitFor(WaitFor);
return 0;
} public int SendAndWait(string Message, string WaitFor, string BreakCharacter)
{
SendMessage(Message);
int t = this.WaitFor(WaitFor,BreakCharacter);
return t;
}
/// <summary>
/// A full log of session activity
/// </summary>
public string SessionLog
{
get 
{
return strFullLog;
}
}
/// <summary>
/// Clears all data in the session log
/// </summary>
public void ClearSessionLog()
{
strFullLog = "";
}
/// <summary>
/// Searches for two strings in the session log, and if both are found, returns
/// all the data between them.
/// </summary>
/// <param name="StartingString">The first string to find</param>
/// <param name="EndingString">The second string to find</param>
/// <param name="ReturnIfNotFound">The string to be returned if a match is not found</param>
/// <returns>All the data between the end of the starting string and the beginning of the end string</returns>
public string FindStringBetween(string StartingString, string EndingString, string ReturnIfNotFound)
{
int intStart;
int intEnd; intStart = strFullLog.ToLower().IndexOf(StartingString.ToLower());
if (intStart == -1)
{
return ReturnIfNotFound;
}
intStart += StartingString.Length; intEnd = strFullLog.ToLower().IndexOf(EndingString.ToLower(),intStart); if (intEnd == -1)
{
// The string was not found
return ReturnIfNotFound;
} // The string was found, let's clean it up and return it
return strFullLog.Substring(intStart, intEnd-intStart).Trim();
}
}
}

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace _YUJUNCAN
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox txtbRemoteIP;
    private System.Windows.Forms.TextBox txtbRemotePort;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox txtbToSend;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Button btnSend;
    private System.Windows.Forms.TextBox txtbToReceive;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.StatusBarPanel sbpSimpleText;
    private System.Windows.Forms.Button btnExit;
    private System.Windows.Forms.StatusBar statusBar;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.Button btnToConnect;
    private System.Windows.Forms.TextBox txtbTimeOut;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Button btnDisconnect;
    private System.Windows.Forms.Button btnWanIP;
    private ScriptingTelnet _ScriptingTelnet = null;
    private System.Windows.Forms.ContextMenu contextMenu;
    private System.Windows.Forms.MenuItem mitemClearAllText;
    private System.Windows.Forms.TextBox txtbRemoteUser;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.Label label7;
    private bool _AutoShow = true;
    private string sIP = string.Empty,sUser = string.Empty,sPassword = string.Empty,sPort = "23",
    sTimeOut = "30",sLocalIP = string.Empty,sWanIP = string.Empty,sWanMAC = string.Empty,
    sLocalMAC = string.Empty,sLocalSubNet = string.Empty,sWanSubNet = string.Empty;
    private int nPort = 23,nTimeOut = 30;
    private System.Windows.Forms.TextBox txtbRemotePass; public bool AutoShow
    {
    get
    {
    return _AutoShow;
    }
    set
    {
    if (_AutoShow != value)
    {
    _AutoShow = value;
    }
         }
    }
    public ScriptingTelnet scriptingTelnet 
    {
    get
    {
    return _ScriptingTelnet;
    }
    } public MainForm()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
      

  2.   

    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.txtbRemoteIP = new System.Windows.Forms.TextBox();
    this.txtbRemotePort = new System.Windows.Forms.TextBox();
    this.label2 = new System.Windows.Forms.Label();
    this.txtbToSend = new System.Windows.Forms.TextBox();
    this.label3 = new System.Windows.Forms.Label();
    this.btnSend = new System.Windows.Forms.Button();
    this.txtbToReceive = new System.Windows.Forms.TextBox();
    this.label4 = new System.Windows.Forms.Label();
    this.statusBar = new System.Windows.Forms.StatusBar();
    this.sbpSimpleText = new System.Windows.Forms.StatusBarPanel();
    this.btnExit = new System.Windows.Forms.Button();
    this.btnToConnect = new System.Windows.Forms.Button();
    this.txtbTimeOut = new System.Windows.Forms.TextBox();
    this.label5 = new System.Windows.Forms.Label();
    this.btnDisconnect = new System.Windows.Forms.Button();
    this.btnWanIP = new System.Windows.Forms.Button();
    this.contextMenu = new System.Windows.Forms.ContextMenu();
    this.mitemClearAllText = new System.Windows.Forms.MenuItem();
    this.txtbRemoteUser = new System.Windows.Forms.TextBox();
    this.label6 = new System.Windows.Forms.Label();
    this.txtbRemotePass = new System.Windows.Forms.TextBox();
    this.label7 = new System.Windows.Forms.Label();
    ((System.ComponentModel.ISupportInitialize)(this.sbpSimpleText)).BeginInit();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.AutoSize = true;
    this.label1.Location = new System.Drawing.Point(48, 40);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(72, 17);
    this.label1.TabIndex = 0;
    this.label1.Text = "远程IP地址:";
    // 
    // txtbRemoteIP
    // 
    this.txtbRemoteIP.Location = new System.Drawing.Point(124, 36);
    this.txtbRemoteIP.Name = "txtbRemoteIP";
    this.txtbRemoteIP.Size = new System.Drawing.Size(176, 21);
    this.txtbRemoteIP.TabIndex = 0;
    this.txtbRemoteIP.Text = "";
    // 
    // txtbRemotePort
    // 
    this.txtbRemotePort.Location = new System.Drawing.Point(124, 72);
    this.txtbRemotePort.Name = "txtbRemotePort";
    this.txtbRemotePort.Size = new System.Drawing.Size(52, 21);
    this.txtbRemotePort.TabIndex = 3;
    this.txtbRemotePort.Text = "23";
    // 
    // label2
    // 
    this.label2.AutoSize = true;
    this.label2.Location = new System.Drawing.Point(56, 76);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(60, 17);
    this.label2.TabIndex = 2;
    this.label2.Text = "远程端口:";
    // 
    // txtbToSend
    // 
    this.txtbToSend.Location = new System.Drawing.Point(124, 108);
    this.txtbToSend.Multiline = true;
    this.txtbToSend.Name = "txtbToSend";
    this.txtbToSend.Size = new System.Drawing.Size(572, 68);
    this.txtbToSend.TabIndex = 8;
    this.txtbToSend.Text = "";
    this.txtbToSend.Enter += new System.EventHandler(this.txtbToSend_Enter);
    // 
    // label3
    // 
    this.label3.AutoSize = true;
    this.label3.Location = new System.Drawing.Point(28, 112);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(85, 17);
    this.label3.TabIndex = 4;
    this.label3.Text = "需发送的信息:";
    // 
    // btnSend
    // 
    this.btnSend.Location = new System.Drawing.Point(32, 144);
    this.btnSend.Name = "btnSend";
    this.btnSend.TabIndex = 9;
    this.btnSend.Text = "&S.发送";
    this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
    // 
    // txtbToReceive
    // 
    this.txtbToReceive.ContextMenu = this.contextMenu;
    this.txtbToReceive.Location = new System.Drawing.Point(124, 192);
    this.txtbToReceive.Multiline = true;
    this.txtbToReceive.Name = "txtbToReceive";
    this.txtbToReceive.ReadOnly = true;
    this.txtbToReceive.ScrollBars = System.Windows.Forms.ScrollBars.Both;
    this.txtbToReceive.Size = new System.Drawing.Size(572, 140);
    this.txtbToReceive.TabIndex = 10;
    this.txtbToReceive.Text = "";
    // 
    // label4
    // 
    this.label4.AutoSize = true;
    this.label4.Location = new System.Drawing.Point(36, 196);
    this.label4.Name = "label4";
    this.label4.Size = new System.Drawing.Size(72, 17);
    this.label4.TabIndex = 7;
    this.label4.Text = "收到的信息:";
    // 
    // statusBar
    // 
    this.statusBar.Location = new System.Drawing.Point(0, 394);
    this.statusBar.Name = "statusBar";
    this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
     this.sbpSimpleText});
    this.statusBar.ShowPanels = true;
    this.statusBar.Size = new System.Drawing.Size(738, 22);
    this.statusBar.TabIndex = 9;
    // 
    // sbpSimpleText
    // 
    this.sbpSimpleText.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
    this.sbpSimpleText.Width = 722;
    // 
    // btnExit
    // 
    this.btnExit.Location = new System.Drawing.Point(332, 352);
    this.btnExit.Name = "btnExit";
    this.btnExit.TabIndex = 11;
    this.btnExit.Text = "&X.退出";
    this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
    // 
    // btnToConnect
    // 
    this.btnToConnect.Location = new System.Drawing.Point(292, 72);
    this.btnToConnect.Name = "btnToConnect";
    this.btnToConnect.TabIndex = 5;
    this.btnToConnect.Text = "&C.连接";
    this.btnToConnect.Click += new System.EventHandler(this.btnToConnect_Click);
    // 
    // txtbTimeOut
    // 
    this.txtbTimeOut.Location = new System.Drawing.Point(224, 72);
    this.txtbTimeOut.Name = "txtbTimeOut";
    this.txtbTimeOut.Size = new System.Drawing.Size(52, 21);
    this.txtbTimeOut.TabIndex = 4;
    this.txtbTimeOut.Text = "30";
    // 
    // label5
    // 
    this.label5.AutoSize = true;
    this.label5.Location = new System.Drawing.Point(188, 76);
    this.label5.Name = "label5";
    this.label5.Size = new System.Drawing.Size(35, 17);
    this.label5.TabIndex = 12;
    this.label5.Text = "超时:";
    // 
    // btnDisconnect
    // 
    this.btnDisconnect.Location = new System.Drawing.Point(380, 72);
    this.btnDisconnect.Name = "btnDisconnect";
    this.btnDisconnect.TabIndex = 6;
    this.btnDisconnect.Text = "&D.断开";
    this.btnDisconnect.Click += new System.EventHandler(this.btnDisconnect_Click);
      

  3.   

    就是要知道通过路由器内的内网IP地址自动获得外网IP地址
      

  4.   

    好像是有这个问题,呵呵,就像FTP一样,如果对方服务器是内网中的一台电脑,通过网关NAT向外发布服务,那么你如果用PASSIVE方式,它返回的是其内网的IP地址,所以CUTEFTP是不能连接这种FTP服务器,可是只要不用它返回的IP地址,而是直接用原来的IP地址,就可以连接了,我刚开始不明白,后来发现,CUTEFTP就是没想到这个,所以自已重写一个FTP客户端,就可以了