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;
}
//--------------------------------------------------------------------------- |
Partager