0

I try to make the LAN chat application, use Send() and Receive() function in C# to send and receive message. But the problem is that, when user type in message into Console and press Enter keyboard, this content suddenly appear in Console Screen before WriteLine my form appear (For example: what I expected is You: hello)

enter image description here

How could I remove line "hello". What I expecting is:
Client: hi
You: hello

Code for Server:

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

        }
    }
}

Code for Client:

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

        }
    }
}

Thanks everyone!

Co May Hoa
  • 33
  • 1
  • 6
  • No. I don't want to hide input while typing. What I really need is that hide the output after I press Enter keyboard. Did you really read my question but said that? – Co May Hoa Sep 04 '17 at 15:14
  • You need to express your question clearly - you said your expected output was : Client: hi You: hello withouth the hello in between - so I gave you an answer that worked. – PaulF Sep 04 '17 at 15:18

1 Answers1

2

You could try this, Set the cursor position back to the start of the previous line after ReadLine & then overwrite the line with your text - in your case as you are prepending "You: " your output string will be longer than the input string - if it were smaller you could overwrite with spaces to clear any excess characters.

  input = Console.ReadLine();
  Console.SetCursorPosition(0, Console.CursorTop-1);
  Console.WriteLine("You: " + input);
PaulF
  • 6,673
  • 2
  • 18
  • 29
  • just in case: do you know a similar method to do this in C-UNIX? – Thecave3 Sep 04 '17 at 15:56
  • 1
    @Thecave3 - maybe this is useful : https://stackoverflow.com/questions/26423537/how-to-position-the-input-text-cursor-in-c – PaulF Sep 04 '17 at 15:58
  • a lot, but this could be a problem in an asynchronous code like a TCP chat because you can't know when the client will send you a message. Anyway, thank you really much. – Thecave3 Sep 04 '17 at 16:02
  • Definitely a problem where you effectively have two threads updating a console - in .Net it could possibly be resolved by using Console.ReadKey(true) - which would stop the automatic echo of the input character - allowing the application to synchronously update the console in response to the asynchronous events. I do not know enough about C-Unix to know if that is possible. – PaulF Sep 04 '17 at 16:12