Y
Yuste
Hola foro necesitaría uno o varios manuales, libros o información de la
programación con sockets y redes en C#, me es muy muy urgente. Pero que
sea claro y bien explicado. Gracias.
Y porqué esto no funciona el código de abajo, un servidor de chat y un
cliente.
Pongo el servidor escuchando y cuando el cliente envía algo este lo
recibe pero se me quedan congeladas las ventanas del cliente y del
servidor ¿?.
Muchísimas gracias.
---------
Servidor:
---------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace ChatYuste
{
public partial class frmServidor : Form
{
Socket sockresp;
public frmServidor()
{
InitializeComponent();
}
private void frmServidor_Load(object sender, EventArgs e)
{
}
private void btnEstado_Click(object sender, EventArgs e)
{
try
{
IPHostEntry iph = Dns.GetHostByName("192.168.0.1");
IPAddress ipa = iph.AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipa,
int.Parse(txtPuerto.Text));
//O en una sola línea así...
//IPEndPoint ipe = new
IPEndPoint(IPAddress.Parse("192.168.0.1"), int.Parse(txtPuerto.Text));
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipe);
socket.Listen(100);
txtRespuesta.Text = "Esperando a un cliente...";
Application.DoEvents();
sockresp = socket.Accept();
txtRespuesta.AppendText("\n");
txtRespuesta.Text += "\r\nConectado...\r\n";
Application.DoEvents();
int bytesRecibidos = 0;
string datosRecibidos = "";
byte[] buffer = new byte[1024];
do
{
bytesRecibidos = sockresp.Receive(buffer,
buffer.Length, 0);
datosRecibidos +=
System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, buffer.Length);
txtRespuesta.Text += "\r\n" + datosRecibidos;
Application.DoEvents();
} while (bytesRecibidos > 0);
string estado = "OK";
byte[] respuesta = new byte[1024];
respuesta = Encoding.ASCII.GetBytes(estado);
sockresp.Send(respuesta);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnEnviar_Click(object sender, EventArgs e)
{
byte[] datosEnviar = Encoding.ASCII.GetBytes(txtEnvio.Text);
sockresp.Send(datosEnviar, datosEnviar.Length,
SocketFlags.None);
sockresp.Shutdown(SocketShutdown.Both);
sockresp.Close();
}
}
}
--------
Cliente:
--------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace ChatYuste
{
public partial class frmCliente : Form
{
public frmCliente()
{
InitializeComponent();
}
private void frmCliente_Load(object sender, EventArgs e)
{
}
private void btnEnviar_Click(object sender, EventArgs e)
{
try
{
IPAddress ipa = IPAddress.Parse(txtIPDestino.Text);
//Dns.Resolve("localhost").AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipa,
int.Parse(txtPuerto.Text));
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipe);
int bytes = 0;
string respuesta = "";
byte[] datosEnvio =
System.Text.ASCIIEncoding.ASCII.GetBytes(txtEnvio.Text);
byte[] datosRecibidos = new byte[1024];
socket.Send(datosEnvio, datosEnvio.Length,
SocketFlags.None);
do
{
bytes = socket.Receive(datosRecibidos,
datosRecibidos.Length, 0);
respuesta =
System.Text.ASCIIEncoding.ASCII.GetString(datosRecibidos);
} while (bytes > 0);
txtRespuesta.Text += "\r\n" + respuesta;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
programación con sockets y redes en C#, me es muy muy urgente. Pero que
sea claro y bien explicado. Gracias.
Y porqué esto no funciona el código de abajo, un servidor de chat y un
cliente.
Pongo el servidor escuchando y cuando el cliente envía algo este lo
recibe pero se me quedan congeladas las ventanas del cliente y del
servidor ¿?.
Muchísimas gracias.
---------
Servidor:
---------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace ChatYuste
{
public partial class frmServidor : Form
{
Socket sockresp;
public frmServidor()
{
InitializeComponent();
}
private void frmServidor_Load(object sender, EventArgs e)
{
}
private void btnEstado_Click(object sender, EventArgs e)
{
try
{
IPHostEntry iph = Dns.GetHostByName("192.168.0.1");
IPAddress ipa = iph.AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipa,
int.Parse(txtPuerto.Text));
//O en una sola línea así...
//IPEndPoint ipe = new
IPEndPoint(IPAddress.Parse("192.168.0.1"), int.Parse(txtPuerto.Text));
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipe);
socket.Listen(100);
txtRespuesta.Text = "Esperando a un cliente...";
Application.DoEvents();
sockresp = socket.Accept();
txtRespuesta.AppendText("\n");
txtRespuesta.Text += "\r\nConectado...\r\n";
Application.DoEvents();
int bytesRecibidos = 0;
string datosRecibidos = "";
byte[] buffer = new byte[1024];
do
{
bytesRecibidos = sockresp.Receive(buffer,
buffer.Length, 0);
datosRecibidos +=
System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, buffer.Length);
txtRespuesta.Text += "\r\n" + datosRecibidos;
Application.DoEvents();
} while (bytesRecibidos > 0);
string estado = "OK";
byte[] respuesta = new byte[1024];
respuesta = Encoding.ASCII.GetBytes(estado);
sockresp.Send(respuesta);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnEnviar_Click(object sender, EventArgs e)
{
byte[] datosEnviar = Encoding.ASCII.GetBytes(txtEnvio.Text);
sockresp.Send(datosEnviar, datosEnviar.Length,
SocketFlags.None);
sockresp.Shutdown(SocketShutdown.Both);
sockresp.Close();
}
}
}
--------
Cliente:
--------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace ChatYuste
{
public partial class frmCliente : Form
{
public frmCliente()
{
InitializeComponent();
}
private void frmCliente_Load(object sender, EventArgs e)
{
}
private void btnEnviar_Click(object sender, EventArgs e)
{
try
{
IPAddress ipa = IPAddress.Parse(txtIPDestino.Text);
//Dns.Resolve("localhost").AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipa,
int.Parse(txtPuerto.Text));
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipe);
int bytes = 0;
string respuesta = "";
byte[] datosEnvio =
System.Text.ASCIIEncoding.ASCII.GetBytes(txtEnvio.Text);
byte[] datosRecibidos = new byte[1024];
socket.Send(datosEnvio, datosEnvio.Length,
SocketFlags.None);
do
{
bytes = socket.Receive(datosRecibidos,
datosRecibidos.Length, 0);
respuesta =
System.Text.ASCIIEncoding.ASCII.GetString(datosRecibidos);
} while (bytes > 0);
txtRespuesta.Text += "\r\n" + respuesta;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}