Reputation: 33
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)
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!
Upvotes: 0
Views: 7021
Reputation: 6773
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);
Upvotes: 2