Απλή class σε C# για telnet I/O

Παρακάτω υπάρχει μία απλή κλάση σε C# για telnet I/O, με buffer 8KB. Αποτελείται απο τις functions:

  • Connect
  • Send
  • Receive

using System;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace telnet_f
{
class telnet_io
{
public static TcpClient connect(string ip, int port)
{
try
{
TcpClient link = new TcpClient(ip, port);
return (link);
}
catch (Exception e)
{
MessageBox.Show(e.Message + ” — connect error.”);
return (null);
}

}

public static string send(string msgsend, TcpClient link)
{
try
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes(msgsend);
NetworkStream stream = link.GetStream();
stream.Write(data,0,data.Length);
return (“OUT”);
}
catch (Exception e)
{
MessageBox.Show(e.Message + ” — send error.”);
return (null);
}
}

public static List<string> receive(string msgrciv, TcpClient link)
{
try
{
List<string> responce_list = new List<string>();
Byte[] data=System.Text.Encoding.ASCII.GetBytes(msgrciv);
NetworkStream stream = link.GetStream();
data = new Byte[8192];
string responce = string.Empty;
stream.ReadTimeout = 30000;
Int32 bytes = stream.Read(data, 0, data.Length);

do{
responce = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
responce_list.Add(responce);
} while(bytes == 8192);

return (responce_list);
}
catch (Exception e)
{
MessageBox.Show(e.Message + ” — receive error.”);
return (null);
}
}

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: