【问题标题】:Simple TCP chat application c#简单的 TCP 聊天应用程序 c#
【发布时间】:2017-09-04 15:07:16
【问题描述】:

我尝试制作局域网聊天应用程序,使用 C# 中的 Send() 和 Receive() 函数来发送和接收消息。但问题是,当用户在控制台中输入消息并按 Enter 键盘时,此内容突然出现在控制台屏幕中,然后 WriteLine 我的表单出现(例如:我期望的是 You: hello

我怎样才能删除行“你好”。我期待的是:
客户:嗨
你:你好

服务器代码:

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

namespace SimpleTcpSrvr
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            int recv;

            byte[] data = new byte[1024];

            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"),9050);

            Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            newsock.Bind(ipep);

            newsock.Listen(10);

            Console.WriteLine("Waiting for a client...");

            Socket client = newsock.Accept();

            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;

            Console.WriteLine("Connected with {0} at port {1}",clientep.Address,clientep.Port);

            string welcome = "Welcome to my test server";

            data = Encoding.UTF8.GetBytes(welcome);

            client.Send(data,data.Length,SocketFlags.None);

            string input;

            while (true)
            {

                data = new byte[1024];

                recv = client.Receive(data);

                if (recv == 0)

                    break;

                Console.WriteLine("Client: "+Encoding.UTF8.GetString(data, 0, recv));

                input = Console.ReadLine();

                Console.WriteLine("You: " + input);

                client.Send(Encoding.UTF8.GetBytes(input));
            }

            Console.WriteLine("Disconnected from {0}", clientep.Address);

            client.Close();

            newsock.Close();

            Console.ReadLine();

        }
    }
}

客户代码:

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

namespace SimpleTcpClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            byte[] data = new byte[1024];

            string input, stringData;

            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"),9050);

            Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            try
            {
                server.Connect(ipep);

            }
            catch(SocketException e)
            {
                Console.WriteLine("Unable to connect to server.");

                Console.WriteLine(e.ToString());

                return;
            }

            int recv = server.Receive(data);

            stringData = Encoding.UTF8.GetString(data, 0, recv);

            Console.WriteLine(stringData);

            while (true)
            {
                input = Console.ReadLine();

                if (input == "exit")

                    break;

                Console.WriteLine("You: " + input);

                server.Send(Encoding.UTF8.GetBytes(input));

                data = new byte[1024];

                recv = server.Receive(data);

                stringData = Encoding.UTF8.GetString(data, 0, recv);

                byte[] utf8string = System.Text.Encoding.UTF8.GetBytes(stringData);

                Console.WriteLine("Server: "+stringData);
            }

            Console.WriteLine("Disconnecting from server...");

            server.Shutdown(SocketShutdown.Both);

            server.Close();

            Console.WriteLine("Disconnected!");

            Console.ReadLine();

        }
    }
}

谢谢大家!

【问题讨论】:

  • 没有。我不想在打字时隐藏输入。我真正需要的是在我按下 Enter 键盘后隐藏输出。你真的读了我的问题但说了那句话吗?
  • 你需要清楚地表达你的问题 - 你说你的预期输出是:客户:你好你:你好,中间没有你好 - 所以我给了你一个有效的答案。

标签: c# sockets chat tcpclient


【解决方案1】:

您可以试试这个,在 ReadLine 之后将光标位置设置回上一行的开头,然后用您的文本覆盖该行 - 在您的情况下,当您在前面加上“您:”时,您的输出字符串将比输入字符串 - 如果它更小,您可以用空格覆盖以清除任何多余的字符。

  input = Console.ReadLine();
  Console.SetCursorPosition(0, Console.CursorTop-1);
  Console.WriteLine("You: " + input);

【讨论】:

  • 以防万一:您知道在 C-UNIX 中执行此操作的类似方法吗?
  • @Thecave3 - 也许这很有用:stackoverflow.com/questions/26423537/…
  • 很多,但这在 TCP 聊天等异步代码中可能是个问题,因为您不知道客户端何时会向您发送消息。无论如何,非常感谢。
  • 绝对是一个问题,您实际上有两个线程更新控制台 - 在.Net中,它可能通过使用 Console.ReadKey(true) 来解决 - 这将停止输入字符的自动回显 - 允许应用程序同步更新控制台以响应异步事件。我对 C-Unix 了解得不够多,不知道这是否可能。
猜你喜欢
  • 2016-05-06
  • 1970-01-01
  • 2016-10-02
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 2012-11-27
  • 2018-03-28
  • 2010-11-06
相关资源
最近更新 更多