Thursday, August 6, 2009

Chat clint in consol

//Client//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace Client
{
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.WriteLine(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);
}
}
}
}
}

No comments:

Post a Comment