Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   .NET (https://www.clubdelphi.com/foros/forumdisplay.php?f=17)
-   -   Cliente -Servidor. (https://www.clubdelphi.com/foros/showthread.php?t=64212)

REHome 22-03-2009 09:04:08

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.

cHackAll 23-03-2009 22:35:26

No entiendo qué necesitas?

PD; si es una aplicacion LAN, por qué no usas el protocolo TCP ?

Crandel 24-03-2009 07:51:01

Si estas interesado en la conexión creo que deberias utilizar el protocolo TCP.

Por otro lado veo que estas agregando texto a un componente grafico (lbConnections) desde una Thread. Eso te va a dar error casi seguro.

Para evitar este problema debes utilizar el método Invoke del formulario para sincronizar los hilos.


La franja horaria es GMT +2. Ahora son las 16:05:04.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi