So I am making an application and I have to use Console.Read because I want at a certain event to rewrite the line, so I can't use Console.ReadLine(). Problem is that my application doesn't output string, instead it outputs multiple lines of numbers. I tried with this line of code. If you need more code sample, comment.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace einuorg_console
{
class UDPserver
{
private static string DatePattern = "HH:mm:ss";
public static void Initialize(string IPaddress, int port)
{
Boolean done = false;
Boolean exception_thrown = false;
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress sendto_address = IPAddress.Parse(IPaddress);
IPEndPoint sending_endpoint = new IPEndPoint(sendto_address, port);
Console.Write("[" + DateTime.Now.ToString(DatePattern) + "] ");
Console.WriteLine("einuorg.UDPserver Initialized on address " + IPaddress + ":" + port.ToString());
while (!done)
{
Console.Write("[********]");
string text_to_send = Console.Read().ToString();
if (text_to_send.Length == 0)
{
done = true;
}
else
{
byte[] sendbuffer = Encoding.ASCII.GetBytes(text_to_send);
try
{
sending_socket.SendTo(sendbuffer, sending_endpoint);
}
catch (Exception send_exception)
{
exception_thrown = true;
Console.WriteLine(" Exception {0}", send_exception.Message);
}
if (exception_thrown == false)
{
Console.WriteLine("\r");
Console.Write("[" + DateTime.Now.ToString(DatePattern) + "] ");
Console.WriteLine(text_to_send);
}
else
{
exception_thrown = false;
Console.WriteLine("The exception indicates the message was not sent.");
}
}
}
}
}
}
Terminal is similar to Console class, but haves more options, something I built.