1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| using System;
using System.Net.Sockets;
using System.Text;
using System.Net;
namespace TestRLogin
{
class Program
{
public static void write(Socket s, char c)
{
char[] toSend = { c };
if (s.Send(new UTF8Encoding().GetBytes(toSend)) <= 0)
{
Console.Error.WriteLine("Echec de l'envoi");
}
}
public static string read(Socket s)
{
int size, count;
byte[] toread;
string text = String.Empty;
while (s.Available > 0)
{
size = s.Available;
toread = new byte[size];
count = s.Receive(toread, size, SocketFlags.None);
text += new UTF8Encoding().GetString(toread);
Console.WriteLine("-> {0} octets lus sur {1} octets disponibles", count, size);
}
return text;
}
public static byte[] resize(int width, int height)
{
byte[] b = { 0xFF, 0xFF, 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
b[6] = (byte) (width >> 8);
b[7] = (byte) (width & 0xFF);
b[4] = (byte) (height >> 8);
b[5] = (byte) (height & 0xFF);
return b;
}
static void Main(string[] args)
{
try
{
byte[] data = new ASCIIEncoding().GetBytes(" user user xterm/38400 ");
for (int i = 0; i < data.Length; i++)
{
if (data[i] == 32)
data[i] = 0x00;
}
IPEndPoint host = new IPEndPoint(Dns.GetHostEntry("hostname").AddressList[0], 513);
IPEndPoint local = new IPEndPoint(IPAddress.Any, 1021);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(local);
s.DontFragment = true;
s.NoDelay = true;
s.Connect(host);
// Connexion
int count = s.Send(data);
Console.WriteLine("{0} octets envoyés", count);
count = s.Send(resize(80, 24));
s.Send(new byte[] {0x00});
Console.WriteLine("{0} octets envoyés", count);
while (true)
{
if (s.Poll(200, SelectMode.SelectRead))
{
Console.Write(read(s));
}
else if (s.Poll(200, SelectMode.SelectWrite))
{
write(s, (char)Console.Read());
}
else
{
break;
}
}
s.Shutdown(SocketShutdown.Both);
s.Close();
Console.WriteLine("Connexion fermée");
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.Read();
}
}
} |