收到消息(套接字)后移动Console.WriteLine()

时间:2013-08-14 19:05:05

标签: c# console-application

我使用套接字创建了一个控制台聊天应用程序。 在服务器和客户端上,我使用Console.WriteLine("Enter message:")。现在,当我从控制台的客户端或服务器收到消息时,它会显示:

"Enter message:
 Received message"

如何将Console.WriteLine("Enter Message");自动移至所有收到的邮件下方?

我当前的服务器代码:

 static void Main(string[] args)
    {
        var ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

        Console.WriteLine("Enter a username:");
        var sname = Console.ReadLine();
        Console.Clear();
        ClientSocket.Connect(new IPEndPoint(IPAddress.Loopback, 9000));

        Console.WriteLine("Connection has been established...");

        var RecieveThread = new Thread(new ParameterizedThreadStart(RecieveMessage));
        RecieveThread.Start(ClientSocket);

        while (true)
        {
            Console.Clear();
            Console.WriteLine("Type message:");
            var message = Console.ReadLine();
            var custmessage = new CustomMessage() { From = sname, Message = message, TimeStamp = DateTime.Now };

            Serialize serialize = new Serialize();

            ClientSocket.Send(serialize.Serializer(custmessage));
        }
    }

    static void RecieveMessage(object socketobject)
    {
        Serialize deserialize = new Serialize();
        var client = (Socket)socketobject;

        while (true)
        {
            byte[] buffer = new byte[1024];

            var newmessage = new CustomMessage();
            client.Receive(buffer);
            object desbuffer = deserialize.Deserializer(buffer);
            newmessage = (CustomMessage)desbuffer;

            Console.WriteLine(newmessage.ToString());
        }
    }`

1 个答案:

答案 0 :(得分:1)

您可以使用Console.CursorTop移动到屏幕的最后一行,输出“输入消息”,然后返回。

这是sample,显示如何在控制台中的任何位置输出文本。