#1 Cliente Servidor UDP en C#
Hola. Estoy haciendo un ejemplo de un programa Cliente-Servidor UDP en C#. Visualmente es algo asi:
Ver Imagen
Lo estoy tratando de probar de manera local pero no funciona. Si alguien fuera tan amable de mostrarme mi error. Gracias!
Ver Imagen
Lo estoy tratando de probar de manera local pero no funciona. Si alguien fuera tan amable de mostrarme mi error. Gracias!
Código:
namespace PC1_Cliente
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_conectar_Click(object sender, EventArgs e)
{
UdpClient udpClient = new UdpClient();
udpClient.Connect(IPAddress.Parse("127.0.0.1")/*txt_ip.Text*/, 8888);
Byte[] sendBytes = Encoding.ASCII.GetBytes(txt_msg.Text);
udpClient.Send(sendBytes, sendBytes.Length);
}
}
}
/* ------------------------------------------------------------------------- */
namespace PC2_Servidor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
lb_pantalla.Items.Add("Esperando.....");
//serverThread();
}
public void serverThread()
{
UdpClient udpClient = new UdpClient(8888);
while (true)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1")/*Any*/, 0);
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
lb_pantalla.Items.Add(RemoteIpEndPoint.Address.ToString() + ":" + returnData.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thdUDPServer = new Thread(new
ThreadStart(serverThread));
thdUDPServer.Start();
}
}
}
0