简单的TCP聊天应用程序c#

时间:2017-09-04 15:07:16

标签: c# sockets chat tcpclient

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

enter image description here

我怎样才能删除行&#34;你好&#34;。我期待的是:
客户:您好
你好:你好

服务器代码:

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

        }
    }
}

谢谢大家!

1 个答案:

答案 0 :(得分:1)

你可以试试这个,在ReadLine&amp; amp; amp;然后用您的文本覆盖该行 - 在您的情况下,因为您在前面添加“您:”您的输出字符串将比输入字符串长 - 如果它更小,您可以用空格覆盖以清除任何多余的字符。

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