//Agreagr
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace Frm_ClienteUDP
{
public partial class Frm_ClienteUDP : Form
{
public Frm_ClienteUDP()
{
InitializeComponent();
}
private delegate void ActualizarInformacion(string texto);
byte[] bufferTx;
byte[] bufferRx;
string datosRx = "";
bool terminado = false;
private void btn_Enviar_Click(object sender, EventArgs e)
{
int puerto = Convert.ToInt32(txt_PuertoEquipoRemoto.Text);
string direccionIp = txt_EquipoRemoto.Text;
UdpClient cliente = new UdpClient();
cliente.Connect(direccionIp, puerto);
bufferTx = Encoding.ASCII.GetBytes(txt_Mensaje.Text);
cliente.Send(bufferTx, bufferTx.Length);
//creacion de hilos.
Thread hiloComunicacion = new Thread(()=> Comunicaciones(cliente));
hiloComunicacion.Start();
txt_Mensaje.Clear();
}
public void Comunicaciones(object ack)
{
UdpClient cliente = (UdpClient)ack;
while (!terminado)
{
IPEndPoint equipoRemoto = new IPEndPoint(IPAddress.Any, 0);
bufferRx = cliente.Receive(ref equipoRemoto);
datosRx = Encoding.ASCII.GetString(bufferRx);
lsb_AckServidor.Invoke(new ActualizarInformacion(ActualizarItemsEnsbConexion),
new object [] {datosRx});
}
}
public void ActualizarItemsEnsbConexion(string texto)
{
lsb_AckServidor.Items.Add(texto);
}
}
}
========================================================================
/*Servidor
//Agregar
using System.Configuration;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace ServidorUDP
{
public partial class Form_ServidorUDP : Form
{
public Form_ServidorUDP()
{
InitializeComponent();
}
private delegate void ActualizarElemento(string texto);
private delegate void ActualizarInformacion();
bool terminado;
byte[] bufferRx = new byte[1024];
byte[] bufferTx;
string datosRx = "";
private void Form_ServidorUDP_Load(object sender, EventArgs e)
{
Thread hiloComunicaciones = new Thread(new ThreadStart(Comunicaciones));
hiloComunicaciones.Start();
}
public void Comunicaciones()
{
string direccionIp= ConfigurationManager.AppSettings["IP"];
string puerto = ConfigurationManager.AppSettings["puerto"];
IPEndPoint equipoConexion = new IPEndPoint(IPAddress.Parse(direccionIp), Convert.ToInt32(puerto));
UdpClient servidor = new UdpClient(equipoConexion);
while (!terminado)
{
IPEndPoint equipoRemoto = new IPEndPoint(IPAddress.Any, 0);
bufferRx = servidor.Receive(ref equipoRemoto);
datosRx = Encoding.ASCII.GetString(bufferRx);
string texto = equipoRemoto.Address.ToString() + "; Mensaje: " + datosRx;
lsbConexiones.Invoke(new ActualizarInformacion(InformarDelCliente));
lsbConexiones.Invoke(new ActualizarElemento(ActualizarItemsEnlsbConexion),
new object[] {texto});
bufferTx = Encoding.ASCII.GetBytes("Datos recibidos por el servidor de la IP: "+equipoRemoto.Address.ToString());
servidor.Send(bufferTx, bufferTx.Length, equipoRemoto);
}
}
public void ActualizarItemsEnlsbConexion(string texto)
{
lsbConexiones.Items.Add(texto);
}
public void InformarDelCliente()
{
lsbConexiones.Items.Add("Transmision recibida a las: " + DateTime.Now.ToString());
}
}
}
No hay comentarios:
Publicar un comentario