TCPListener C # capture the values received by it.

1

I need to make an application that saves the log of a Telnet made to ip 10.123.12.123, to give an example, with a port 1234 for example too.

The problem is that looking at examples I can not understand how the socket class works, I do not know if I'm creating it wrong or not.

The application consists simply of a button, when you press it, it starts the connection and starts saving the log in a text / DB file, I still do not know how I will do it.

I have seen codes and examples but I can not understand what is going on in the program, I do not know if someone could make a simple example of a listener that stores information about a telnet connection.

Thanks in advance.

This is what TELNET returns to me and I need to add those values with dates and so on in a BBDD

The thing is, I write in CMD: Telnet 10.123.12.123 1234 once it enters the server I login with login user password and once inside I launch the command queueinfo to return the data of the queues that are in the moment , what I need exactly is that in my program the values that are returned to me are saved to save them in a database with a datetime.now or some equivalent of it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace ColasTelnet
{
    class TelnetServer
    {

        public static void conectar()
        {
            TcpListener server = null;


            IPAddress ip = IPAddress.Parse("10.123.12.123");

            server = new TcpListener(ip, 1234);
            Thread.Sleep(10000);
            server.Start();

            byte[] bytes = new Byte[256];
            String data = null;


            while (true)
            {

                TcpClient client = server.AcceptTcpClient();

                data = null;

                NetworkStream stream = client.GetStream();


                int i;

                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    MessageBox.Show("Received: {0}", data);

                    // Process the data sent by the client.
                    data = data.ToUpper();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);
                    MessageBox.Show("Sent: {0}", data);
                }
            }

        }





    }
}
    
asked by Aritzbn 18.10.2017 в 08:42
source

1 answer

1

First, I would recommend that you use some of the libraries available for telnet in .Net. But I give you a basic idea of how you can connect and get the data.

What you need is to use the class TcpClient , not TcpListener . The process is basically to connect to the server, wait for Telnet to ask you for the necessary data to log in and once you are, send the command. More or less, like this:

byte[] bytes = new Byte[256];
String data = null;
IPAddress ip = IPAddress.Parse("10.123.12.123");
TcpClient cliente = new TcpClient();
cliente.Connect(ip, 1234);

while (true)
{
    NetworkStream stream = cliente.GetStream();
    int i;

    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
    {
        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
        Console.WriteLine("Recibido: {0}", data);

        data = data.ToUpper();

        byte[] msg;
        if (data.Contains("USERNAME:")) //Tienes que asegurarte que el texto que manda el servidor es igual que esto
        {
            //enviamos el nombre de usuario
            msg = System.Text.Encoding.ASCII.GetBytes("tuUsuario" + Environment.NewLine);
            stream.Write(msg, 0, msg.Length);
        }

        if (data.Contains("PASSWORD:"))
        {
            //enviamos la contraseña
            msg = System.Text.Encoding.ASCII.GetBytes("tuContraseña" + Environment.NewLine);
            stream.Write(msg, 0, msg.Length);
        }

        if (data.Contains("$")) //aqui debes poner el simbolo de sistema que 
        {
            msg = System.Text.Encoding.ASCII.GetBytes("queueinfo" + Environment.NewLine);
            stream.Write(msg, 0, msg.Length);
        }

    }
}

This is an example for a normal telnet. If it is a process for yours, it is different, you simply have to adapt the server to send it to you and you must answer your below. Simply in the console you will see if it gives you an error or the process is working.

    
answered by 18.10.2017 / 10:27
source