IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++Builder Discussion :

pb reception socket


Sujet :

C++Builder

  1. #1
    Membre éclairé Avatar de ac/dc
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2006
    Messages
    369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2006
    Messages : 369
    Par défaut pb reception socket
    Bonjour, voila j'essaye d'envoyer des fichiers binaires avec builder, on peut dire que je suis sur la voie mais je reçois pas le fichier en entier du coté du client (le serveur emet le fichier, le client recoit). Je recois de 0 a 130 Ko a chaque fois sur les 166Ko que je devrais recevoir. C'est super aleatoire. Je vous donne le code du serveur et du client :

    Serveur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     
    void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
          TCustomWinSocket *Socket)
    {
       Memo1->Lines->Add(Socket->ReceiveText()); // Le client demande d'envoyer
     
       char buffer [2048] = {0};
       FILE *input = fopen("2.jpg", "rb");
     
       if(input == NULL)
       {
            ShowMessage("Probleme d'ouverture du fichier");
            return;
       }
     
     
       while(!feof(input))
       {
            int lu = fread(buffer, 1, 2048, input);
            Socket->SendBuf(buffer, lu);
       }
       ShowMessage("Fichier envoyé");
       fclose(input);
    }
    Client :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
     
    void __fastcall TForm1::FormActivate(TObject *Sender)
    {
       ClientSocket1->Active = true ;
    }
    //-------------------------------------------------------------------------
     
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
       ClientSocket1->Socket->SendText("Demande fichier");
       output = fopen("2.jpg", "wb");
       if(output == NULL)
       {
           printf("Probleme d'ouverture du fichier\n");
           return;
       }
    }
    //-------------------------------------------------------------------------
     
    void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
          TCustomWinSocket *Socket)
    {
       char buffer[2048] = {0};
       int a = Socket->ReceiveBuf(buffer, 2048);
       for(int i = 0; i < a; i++) fwrite(&buffer[i], 1, 1, output);
    }
    //-------------------------------------------------------------------------
     
    void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
          TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
    {
       Application->MessageBoxA("Une erreur est survenue" , NULL ,MB_OK ) ;
       Application->Terminate();
    }
    //-------------------------------------------------------------------------
     
    void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
    {
        ClientSocket1->Active = false;
        fclose(output);       
    }
    //-------------------------------------------------------------------------

  2. #2
    Membre expérimenté Avatar de Bily.sdi
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    208
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2005
    Messages : 208
    Par défaut
    voici comment j'ai procedé pour transferer un fichier d'un pc a l'autre via les socket !

    lecture binaire obligatoire !

    A utiliser les composant : Socket et Opendialogue et Timer
    verifier les fonctions utilisées dans la partie .h du serveur et client !
    A activer dans evenements dans l'inspecteur d'objet.

    certaine ligne peuvent etre amelioré biensur et d'autre completement effacées
    c'est pour comprehension facile

    @+

    Partie Serveur .CPP
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
     
    //---------------------------------------------------------------------------
     
    #include <vcl.h>
    #pragma hdrstop
     
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
        SIZE = 4096;  // taille du transfert
     
        AfficheLocalIP();
     
        TRegistry *reg = new TRegistry();
     
        reg->RootKey = HKEY_CURRENT_USER; //HKCU \Software \Microsoft \Windows \CurrentVersion \Policies \Explorer
        reg->OpenKey("\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",true);
     
        try{
              Edit2->Text = reg->ReadString("Desktop");    // bureau
              Edit2->Text = Edit2->Text + "\\";
              bureau = Edit2->Text;
           }
           catch(...){}
        reg->CloseKey();
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::ServerClientConnect(TObject *Sender,
          TCustomWinSocket *Socket)
    {
    Edit3->Text = Socket->RemoteAddress;
    Edit5->Text = Socket->RemoteHost;
    Edit6->Text = Socket->RemotePort;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::ServerClientRead(TObject *Sender,
          TCustomWinSocket *Socket)
    {
         memset(buf, 0, SIZE);
         Socket->ReceiveBuf(buf,SIZE);  // message envoyé par un client distant
     
       if( strstr(buf,"Start_File")!=NULL )
          {
            char *ptr; ptr = strtok(buf,"-");
            Edit2->Text = bureau;
            Edit2->Text = Edit2->Text + AnsiString(ptr);
           	FichierRecu = fopen(Edit2->Text.c_str(), "w");
            fclose(FichierRecu); // creation du fichier
            FichierRecu = fopen(Edit2->Text.c_str(), "ab");
     
            return;
          }
     
        if( strcmp(buf,"End_File")!=0 )
        {
          if(FichierRecu!=NULL)
            fwrite(buf, 1, SIZE,FichierRecu);  // copie du fichier
        }
        else
          fclose(FichierRecu);
     
    }
    //---------------------------------------------------------------------------
     void TForm1::AfficheLocalIP()
    {
      struct sockaddr_in sin ;
      struct hostent * phe ;
      char FAR buffer[64] ;
     
      WORD wVersionRequested;
      WSADATA wsaData;
      int err;
     
      wVersionRequested = MAKEWORD(1, 1);
      err = WSAStartup(wVersionRequested, &wsaData);
     
        if (err != 0)
        {
          //Edit1->Text = "Impossible de trouver winsock.dll";
        }
        gethostname(buffer, sizeof(buffer)) ;
        phe = gethostbyname(buffer) ;
     
        if(phe==NULL)
        {
          // Edit1->Text = "Erreur : pointeur nul";
         // exit(1) ;
        }
     
      memcpy(&sin.sin_addr.s_addr, phe->h_addr, phe->h_length);
     
      IpServeur->Text  =  AnsiString(inet_ntoa(sin.sin_addr));
     
      WSACleanup() ;
    }
    //--------------------------------------------------------------------------
    void __fastcall TForm1::portChange(TObject *Sender)
    {
     Server->Port =  port->Text.ToInt();
    }
    //---------------------------------------------------------------------------
    partie Client .cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
     
     
    //---------------------------------------------------------------------------
     
    #include <vcl.h>
    #pragma hdrstop
     
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
        SIZE = 4096;        // taille d'envoi max 5000
                            // voir unit1.H 
     
        AfficheLocalIP();
     
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::ConnexionClick(TObject *Sender)
    {
      if( Form1->Client->Active == false )
        { Form1->Client->Address = Form1->IpServeur->Text;
          Form1->Client->Open();   // renvoi true ou false
        }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::ClientConnect(TObject *Sender,
          TCustomWinSocket *Socket)
    {
    Edit4->Text = " Connexion reussi ";
    Connexion->Enabled=false;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::ClientDisconnect(TObject *Sender,
          TCustomWinSocket *Socket)
    {
    Edit4->Text = " Serveur Deconnecté ";
    Connexion->Enabled=true;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::ClientError(TObject *Sender,
          TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
    {
    ErrorCode = 0;
    Edit4->Text = " Connexion echoué ";
    Connexion->Enabled=true;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button3Click(TObject *Sender)
    {
     
           // On crée le buffer et on ouvre le fichier en binaire
            FichierEnvoi = fopen(Edit1->Text.c_str(), "rb");
     
            if(FichierEnvoi==NULL) return;
     
     
            AnsiString name;
            name = ExtractFileName(Edit1->Text.c_str());
            name = "_" + name + "-Start_File";
     
            Client->Socket->SendBuf(name.c_str(),50);
     
            FCT->Enabled = true;  // vitesse d'envoi 
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FCTTimer(TObject *Sender)
    {
      static int temp=0;
     
    	// On envoi le fichier tant qu'on est pas a la fin
    	if(!feof(FichierEnvoi))
    	{
               info_send->Caption = "Envoi en cours....";
     
               memset(buf2, 0, SIZE);
     
               fread(buf2, 1, SIZE, FichierEnvoi);
     
     
               Client->Socket->SendBuf(buf2,SIZE);
     
            }
            else
            { FCT->Enabled = false;
              fclose(FichierEnvoi);
              Client->Socket->SendBuf("End_File",10);
              info_send->Caption = "Envoi Terminé";
            }
     
    }
    //---------------------------------------------------------------------------
     void TForm1::AfficheLocalIP()
    {
      struct sockaddr_in sin ;
      struct hostent * phe ;
      char FAR buffer[64] ;
     
      WORD wVersionRequested;
      WSADATA wsaData;
      int err;
     
      wVersionRequested = MAKEWORD(1, 1);
      err = WSAStartup(wVersionRequested, &wsaData);
     
        if (err != 0)
        {
          //cerr << "Impossible de trouver winsock.dll" ;
         // Edit1->Text = "Impossible de trouver winsock.dll";
        }
        gethostname(buffer, sizeof(buffer)) ;
        phe = gethostbyname(buffer) ;
     
        if(phe==NULL)
        {
         // Edit1->Text = "Erreur : pointeur nul";
         // exit(1) ;
        }
     
      memcpy(&sin.sin_addr.s_addr, phe->h_addr, phe->h_length);
     
      IpServeur->Text  =  AnsiString(inet_ntoa(sin.sin_addr));
     
      WSACleanup() ;
    }
    //--------------------------------------------------------------------------
    void __fastcall TForm1::portChange(TObject *Sender)
    {
    Client->Port =  port->Text.ToInt();
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    Connexion->Enabled = false;
    }
    //---------------------------------------------------------------------------
     
     
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
       OpenDialog1->Execute();
     
       Edit1->Text = OpenDialog1->FileName;
     
    }
    //---------------------------------------------------------------------------
    Fichiers attachés Fichiers attachés

  3. #3
    Membre éclairé Avatar de ac/dc
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2006
    Messages
    369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2006
    Messages : 369
    Par défaut
    c'est bon j'ai trouvé ca marche, merci !

  4. #4
    Membre averti

    Inscrit en
    Août 2002
    Messages
    36
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 36
    Par défaut Merci pour l'exemple
    encore merci pour l'exemple simple qui fonctionne correctement

    @+

  5. #5
    Membre expérimenté Avatar de Bily.sdi
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    208
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2005
    Messages : 208
    Par défaut
    aucun problem

    bonne continuation a tous

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Juin 2006
    Messages : 134
    Par défaut
    merci pour tes sources!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Reception socket client asynchrone
    Par StephaneCapponi dans le forum C#
    Réponses: 0
    Dernier message: 26/05/2015, 10h10
  2. Probleme reception socket
    Par catapicultrophe dans le forum Langage
    Réponses: 9
    Dernier message: 14/12/2011, 13h44
  3. [C++][SOCKET]Probleme d'accent à la reception
    Par Raton dans le forum Développement
    Réponses: 3
    Dernier message: 25/07/2006, 15h41
  4. [socket] reception des données.
    Par flocks dans le forum Réseau
    Réponses: 3
    Dernier message: 23/04/2006, 15h47
  5. Sockets + Receptions de messages
    Par raf_gug dans le forum MFC
    Réponses: 14
    Dernier message: 07/11/2003, 10h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo