Evo opet ja smaram sa threadovima..
Hoću da se telnetujem na neki host i da šaljem podatke i da obrađujem primljene podatke.
Za test sam koristio sezamov telnet servis.
Telnetujem se, pošaljem mu ime, i ako u njegovom responsu pronađem određeni string, radim nešto.
Napravio sam klasu za telnetovanje u kojoj imam promenjivu XXX tipa int i def. vrednost 1.
Pošaljem komandu john.doe (za logovanje) i ako u responsu dobijem string koji počinje sa john.doe (recimo john.doe nije korisnik...) vrednost promenljive XXX se promeni na 2.
Sledi kod moje klase
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace WindowsApplication8
{
class Telnet
{
public int XXX = 1;
public Telnet()
{
connect();
}
#region Deklaracije
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;
private ArrayList m_ListOptions = new ArrayList();
private IPEndPoint iep;
private AsyncCallback callbackProc;
private string address;
private int port;
private Socket s;
Byte[] m_byBuff = new Byte[32767];
#endregion
private void connect()
{
address = "sezampro.yu";
port = 23;
IPHostEntry IPHost = Dns.Resolve(address);
string[] aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;
try
{
// Create New Socket
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Create New EndPoint
iep = new IPEndPoint(addr[0], port);
// This is a non blocking IO
s.Blocking = true;
// Assign Callback function to read from Asyncronous Socket
callbackProc = new AsyncCallback(ConnectCallback);
// Begin Asyncronous Connection
s.BeginConnect(iep, callbackProc, s);
}
catch (Exception eeeee)
{
MessageBox.Show(eeeee.Message, "Application Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
}
public void ConnectCallback(IAsyncResult ar)
{
try
{
// Get The connection socket from the callback
Socket sock1 = (Socket)ar.AsyncState;
if (sock1.Connected)
{
// Define a new Callback to read the data
AsyncCallback recieveData = new AsyncCallback(OnRecievedData);
// Begin reading data asyncronously
sock1.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData, sock1);
}
}
catch (Exception ex)
{
MessageBox.Show("Setup Recieve callbackProc failed!");
}
}
public void OnRecievedData(IAsyncResult ar)
{
// 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
string mOutText = ProcessOptions(mToProcess);
if (mOutText != "")
{
parsirajTekst(mOutText);
//MessageBox.Show(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();
Application.Exit();
}
}
private void parsirajTekst(string s)
{
char[] newLineSplitter = { '\n' };
char[] spaceSplitter = { ' ' };
char[] xSplitter = { ' ', 'x' };
string[] line = s.Split(newLineSplitter, StringSplitOptions.RemoveEmptyEntries);
foreach (string l in line)
{
if (l.StartsWith("john.doe"))
{
XXX = 2;
}
}
}
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);
}
while (bScanDone != true)
{
int lensmk = m_strTemp.Length;
ndx = m_strTemp.IndexOf(Convert.ToString(IAC));
if (ndx > lensmk)
ndx = m_strTemp.Length;
if (ndx != -1)
{
m_DISPLAYTEXT += m_strTemp.Substring(0, ndx);
ch = m_strTemp[ndx + 1];
if (ch == DO || ch == DONT || ch == WILL || ch == WONT)
{
m_strOption = m_strTemp.Substring(ndx, 3);
string txt = m_strTemp.Substring(ndx + 3);
m_DISPLAYTEXT += m_strTemp.Substring(0, ndx);
m_ListOptions.Add(m_strOption);
m_strTemp = txt;
}
else
if (ch == IAC)
{
m_DISPLAYTEXT = m_strTemp.Substring(0, ndx);
m_strTemp = m_strTemp.Substring(ndx + 1);
}
else
if (ch == SB)
{
m_DISPLAYTEXT = m_strTemp.Substring(0, ndx);
ldx = m_strTemp.IndexOf(Convert.ToString(SE));
m_strOption = m_strTemp.Substring(ndx, ldx);
m_ListOptions.Add(m_strOption);
m_strTemp = m_strTemp.Substring(ldx);
}
}
else
{
m_DISPLAYTEXT = m_DISPLAYTEXT + m_strTemp;
bScanDone = true;
}
}
m_strNormalText = m_DISPLAYTEXT;
}
catch (Exception eP)
{
MessageBox.Show(eP.Message, "Application Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
return m_strNormalText;
}
public void DispatchMessage(string strText)
{
try
{
Byte[] smk = new Byte[strText.Length];
for (int i = 0; i < strText.Length; i++)
{
Byte ss = Convert.ToByte(strText[i]);
smk[i] = ss;
}
IAsyncResult ar2 = s.BeginSend(smk, 0, smk.Length, SocketFlags.None, callbackProc, s);
s.EndSend(ar2);
}
catch (Exception ers)
{
MessageBox.Show("ERROR IN RESPOND OPTIONS");
}
}
void RespondToOptions()
{
try
{
string strOption;
for (int i = 0; i < m_ListOptions.Count; i++)
{
strOption = (string)m_ListOptions[i];
ArrangeReply(strOption);
}
DispatchMessage(m_strResp);
m_strResp = "";
m_ListOptions.Clear();
}
catch (Exception ers)
{
MessageBox.Show("ERROR IN RESPOND OPTIONS");
}
}
void ArrangeReply(string strOption)
{
try
{
Char Verb;
Char Option;
Char Modifier;
Char ch;
bool bDefined = false;
if (strOption.Length < 3) return;
Verb = strOption[1];
Option = strOption[2];
if (Option == 1 || Option == 3)
{
// case 1: // Echo
// case 3: // Suppress Go-Ahead
bDefined = true;
// break;
}
m_strResp += IAC;
if (bDefined == true)
{
if (Verb == DO)
{
// case DO:
ch = WILL;
m_strResp += ch;
m_strResp += Option;
// break;
}
if (Verb == DONT)
{
ch = WONT;
m_strResp += ch;
m_strResp += Option;
// break;
}
if (Verb == WILL)
{
ch = DO;
m_strResp += ch;
m_strResp += Option;
//break;
}
if (Verb == WONT)
{
ch = DONT;
m_strResp += ch;
m_strResp += Option;
// break;
}
if (Verb == SB)
{
Modifier = strOption[3];
if (Modifier == SEND)
{
ch = SB;
m_strResp += ch;
m_strResp += Option;
m_strResp += IS;
m_strResp += IAC;
m_strResp += SE;
}
// break;
}
}
else
{
// switch(Verb)
// {
if (Verb == DO)
{
ch = WONT;
m_strResp += ch;
m_strResp += Option;
// break;
}
if (Verb == DONT)
{
ch = WONT;
m_strResp += ch;
m_strResp += Option;
// break;
}
if (Verb == WILL)
{
ch = DONT;
m_strResp += ch;
m_strResp += Option;
// break;
}
if (Verb == WONT)
{
ch = DONT;
m_strResp += ch;
m_strResp += Option;
// break;
}
}
}
catch (Exception eeeee)
{
MessageBox.Show(eeeee.Message, "Application Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
}
}
}
U nekoj mojoj test formi imam sledeće
private Telnet t;
public Form1()
{
InitializeComponent();
t = new Telnet();
}
private void button1_Click(object sender, EventArgs e)
{
t.DispatchMessage("john.doe");
t.DispatchMessage("\r\n");
MessageBox.Show(t.XXX.ToString());
}
Kada kliknem prvi put na button, pročitam vrednost 1 iako bi trebala da bude 2, a drugi put kad kliknem, normalno pročita dvojku.
Znam da nešto ne valja kod threadova samo nikad se njima nisam ozbiljnije bavio pa da znam šta to.












Telnetovanje i problem sa threadovima