Barrie Eksteen

Barrie Eksteen

  • NA
  • 2
  • 2.4k

TELNET into Server

Apr 26 2013 3:17 AM

please help

I am trying to telnet into a zebra printer with C# and then run through the menu with values I supply and then want to be returned a value.

This is my code thus far: When I telnet with putty, I get prompted for a password, then after i put in the password I get the menu. So in my code I want to give the value "1234" as the password and I then want to be returned the menu. Please help Im stuck :(




public static void StartClient() {
   
// Data buffer for incoming data.
   
byte[] bytes = new byte[1024];
   
// Connect to a remote device.
   
try {
       
// Establish the remote endpoint for the socket.
       
// This example uses port 23 on the Server.
       
IPHostEntry ipHostInfo = Dns.Resolve("10.121.237.151");//Dns.GetHostName());
       
IPAddress ipAddress = ipHostInfo.AddressList[0];
       
IPEndPoint remoteEP = new IPEndPoint(ipAddress,23);

       
// Create a TCP/IP  socket.
       
Socket sender = new Socket(AddressFamily.InterNetwork,
           
SocketType.Stream, ProtocolType.Tcp );

       
// Connect the socket to the remote endpoint. Catch any errors.
       
try {
            sender
.Connect(remoteEP);

           
Console.WriteLine("Socket connected to {0}",
                sender
.RemoteEndPoint.ToString());

           
// Encode the data string into a byte array.
           
byte[] msg = Encoding.ASCII.GetBytes("1234/n/r");


           
// Send the data through the socket.
           
int bytesSent = sender.Send(msg);

           
// Receive the response from the remote device.
           
int bytesRec = sender.Receive(bytes);
           
//Console.WriteLine(msg.ToString());
           
//Console.WriteLine("Echoed test = {0}",
           
//    Encoding.ASCII.GetString(bytes,0,bytesRec));
           
//Console.ReadLine();

           
// Release the socket.
            sender
.Shutdown(SocketShutdown.Both);
            sender
.Close();
           
Console.ReadLine();

       
} catch (ArgumentNullException ane) {
           
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
           
Console.ReadLine();
       
} catch (SocketException se) {
           
Console.WriteLine("SocketException : {0}",se.ToString());
           
Console.ReadLine();
       
} catch (Exception e) {
           
Console.WriteLine("Unexpected exception : {0}", e.ToString());
           
Console.ReadLine();
       
}

   
} catch (Exception e) {
       
Console.WriteLine( e.ToString());
       
Console.ReadLine();
   
}
}