Ver Mensaje Individual
  #11 (permalink)  
Antiguo 03/02/2005, 10:26
Avatar de xknown
xknown
 
Fecha de Ingreso: diciembre-2004
Ubicación: Cusco - Perú
Mensajes: 2.248
Antigüedad: 20 años, 2 meses
Puntos: 7
Para lograrlo con TcpListener debes usar hilos al momento de esperar conexiones, aquí un pequeño ejemplo:
Servidor
Código:
namespace Buayacorp {
	using System;
	using System.Net.Sockets;
	using System.Net ;
	using System.Text;
	using System.Threading ;

	public class Servidor
	{
		private TcpListener servidorLst ;
		private int port = 4554 ;
		
		public Servidor()
		{
			try{
					servidorLst = new TcpListener(IPAddress.Any, port) ;
					servidorLst.Start();
					Console.WriteLine("Servidor - Esperando Conexiones ...") ;
					Thread th = new Thread(new ThreadStart(EscucharConexion));
					th.Start() ;
					
				}
			catch(Exception e)
			{
				Console.WriteLine("Ocurrió un excepción :"+e.ToString());
			}
			
		}
		public static void Main(String[] argv)
		{
			Servidor dts = new Servidor();
		}
		public void EscucharConexion()
		{
			while(true)
			{
				Socket client = servidorLst.AcceptSocket() ;
				if(client.Connected)
				{
					Console.WriteLine("Cliente Conectado de "+client.RemoteEndPoint.ToString());
					string mensaje = "\n\n\tBienvenido al Servidor: La hora es: "+ DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
					byte[] msg = Encoding.UTF8.GetBytes(mensaje);
					
					client.Send(msg,msg.Length,0);

					Byte[] datos = new Byte[64];
					
                    int i=client.Receive(datos,datos.Length,0);
					char[] novalido = {' ',' ',' '};

					string recibido = System.Text.Encoding.UTF8.GetString(datos);
					recibido = recibido.TrimEnd(novalido);
					
					Console.WriteLine("Cliente: "+recibido) ;
					
					mensaje="\n\nServidor recibio: "+recibido;
					msg = Encoding.ASCII.GetBytes(mensaje);
					client.Send(msg,msg.Length,0);
				}
			}
		}
	}
}
Cliente
Código:
namespace Buayacorp {

	using System ;
	using System.Net.Sockets ;
	using System.Net ;
	using System.Threading ;

	public class Cliente
	{
		private TcpClient tcpc;
		private string msg ;
		private int port=4554 ;
		private bool readData=false ;

		public Cliente(string msg)
		{
			this.msg=msg ;
			try
			{
                tcpc =new TcpClient("localhost",port) ;
				NetworkStream nts = tcpc.GetStream() ;
				if(nts.CanWrite)
				{
					string sender = msg ;
					Byte[] sends = System.Text.Encoding.UTF8.GetBytes(sender.ToCharArray());
					nts.Write(sends,0,sends.Length) ;
					nts.Flush() ;
				}
				while(!readData&&nts.CanRead)
				{
					if(nts.DataAvailable)
					{
						byte[] rcd = new byte[128];
						int i=nts.Read( rcd,0,128);
						
						string ree = System.Text.Encoding.UTF8.GetString(rcd);
						char[] unwanted = {' ',' ',' '};
						
						Console.WriteLine(ree.TrimEnd(unwanted)) ;
						readData=true ;
					}
				}
			}
			catch(Exception e)
			{
				Console.WriteLine("No se pudo conectar al servidor "+e.ToString());
			}
		}
		public static void Main(string[] argv)
		{
			if(argv.Length<=0)
			{
				Console.WriteLine("Usage: Cliente <msg>") ;
				Console.Write("Mensaje: ") ;
				string msg=Console.ReadLine();
				Cliente dc = new Cliente(msg) ;
			}
			else
			{
				Cliente dc = new Cliente(argv[0]) ;
			}
			Console.WriteLine("Desconectado!!") ;
			Console.ReadLine() ;
		}
	}
}
Saludos
__________________
Alex Concha
Buayacorp - Programación y Diseño