public void ConnectCallback( IAsyncResult ar )
{
try
{
// Get The connection socket from the callback
Socket sock= (Socket)ar.AsyncState;
if ( sock.Connected )
{
// Define a new Callback to read the data
AsyncCallback recieveData = new AsyncCallback( OnRecievedData );
// Begin reading data asyncronously
sock.BeginReceive( m_byBuff,0,m_byBuff.Length,SocketFlags.None,recieveData ,sock);
}
}
catch( Exception ex )
{
MessageBox.Show( this, ex.Message, "Setup Recieve callbackProc failed!" );
}
}
public void OnRecievedData( IAsyncResult ar )
{
try
{
// Get The connection socket from the callback
Socket sock = (Socket)ar.AsyncState;
// Get The data , if any
int nBytesRec = sock.EndReceive(ar);
if( nBytesRec > 0 )
{
string sRecieved = Encoding.ASCII.GetString( m_byBuff, 0, nBytesRec );
string m_strLine="";
for ( int i=0; i < nBytesRec;i++)
{
Char ch = Convert.ToChar(m_byBuff[i]);
switch( ch )
{
case '\r':
m_strLine += Convert.ToString("\r\n");
break;
case '\n':
break;
default:
m_strLine += Convert.ToString(ch);
break;
}
}
try
{
int strLinelen = m_strLine.Length ;
if ( strLinelen == 0 )
{
m_strLine = Convert.ToString("\r\n");
}
Byte[] mToProcess = new Byte[strLinelen];
for ( int i=0; i < strLinelen ; i++)
{
mToProcess[i] = Convert.ToByte(m_strLine[i]);
}
// Process the incoming data
this.mOutText = ProcessOptions(mToProcess);
if ( mOutText != "" )
{
richTextBox1.AppendText(mOutText);
}
// Respond to any incoming commands
RespondToOptions();
}
catch( Exception ex )
{
//Object x = this ;
MessageBox.Show(ex.Message , "Information!" );
}
}
else
{
// If no data was recieved then the connection is probably dead
Console.WriteLine( "Disconnected", sock.RemoteEndPoint );
sock.Shutdown( SocketShutdown.Both );
sock.Close();
}
}
catch//(Exception e2)
{
//MessageBox.Show(e2.ToString());
this.Close();
}
}
private string ProcessOptions(byte[] m_strLineToProcess)
{
string m_DISPLAYTEXT ="";
string m_strTemp ="" ;
string m_strOption ="";
string m_strNormalText ="";
bool bScanDone =false;
int ndx =0;
int ldx =0;
char ch ;
try
{
for ( int i=0; i < m_strLineToProcess.Length ; i++)
{
Char ss = Convert.ToChar(m_strLineToProcess[i]);
m_strTemp = m_strTemp + Convert.ToString(ss);
}
这是网上的一个程序的代码,可以参考一下。可以telnet到主机如UNIX成功,但telnet到路由器失败。正在找办法解决。
这是一个窗体的代码,窗体中只有一个richtextbox控件。
你只要将IP赋值给address ,port赋值23,然后show这个窗体就可以了。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading ;
namespace TelnetFliter
{
/// <summary>
/// TelnetForm 的摘要说明。
/// </summary>
public class TelnetForm : System.Windows.Forms.Form
{
/// <summary>
Char IAC = Convert.ToChar(255);
Char DO = Convert.ToChar(253);
Char DONT = Convert.ToChar(254);
Char WILL = Convert.ToChar(251);
Char WONT = Convert.ToChar(252);
Char SB = Convert.ToChar(250);
Char SE = Convert.ToChar(240);
const Char IS = '0';
const Char SEND = '1';
const Char INFO = '2';
const Char VAR = '0';
const Char VALUE = '1';
const Char ESC = '2';
const Char USERVAR = '3';
string m_strResp;
public string address ;
public int port;
public string command;
public string mOutText;
private ArrayList m_ListOptions = new ArrayList();
public IPEndPoint iep ;
public Socket s ;
private AsyncCallback callbackProc ;
Byte[] m_byBuff = new Byte[32767];
public System.Windows.Forms.RichTextBox richTextBox1;
/// </summary>
private System.ComponentModel.Container components = null;
public TelnetForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
// Create New EndPoint
iep= new IPEndPoint(IPHost.AddressList[0],port);
// Create New Socket
s= new Socket(iep.Address.AddressFamily, SocketType.Stream,ProtocolType.Tcp);
// This is a non blocking IO
s.Blocking= false ;
// Assign Callback function to read from Asyncronous Socket
callbackProc= new AsyncCallback(ConnectCallback);
// Begin Asyncronous Connection
s.BeginConnect(iep,callbackProc,s ) ;
}
catch(Exception e1)
{
MessageBox.Show( "Application Error!!!-TelnetForm_Load"+e1.ToString());
Application.Exit();
}
}