Client
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class Program
{
private static TcpClient client = new TcpClient();
private static NetworkStream ns;
static void Main(string[] args)
{
try
{
client.Connect("localhost", 4545);
Console.WriteLine("Connected with Server...");
ns = client.GetStream();
Thread t = new Thread(new ThreadStart(ReadServerData));
t.Start();
while (client.Connected)
{
string data = Console.ReadLine();
byte[] ByteData = Encoding.ASCII.GetBytes(data);
ns.Write(ByteData, 0, ByteData.Length);
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.ReadLine();
}
}
private static void ReadServerData()
{
while (client.Connected)
{
try
{
byte[] Buffer = new byte[255];
ns.Read(Buffer, 0, 255);
string data = Encoding.ASCII.GetString(Buffer);
Console.WriteLine("Server: " + data);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Output:
Connected with Server...
Server: hai
hello