الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

client et serveur

بدأه mina1235 في 23 مايو 2012 · 2 رد · 506 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

عملت برنامج يقوم بإرسال حسابات بين سرفور و خادم لكن حسابات فىListClient تكونServerSocket1ClientRead صحيحة وعندما استعملها فىButton3Click حسابات خارج تكون 0

//serveur//------------------------------------------------------

//---------------------------------------------------------------------------


#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define D_max 100 // Dimension maximale de l'espace de recherche
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------
struct position {int taille;double x[D_max]; double f;}; // Position d'une particule et sa performance
struct vecteur {int taille;double v[D_max];}; // Vitesse d'une particule
// Sous-programmes//
double alea(double a,double b); // Nombre pseudo-aléatoire entre a et b
//double ma_fonction (struct position x); // Fonctions à minimiser
// Variables globales//
int nb_eval; // Nombre total d'évaluation
struct Client{String Adresse;String Port;String x;String f;};
//Client ListClient[20];
//---------------------------------------------------------------------------
int Nclients;
int liste[20];
 struct position pp[1];
   struct Client ListClient[20];
   struct position P[D_max]; // Positions
   struct position P_m[D_max]; // Meilleures positions trouvées
   struct position meilleure; // Toute meilleure position trouvée


//----------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

ServerSocket1->Port=StrToInt(Edit1->Text);
ServerSocket1->Open();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
ServerSocket1->Close();

}

//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
int i,n;
 n=0;
 for(i=0;i<Nclients;i++)
     if((ListClient.Adresse==Socket->RemoteAddress )&&
     (ListClient.Port==Socket->RemotePort))
           n=i;
           //pp[0]=P[n];
         Socket->ReceiveBuf(pp,sizeof(pp));
         ListClient[n].f=pp[0].f;
         P[n]=pp[0];
        // Form1->Caption=P[n].f;
       /* Label12->Caption=P[1].f ;
       Label13->Caption=P[2].f ;
       Label15->Caption=P[0].f ; */

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1Accept(TObject *Sender,
      TCustomWinSocket *Socket)
{
Memo1->Lines->Add("----------------------------------------");
Memo1->Lines->Add("Connécté à "+Socket->RemoteAddress);
Memo1->Lines->Add(Socket->RemotePort);
ListBox1->Items->Add(Socket->RemotePort);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
Memo1->Lines->Add("Connexion acceptée par le socket serveur");
//Memo1->Lines->Add("Déconnécté");
StatusBar1->Panels->Items[2]->Text="  Nombre des Clients connectés  "+IntToStr(Nclients+1);
ListClient[Nclients].Adresse=Socket->RemoteAddress;
ListClient[Nclients].Port=Socket->RemotePort;
Nclients=Nclients+1;
}
//-----------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
 Memo1->Lines->Add("Déconnecté");
 int i,n;
 n=0;
 for(i=0;i<Nclients;i++)
     if((ListClient.Adresse==Socket->RemoteAddress )&&
     (ListClient.Port==Socket->RemotePort))
           n=i;

 Nclients=Nclients-1;

 for(i=n;i<Nclients;i++)
   ListClient=ListClient[i+1];
 ListBox1->Items->Delete(n);
 StatusBar1->Panels->Items[2]->Text="  Nombre des Clients connectés  "+IntToStr(Nclients);


}


//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1Listen(TObject *Sender,
      TCustomWinSocket *Socket)
{
Memo1->Lines->Add("A l''écoute...");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientError(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
if (ErrorEvent==eeGeneral)
Memo1->Lines->Add("Erreur inattendu");

if (ErrorEvent==eeSend)
Memo1->Lines->Add("Erreur d''écriture sur la connexion socket");

if (ErrorEvent==eeReceive)
Memo1->Lines->Add("Erreur de lecture sur la connexion socket");

if (ErrorEvent==eeConnect)
Memo1->Lines->Add("Une demande de connexion déjà acceptée n''a pas pu être achevée");

if (ErrorEvent==eeDisconnect)
Memo1->Lines->Add("Erreur de fermeture d''une connexion");

if (ErrorEvent==eeAccept)
Memo1->Lines->Add("Erreur d''acceptation d''une demande de connexion cliente");
}
//----------------------PSO-----------------------------------------------------


void __fastcall TForm1::Button3Click(TObject *Sender)
{
//---------------------------------------------------------------------------



//================PROGRAMME PRINCIPAL=====================//


   double c1,cmax; // Coefficients de confiance
   int D; // Dimension de l'espace de recherche
   int d; // Numéro de la dimension courante
   double eps; // Précision souhaitée
   int eval_max; // Nombre max d'évaluations de la fonction

  // double fmin; // Objectif à atteindre

   int g; // Numéro de la meilleure informatrice


        //  struct position pp[1];
   double min_f;
   int N; // Taille de l'essaim
   int n; // Numéro de la particule courante

   int rang;
   int signe;
   struct vecteur V[D_max]; // Vitesses
   double xmin,xmax; // Intervalle pour l'espace de recherche
   int dis[20];
   int dis_pso[20];
   int j,i;//
   int m,p,bat,vat,s,ss,l,lompe;


//***********************************Paramètres de réglage/******************//


   N=StrToIntDef(Edit3->Text,20); // Taille de l’essaim (comme un test d’arrêt)
   c1=StrToIntDef(Edit4->Text,0.738); // Confiance en la tendance actuelle
   cmax=StrToIntDef(Edit5->Text,1.51); // Confiance maximale en les informatrices


  // Problème à traiter//

   xmin=StrToIntDef(Edit6->Text,0); xmax=StrToIntDef(Edit7->Text,2.7); // Intervalle pour l'espace de recherche
   D=StrToIntDef(Edit8->Text,1); // Espace de recherche
   eps=0.00001; // Précision souhaitée
   //fmin=0; // Valeur minimale à atteindre, à la précisione
   eval_max=20005; // Nombre maximum d'évaluations (comme un test d'arret

   p=0;
   s=0;
   ss=0;
   lompe=1;
  // l=0;

//---------------------------------------------------------------------------//
   // Initialisations des positions et vitesses//
 init:

      for (n=0;n<N;n++)
       {
           P[n].taille=D;
           for (d=0;d<D;d++)
               P[n].x[d]=alea(xmin,xmax);
           V[n].taille=D;
           for (d=0;d<D;d++)
                V[n].v[d]=alea ((xmin-xmax)/2, (xmax-xmin)/2) ;
                pp[0]=P[n];
                  ServerSocket1->Socket->Connections[n]->SendBuf(pp,sizeof(pp));

       }

//--------------------------------------------------------------------------//
for(n=0;n<N;n++)
{ ListClient[n].f=-100;
}

      if(ListClient[n].f!=-100)
      Form1->Caption="fffffffffffffffff";
       {Label12->Caption=P[1].f ;
       Label13->Caption=P[2].f ;
       Label15->Caption=P[3].f ;}



}               //====================== ALEA==================//
               double alea(double a,double b)
               {
               // Délivre un nombre pseudo-aléatoire entre a et b selon une distribution simulant l'uniforme//
               double r;
               // Normalement, RAND_MAX = 32767 = 2^15-1
               r=(double)rand()/RAND_MAX; // r dans [0..1]
               r=a+r*(b-a);
               return r;
               }


 //-----------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{

int i;
Memo2->Lines->Add("Serveur>"+ Memo3->Text);
if (ServerSocket1->Socket->ActiveConnections>0)

{
Form1->Caption="1";

for(i=0;i<=ServerSocket1->Socket->ActiveConnections-1;i++)
{
Form1->Caption="2";
ServerSocket1->Socket->Connections->SendText(88);
//ServerSocket1->Socket->Connections->SendBuf(Memo2->Text);

}}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
int i;

Memo2->Lines->Add("Serveur>"+ Memo3->Text);
if (ServerSocket1->Socket->ActiveConnections>0)

{
//Form1->Caption="1";


//Form1->Caption="2";

ServerSocket1->Socket->Connections[ListBox1->ItemIndex]->SendText(ListClient[ListBox1->ItemIndex].x);

//ServerSocket1->Socket->Connections->SendBuf(Memo2->Text);
//ListBox1->Panels->Items[2]->Text="  Nombre des Clients connectés  "+IntToStr(Nclients+1);

 }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
StatusBar1->Panels->Items[0]->Text=TimeToStr(Now());

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormActivate(TObject *Sender)
{
StatusBar1->Panels->Items[1]->Text=FormatDateTime("dddddd",Date());
}
//---------------------------------------------------------------------------

// Client//

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "client.h"
#define D_max 100 // Dimension maximale de l'espace de recherche
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


struct position {int taille;double x[D_max]; double f;}; // Position d'une particule et sa performance
struct vecteur {int taille;double v[D_max];}; // Vitesse d'une particule
double ma_fonction (struct position x); // Fonctions à minimiser
struct position P[D_max]; // Positions
   struct vecteur V[D_max]; // Vitesses
  // struct position x;
  struct position pp[1];
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 ClientSocket1->Port=StrToInt(Edit1->Text);
ClientSocket1->Host = Edit2->Text;
ClientSocket1->Open();
 }
//-------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Memo1->Lines->Add("Connectézzzzz à "+Socket->RemoteHost);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 ClientSocket1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 Memo3->Lines->Add("Client>"+ Memo2->Text);
Memo2->Clear();
ClientSocket1->Socket->SendText(Memo3->Text);
Memo1->Lines->Add("Informations envoyées au serveur");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connecting(TObject *Sender,
      TCustomWinSocket *Socket)
{
 Memo1->Lines->Add("Serveur a été trouvé");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
 Memo1->Lines->Add("Déconnecté");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
 if (ErrorEvent==eeGeneral)

Memo1->Lines->Add("Erreur inattendu");

if (ErrorEvent==eeSend)

Memo1->Lines->Add("Erreur d''écriture sur la connexion socket");

if (ErrorEvent==eeReceive)
Memo1->Lines->Add("Erreur de lecture sur la connexion socket");

if (ErrorEvent==eeConnect)
Memo1->Lines->Add("Une demande de connexion déjà acceptée n''a pas pu être achevée");

if (ErrorEvent==eeDisconnect)
Memo1->Lines->Add("Erreur de fermeture d''une connexion");

if (ErrorEvent==eeAccept)
Memo1->Lines->Add("Erreur d''acceptation d''une demande de connexion cliente");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
int n;
n=0;
//int liste[20];
//struct position x;
int f;
String d;
Socket->ReceiveBuf(pp,sizeof(pp));

     Memo2->Text=pp[0].x[1];

pp[0].f=ma_fonction(pp[0]);
n++;
Memo2->Text=pp[0].f;
ClientSocket1->Socket->SendBuf(pp,sizeof(pp));
//C=Socket->ReceiveBuf();
}
//---------------------------------------------------------------------------
double ma_fonction(struct position x)
               {
               // Evalue la valeur de la fonction à minimiser à la position x//
               // AJOUTEZ VOTRE PROPRE FONCTION//
               int D,d,nb_eval;
               double f,p,xd;
               nb_eval=nb_eval+1;
               D=x.taille;


                    //fonction de cour//
                    //F(x) = x3- 9 * x2+24 * x – 12//
                     f=0;
                     for(d=0;d<D;d++)
                     {
                     f=f+(x.x[d]*x.x[d]*x.x[d]-9*x.x[d]*x.x[d]+24*x.x[d]-12);
                     }


                     return f;
                     }
#2

لم أستطع فهمك أخي

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3
علاء الصالحي كتب:

لم أستطع فهمك أخي

تحياتي

على فكره يا علاء الكود سى بلس بلس مش جافا يعنى الموضوع يتنقل الى قسم السى بلس

73b14eadba.gif

الحمدلله والصلاة والسلام على رسول الله وعلى آله وصحبه أجمعين

لكل مبتدئى البرمجه برمجه بلغه السى شارب

تنصيب ويندوز 8 على vmware

مواضيع مشابهة