user474926
user474926

Reputation: 3

using C# socket to connect Ubuntu( port 23 ,telnet) can't recieve the Prompt

I am a newbie with C#, and just trying to use C# to program a client that will telnet an Ubuntu server and log into this server. I used Putty or windows cmd to log into this server, the response looked like this:

------------------Welcome to syseng1--------------------
This machine is the System Engineering Automation Server
--------------------------------------------------------
Ubuntu 8.04.3 LTS
syseng1 login:

but when I used C# async socket to tried to log into this server, I can't receive the the last two lines as showed above.

here's my code:

 private 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)
        {
            // Decode the received data
            //byte[] buf = Encoding.Convert(Encoding.UTF8, Encoding.UTF8, m_byBuff, 0, nBytesRec);
           // String sRecieved = Encoding.UTF8.GetString(m_byBuff, 0, nBytesRec);
           // string sRecieved = Encoding.GetEncoding("utf-8").GetString(m_byBuff, 0, nBytesRec);

            string sRecieved = "";
            for (int i = 0; i < nBytesRec; i++)
            {

                Char ch = Convert.ToChar(m_byBuff[i]);

                switch (ch)
                {
                   case '\r':
                        sRecieved += Convert.ToString("\r\n");
                        break;
                    case '\n':
                        break;
                    default:
                        sRecieved += Convert.ToString(ch);
                        break;
                }
            }

            // Console.WriteLine(sRecieved);
            strWorkingData += sRecieved;
            strFullLog += sRecieved;

            richbox.Invoke(new UpdateTextCallback(this.UpdateText), sRecieved);
            // Launch another callback to listen for data

            //AsyncCallback recieveData = new AsyncCallback(OnRecievedData);
            sock.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, OnRecievedData, sock);

        }
        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();
        }
    }

also I tried with ruby to find what exact I received if I used socket to log into this server.here's my log:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\vzheng>irb
irb(main):001:0> require 'socket'
=> true
irb(main):002:0> include Socket::Constants
=> Object
irb(main):003:0> socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
=> #
irb(main):004:0> sockaddr = Socket.sockaddr_in( 23, '10.4.0.50'
=> "\002\000\000\027\n\004\0002\000\000\000\000\000\000\000\000
irb(main):005:0> socket.connect( sockaddr )
=> 0
irb(main):006:0>
irb(main):007:0* socket.gets
=> "------------------Welcome to syseng1--------------------\r\
irb(main):008:0> socket.gets
=> "This machine is the System Engineering Automation Server\r\
irb(main):009:0> socket.read(40)
=> "----------------------------------------"
irb(main):010:0> socket.read(2)
=> "--"
irb(main):011:0> socket.read(2)
=> "--"
irb(main):012:0> socket.read(2)
=> "--"
irb(main):013:0> socket.read(2)
=> "--"
irb(main):014:0> socket.read(2)
=> "--"
irb(main):015:0> socket.read(2)
=> "--"
irb(main):016:0> socket.read(2)
=> "--"
irb(main):017:0> socket.read(2)
=> "--"
irb(main):018:0> socket.read(2)
=> "\r\n"
irb(main):019:0> socket.read(2)
=> "\377\375"
irb(main):020:0> socket.read(1)
=> "'"
irb(main):021:0> socket.read(1)
(hang there)...

So with socket no matter in c# or ruby, I just can receive the first three lines but I can't receive the follwoing lines.

Ubuntu 8.04.3 LTS
syseng1 login:

Would you guys please give me some idea about this issue?

Upvotes: 0

Views: 824

Answers (1)

KevinDTimm
KevinDTimm

Reputation: 14376

Telnet is a protocol (as evidenced by your "\002\000\000\027\n\004\0002\000\000\000\000\000\000\000\000" output). What this means is that the login prompt won't occur automatically. Typically, sending a carriage return (or two) will get the login prompt, but may (or may not) be part of that protocol.

In other words, you'll need to implement the telnet protocol to make this work.

Upvotes: 1

Related Questions