Ver Mensaje Individual
  #1  
Antiguo 22-03-2009
REHome REHome is offline
Miembro
 
Registrado: jul 2003
Ubicación: España
Posts: 454
Reputación: 21
REHome Va por buen camino
Cliente -Servidor.

Hola:

Tengo hecho algo de Cliente-Servidor muy básico. Me gustaría saber que si el PC1 se conecta al PC2, envía al PC1 un mensaje que advierta si su conexión ha sido un éxito y que se mantenga en línea como el messenger. Cuando PC1 cierra la conexión mediante un buttón o botón, el PC2 muestra un mensaje indicando su conexión.

NOTA: Los botones de Control,por ahora no hablamos de ellos y aún no es funcional.

DESCARGAR


PC1-Cliente:
Código:
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; 
using System.IO; 
using System.Net; 
using System.Net.Sockets; 
 
namespace PC1_Cliente 
{ 
    public partial class Form_principal : Form 
    { 
        public Form_principal() 
        { 
            InitializeComponent(); 
        } 
 
        private void button_Conectar_Click(object sender, EventArgs e) 
        { 
            UdpClient udpClient = new UdpClient(); 
            udpClient.Connect(textBox1.Text, 8888); 
            Byte[] sendBytes = Encoding.ASCII.GetBytes(textBox2.Text); 
            udpClient.Send(sendBytes, sendBytes.Length); 
        } 
    } 
}
PC2-Servidor:
Código:
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; 
using System.IO; 
using System.IO.Ports; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
 
namespace PC2_Servidor 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
            if (!serialPort1.IsOpen) 
            { 
                try 
                { 
                    serialPort1.Open(); 
                } 
                catch (System.Exception ex) 
                { 
                    MessageBox.Show(ex.ToString()); 
                } 
            } 
        } 
        public void serverThread() 
        { 
        UdpClient udpClient = new UdpClient(8888); 
        while(true) 
        { 
        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); 
        Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); 
        string returnData = Encoding.ASCII.GetString(receiveBytes); 
        lbConnections.Items.Add(RemoteIpEndPoint.Address.ToString() + ":" + returnData.ToString() ); 
        } 
        } 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            Thread thdUDPServer = new Thread(new 
            ThreadStart(serverThread)); 
            thdUDPServer.Start(); 
        } 
    } 
}
Un cordial saludo.
__________________
http://electronica-pic.blogspot.com....n-arduino.html Manuales de electrónica general, PIC y Arduino.
Responder Con Cita