jueves, 25 de febrero de 2016

TCP_Eco

/*CLiente
//Agregar
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace ClienteTCPTipoEco
{
    class Program
    {
        static void Main(string[] args)
        {
             Console.WriteLine("CLIENTE TCP TIPO ECO");

            string datos = "##--##--##----***----##--##--##";

            IPEndPoint remoto = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
           
            TcpClient cliente = new TcpClient();

            byte[] bufferRx = new byte[512];
            int indicador;
           
            cliente.Connect(remoto);

            if(cliente.Connected)
            {
                NetworkStream flujo = cliente.GetStream();
                byte[] bufferTx = Encoding.ASCII.GetBytes(datos);
                flujo.Write(bufferTx, 0, bufferTx.Length);
               
                do
                {
                    indicador = flujo.Read(bufferRx, 0, bufferRx.Length);

                    if(indicador>0)
                    {
                        datos = Encoding.ASCII.GetString(bufferRx);
                       
                        Console.WriteLine("Mensaje Recibido");
                        Console.WriteLine("Se recibio: \n{0}", datos);
                    }
                }while(indicador>0);
                cliente.Close();
            }
            Console.ReadLine();
        }
    }
}

*/
=======================================================================
/*Servidor
//Agregar
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace ServidorTCPTipoEco
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("SERVIDOR TCP TIPO ECO");
           
            TcpClient manejoCliente;

            TcpListener servidor;

            byte[] bufferRx = new byte[512];
            byte[] bufferTx;
            int datosLeidos;
            string datos;

            IPEndPoint puntoLocal = new IPEndPoint(IPAddress.Any, 11000);
            servidor = new TcpListener(puntoLocal);

            Console.WriteLine("El servidor está escuchando...");
            servidor.Start(5);

            while(true)
            {
                manejoCliente = servidor.AcceptTcpClient();

                Console.WriteLine("El servidor ha aceptado a un cliente...");

                NetworkStream flujo = manejoCliente.GetStream();

                do
                {
                    datosLeidos = flujo.Read(bufferRx, 0, bufferRx.Length);

                    if(datosLeidos>0)
                    {
                        datos = Encoding.ASCII.GetString(bufferRx);

                        Console.WriteLine("Mensaje Recibido");
                        Console.WriteLine("Se recibio: \n{0}", datos);

                        Console.WriteLine("Mensaje Enviado");
                       
                        bufferTx = Encoding.ASCII.GetBytes(datos);
                        flujo.Write(bufferTx, 0, bufferTx.Length);
                    }
                }while(datosLeidos>0);
            }
        }
    }
}
*/

No hay comentarios:

Publicar un comentario