2017-09-04 82 views
0

我嘗試使局域網聊天應用程序,使用C#中的Send()和Receive()函數來發送和接收消息。但問題是,當消息用戶類型到控制檯,然後按Enter鍵鍵盤,這種內容突然出現在控制檯屏幕的WriteLine我的形式出現之前(例如:我的預期是你:你好簡單的TCP聊天應用程序c#

enter image description here

我怎麼能刪除線「你好」。我希望是:
客戶:您好
你:你好

代碼服務器:

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

     } 
    } 
} 

謝謝大家!

+0

不,我不想在輸入時隱藏輸入。我真正需要的是在按下Enter鍵後隱藏輸出。你真的讀過我的問題,但說過嗎? –

+0

你需要清楚地表達你的問題 - 你說你的預期輸出是:客戶:嗨 你:你好沒有你們之間的問候 - 所以我給了你一個有效的答案。 – PaulF

回答

1

您可以試試這個,將光標位置設置回ReadLine &之後的上一行的開頭,然後用您的文本覆蓋該行 - 在您的情況下,因爲您正在預先設置「您:」您的輸出字符串將長於輸入字符串 - 如果它較小,可以用空格覆蓋以清除多餘的字符。

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

以防萬一:您是否知道在C-UNIX中執行此操作的類似方法? – Thecave3

+1

@ Thecave3 - 也許這很有用:https://stackoverflow.com/questions/26423537/how-to-position-the-input-text-cursor-in-c – PaulF

+0

很多,但這可能是一個問題異步代碼就像TCP聊天,因爲你無法知道客戶端何時會向你發送消息。無論如何,非常感謝你。 – Thecave3