Hola que tal ! Tengo una super duda, tengo que hacer un chat que contenga un servidor y varios clientes. En mí código sólo soporta 1 servidor y 1 clientes. Estoy utilizando el control WinSock, ¿pero cómo logro hacer varias conexiones? ... he leeido que con un arreglo de controles y que en ejecución se vayan creando esos controles, pero no se cómo!!! ayuda!!
Código:
Este es el codigo que tengo del Cliente:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Cliente : Form
{
public Cliente()
{
InitializeComponent();
}
private void Conectar_Click(object sender, EventArgs e)
{
object host = txthost .Text ;
object puerto = txtpuerto .Text ;
winsock.Connect(ref host, ref puerto);
MessageBox.Show("Conectado con " + host + " en" + puerto);
}
private void Enviar_Click(object sender, EventArgs e)
{
rtbconversacion.Text = rtbconversacion.Text + "\n El cliente dice " + txtmensajes.Text;
object msj=txtmensajes .Text ;
winsock.SendData(ref msj);
}
private void winsock_OnDataArrival(object sender, AxOSWinsckControl.__Winsock_OnDataArrivalEvent e)
{
rtbconversacion.Text = rtbconversacion.Text + "\n El servidor dice "
+ winsock.GetDataBuffer().ToString() ;
}
}
}
Y este el del servidor:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Servidor : Form
{
public Servidor()
{
InitializeComponent();
}
private void Conectar_Click(object sender, EventArgs e)
{
txthost.Text = winsock.LocalIP;
int puerto = int.Parse(txtpuerto.Text);
winsock.set_LocalPort(ref puerto);
winsock.Listen();
MessageBox.Show("se ha conectado correctamente");
}
private void Enviar_Click(object sender, EventArgs e)
{
rtbconversacion.Text = rtbconversacion.Text + "\nEl servidor dice " + txtmensajes.Text;
object datos = txtmensajes.Text;
winsock.SendData(ref datos);
}
private void axWinsock1_OnConnectionRequest(object sender, AxOSWinsckControl.__Winsock_OnConnectionRequestEve nt e)
{
winsock.Accept(ref e.requestID);
}
private void Servidor_Load(object sender, EventArgs e)
{
new Cliente().Show();
}
}