• Viernes 3 de Mayo de 2024, 02:09

Autor Tema:  Cliente -Servidor.  (Leído 899 veces)

Meta

  • Miembro MUY activo
  • ***
  • Mensajes: 140
    • Ver Perfil
Cliente -Servidor.
« en: Domingo 22 de Marzo de 2009, 09:13 »
0
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: Text
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Net;
  12. using System.Net.Sockets;
  13.  
  14. namespace PC1_Cliente
  15. {
  16.     public partial class Form_principal : Form
  17.     {
  18.         public Form_principal()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void button_Conectar_Click(object sender, EventArgs e)
  24.         {
  25.             UdpClient udpClient = new UdpClient();
  26.             udpClient.Connect(textBox1.Text, 8888);
  27.             Byte[] sendBytes = Encoding.ASCII.GetBytes(textBox2.Text);
  28.             udpClient.Send(sendBytes, sendBytes.Length);
  29.         }
  30.     }
  31. }
  32.  
  33.  

PC2-Servidor:
Código: Text
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.IO.Ports;
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.Threading;
  15.  
  16. namespace PC2_Servidor
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.             if (!serialPort1.IsOpen)
  24.             {
  25.                 try
  26.                 {
  27.                     serialPort1.Open();
  28.                 }
  29.                 catch (System.Exception ex)
  30.                 {
  31.                     MessageBox.Show(ex.ToString());
  32.                 }
  33.             }
  34.         }
  35.         public void serverThread()
  36.         {
  37.         UdpClient udpClient = new UdpClient(8888);
  38.         while(true)
  39.         {
  40.         IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
  41.         Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
  42.         string returnData = Encoding.ASCII.GetString(receiveBytes);
  43.         lbConnections.Items.Add(RemoteIpEndPoint.Address.ToString() + ":" + returnData.ToString() );
  44.         }
  45.         }
  46.         private void Form1_Load(object sender, EventArgs e)
  47.         {
  48.             Thread thdUDPServer = new Thread(new
  49.             ThreadStart(serverThread));
  50.             thdUDPServer.Start();
  51.         }
  52.     }
  53. }
  54.  
  55.  

Un cordial saludo.