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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
|
//---------------------------------------------------------------------------
UNIT1.cpp
#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;
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{
Edit1->Text = reg->ReadString("Desktop"); // bureau
Edit1->Text = Edit1->Text + "\\";
Edit2->Text = Edit1->Text;
bureau = Edit1->Text;
}
catch(...){}
reg->CloseKey();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Pub->Picture = NULL;
OpenDialog1->Execute();
Edit1->Text = OpenDialog1->FileName;
if(Edit1->Text.IsEmpty()) return;
AnsiString Dest;
Dest = ExtractFilePath(Edit2->Text.c_str()) + ExtractFileName(Edit1->Text.c_str());
Edit2->Text = Dest;
// On crée le buffer et on ouvre le fichier en binaire
FILE *FichierEnvoi = fopen(Edit1->Text.c_str(), "rb");
// Crée le fichier en mode binaire
FILE *FichierRecu = fopen(Edit2->Text.c_str(), "wb");
// On envoi le fichier tant qu'on est pas a la fin
while(!feof(FichierEnvoi))
{
memset(buf2, 0, SIZE);
fread(buf2, 1, SIZE, FichierEnvoi); // lecture
fwrite(buf2, 1, SIZE, FichierRecu); // ecriture binaire pour l'envoie
}
fclose(FichierEnvoi); fclose(FichierRecu);
try
{Pub->Picture->LoadFromFile(Edit2->Text);}
catch(...){};
}
//---------------------------------------------------------------------------
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;
Edit3->Text = Socket->RemoteAddress;
Edit5->Text = Socket->RemoteHost;
Edit6->Text = Socket->RemotePort;
}
//---------------------------------------------------------------------------
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::ServerClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Edit3->Text = Socket->RemoteAddress;
Edit5->Text = Socket->RemoteHost;
Edit6->Text = Socket->RemotePort;
Edit4->Text = " Connexion Reussi ";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode = 0;
Edit4->Text = " Serveur Deconnecté ";
Connexion->Enabled=true;
}
//---------------------------------------------------------------------------
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);
sec = (sec + FCT->Interval);
if(sec==1000) { sec=0; min++; }
if(min==60) { her++; min=0; }
timer->Caption = AnsiString(her) + ":" + AnsiString(min);// + ":" + AnsiString(sec);
Client->Socket->SendBuf(buf2,SIZE);
}
else
{ FCT->Enabled = false;
temp = 0; sec=min=her=0;
fclose(FichierEnvoi);
Client->Socket->SendBuf("End_File",10);
info_send->Caption = "Envoi Terminé";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
error->Visible = false;
// On crée le buffer et on ouvre le fichier en binaire
FichierEnvoi = fopen(Edit1->Text.c_str(), "rb");
if(FichierEnvoi==NULL) return;
try
{Pub->Picture->LoadFromFile(Edit1->Text);}
catch(...){};
AnsiString name;
name = ExtractFileName(Edit1->Text.c_str());
name = "_" + name + "-Start_File";
Client->Socket->SendBuf(name.c_str(),50);
timer->Caption = "00:00:00";
FCT->Enabled = true;
}
//---------------------------------------------------------------------------
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 )
{ info_send->Caption = "Envoi en cours....";
char *ptr; ptr = strtok(buf,"-");
Edit2->Text = bureau;
Edit2->Text = Edit2->Text + AnsiString(ptr);
FichierRecu = fopen(Edit2->Text.c_str(), "w");
fclose(FichierRecu);
FichierRecu = fopen(Edit2->Text.c_str(), "ab");
Pub->Picture = NULL;
return;
}
if( strcmp(buf,"End_File")!=0 )
{
sec = (sec + FCT->Interval);
if(sec==1000) { sec=0; min++; }
if(min==60) { her++; min=0; }
timer->Caption = AnsiString(her) + ":" + AnsiString(min);// + ":" + AnsiString(sec);
if(FichierRecu!=NULL)
fwrite(buf, 1, SIZE,FichierRecu);
else
error->Visible = true;
}
else
{ fclose(FichierRecu);
info_send->Caption = "Envoi Terminé";
sec=min=her=0;
Pub->Picture = NULL;
try
{Pub->Picture->LoadFromFile(Edit2->Text);}
catch(...){};
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
Connexion->Enabled = false;
Server->Active = true;
AfficheLocalIP();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton2Click(TObject *Sender)
{
Connexion->Enabled = true;
Server->Active = false;
IpServeur->Text = "000.000.000.000";
}
//---------------------------------------------------------------------------
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)
{
// 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::ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
Application->MessageBox("Client a recu ?","FCT", MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::portChange(TObject *Sender)
{
Client->Port = port->Text.ToInt();
Server->Port = port->Text.ToInt();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PubClick(TObject *Sender)
{
TJPEGImage *jpg = new TJPEGImage();
Graphics :: TBitmap *bmp = new Graphics :: TBitmap();
TRect *rect = new TRect();
TPicture *img = new TPicture();
img->Bitmap->Height = Screen->Height;
img->Bitmap->Width = Screen->Width;
int scrw = Screen->Width, scrh = Screen->Height;
HWND hwnd = GetDesktopWindow();
HDC hDC = GetDC(hwnd);
BitBlt(img->Bitmap->Canvas->Handle,0,0,scrw,scrh,hDC,0,0,SRCCOPY);
try
{
jpg->Assign(img->Bitmap);
jpg->CompressionQuality = 50;
jpg->Assign(img->Bitmap);
bmp->Width = jpg->Width -50;
bmp->Height = jpg->Height -50;
rect->Left = 0;
rect->Top = 0;
rect->Right = bmp->Width-1;
rect->Bottom = bmp->Height-1;
bmp->Canvas->StretchDraw(*rect,jpg);
jpg->Assign(img->Bitmap);
jpg->SaveToFile("c:\\img.jpg");
jpg->Free();
bmp->Free();
img->Free();
}catch(...){}
ReleaseDC(hwnd,hDC);
Pub->Picture->LoadFromFile("c:\\img.jpg");
}
//-------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Connexion->Enabled = false;
Server->Active = false;
Client->Active = false;
}
//-------------------------------------------------------------------------
UNIT1.H
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "stdio.h"
#include <ExtCtrls.hpp>
#include <ScktComp.hpp>
#include <jpeg.hpp>
#include <Registry.hpp>
#include <Dialogs.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // Composants gérés par l'EDI
TGroupBox *GroupBox1;
TEdit *Edit1;
TButton *Button2;
TEdit *Edit2;
TButton *Connexion;
TEdit *IpServeur;
TGroupBox *GroupBox2;
TImage *Pub;
TServerSocket *Server;
TClientSocket *Client;
TEdit *Edit4;
TGroupBox *GroupBox3;
TEdit *Edit3;
TLabel *Label2;
TLabel *Label3;
TEdit *Edit5;
TLabel *Label4;
TEdit *Edit6;
TButton *Button3;
TTimer *FCT;
TRadioGroup *RadioGroup1;
TLabel *Label5;
TLabel *Label6;
TRadioButton *RadioButton1;
TRadioButton *RadioButton2;
TLabel *info_send;
TLabel *timer;
TLabel *error;
TEdit *port;
TOpenDialog *OpenDialog1;
TLabel *Label1;
TButton *Button1;
TLabel *Label7;
void __fastcall Button2Click(TObject *Sender);
void __fastcall ConnexionClick(TObject *Sender);
void __fastcall ClientConnect(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall ClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent,
int &ErrorCode);
void __fastcall ServerClientConnect(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall ServerClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent,
int &ErrorCode);
void __fastcall FCTTimer(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall ServerClientRead(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall RadioButton1Click(TObject *Sender);
void __fastcall RadioButton2Click(TObject *Sender);
void __fastcall ClientRead(TObject *Sender,
TCustomWinSocket *Socket);
void __fastcall portChange(TObject *Sender);
void __fastcall PubClick(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // Déclarations utilisateur
char buf2[5000];
FILE *FichierEnvoi;
char buf[5000];
FILE *FichierRecu;
float sec,min,her;
int SIZE;
AnsiString bureau;
void AfficheLocalIP();
public: // Déclarations utilisateur
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
UNIT1.DMF
object Form1: TForm1
Left = 217
Top = 213
Width = 977
Height = 565
AutoSize = True
BorderIcons = [biSystemMenu]
Caption = 'Create Soft : CFT , Copie File Transfer ( Lan & Wan )'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object GroupBox1: TGroupBox
Left = 0
Top = 2
Width = 313
Height = 529
Caption = 'Connexion'
TabOrder = 0
object Label5: TLabel
Left = 8
Top = 472
Width = 104
Height = 13
Caption = 'Chemin de destination'
end
object Label6: TLabel
Left = 10
Top = 375
Width = 66
Height = 13
Caption = 'Fichier source'
end
object Label1: TLabel
Left = 10
Top = 117
Width = 73
Height = 15
Caption = 'IP PC Serveur'
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Times New Roman'
Font.Style = []
ParentFont = False
end
object Edit1: TEdit
Left = 8
Top = 401
Width = 297
Height = 21
TabOrder = 0
Text = 'D:\Documents and Settings\Végéta\Bureau\pub.bmp'
end
object Button2: TButton
Left = 8
Top = 434
Width = 144
Height = 26
Caption = 'Lecture et copie sur disque '
TabOrder = 1
OnClick = Button2Click
end
object Edit2: TEdit
Left = 8
Top = 496
Width = 297
Height = 21
TabOrder = 2
Text = 'D:\Documents and Settings\Végéta\Bureau\pub2.bmp'
end
object Connexion: TButton
Left = 8
Top = 150
Width = 289
Height = 25
Caption = 'Connexion'
TabOrder = 3
OnClick = ConnexionClick
end
object IpServeur: TEdit
Left = 90
Top = 115
Width = 213
Height = 21
TabOrder = 4
Text = '000.000.000.000'
end
object Edit4: TEdit
Left = 8
Top = 184
Width = 292
Height = 21
ReadOnly = True
TabOrder = 5
end
object GroupBox3: TGroupBox
Left = 7
Top = 228
Width = 295
Height = 133
Caption = 'info-Client'
Enabled = False
TabOrder = 6
object Label2: TLabel
Left = 34
Top = 25
Width = 38
Height = 13
Caption = 'Ip Client'
end
object Label3: TLabel
Left = 33
Top = 61
Width = 51
Height = 13
Caption = 'Host Client'
end
object Label4: TLabel
Left = 33
Top = 99
Width = 48
Height = 13
Caption = 'Port Client'
end
object Edit3: TEdit
Left = 97
Top = 22
Width = 165
Height = 21
TabOrder = 0
end
object Edit5: TEdit
Left = 97
Top = 58
Width = 165
Height = 21
TabOrder = 1
end
object Edit6: TEdit
Left = 97
Top = 96
Width = 165
Height = 21
TabOrder = 2
end
end
object Button3: TButton
Left = 165
Top = 436
Width = 139
Height = 25
Caption = 'Envoyer Fichier "FCT"'
TabOrder = 7
OnClick = Button3Click
end
object RadioGroup1: TRadioGroup
Left = 8
Top = 17
Width = 297
Height = 88
Caption = 'Type'
TabOrder = 8
end
object RadioButton1: TRadioButton
Left = 20
Top = 42
Width = 61
Height = 17
Caption = 'Serveur'
TabOrder = 9
OnClick = RadioButton1Click
end
object RadioButton2: TRadioButton
Left = 19
Top = 75
Width = 55
Height = 17
Caption = 'Client'
Checked = True
TabOrder = 10
TabStop = True
OnClick = RadioButton2Click
end
object port: TEdit
Left = 142
Top = 36
Width = 73
Height = 21
TabOrder = 11
Text = '7879'
OnChange = portChange
end
object Button1: TButton
Left = 112
Top = 72
Width = 161
Height = 25
Caption = 'deconnecter'
Font.Charset = DEFAULT_CHARSET
Font.Color = clMaroon
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
ParentFont = False
TabOrder = 12
OnClick = Button1Click
end
end
object GroupBox2: TGroupBox
Left = 318
Top = 0
Width = 651
Height = 529
Caption = 'Image'
TabOrder = 1
object Pub: TImage
Left = 6
Top = 45
Width = 640
Height = 480
Stretch = True
OnClick = PubClick
end
object info_send: TLabel
Left = 72
Top = 18
Width = 138
Height = 24
Caption = 'Envoi Terminé'
Font.Charset = DEFAULT_CHARSET
Font.Color = clMaroon
Font.Height = -19
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
end
object timer: TLabel
Left = 264
Top = 16
Width = 70
Height = 24
Caption = '00:00:00'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -19
Font.Name = 'MS Sans Serif'
Font.Style = []
ParentFont = False
end
object error: TLabel
Left = 400
Top = 16
Width = 230
Height = 24
Caption = 'Error fichier destinataire '
Font.Charset = DEFAULT_CHARSET
Font.Color = clMaroon
Font.Height = -19
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
Transparent = True
Visible = False
end
object Label7: TLabel
Left = 304
Top = 272
Width = 42
Height = 13
Caption = 'Clique içi'
end
end
object Server: TServerSocket
Active = False
Port = 0
ServerType = stNonBlocking
OnClientConnect = ServerClientConnect
OnClientRead = ServerClientRead
OnClientError = ServerClientError
Left = 376
Top = 104
end
object Client: TClientSocket
Active = False
ClientType = ctNonBlocking
Port = 7879
OnConnect = ClientConnect
OnDisconnect = ClientDisconnect
OnRead = ClientRead
OnError = ClientError
Left = 344
Top = 104
end
object FCT: TTimer
Enabled = False
Interval = 250
OnTimer = FCTTimer
Left = 464
Top = 104
end
object OpenDialog1: TOpenDialog
Filter = '*.bmp;*.jpg;*.*";'
Options = [ofReadOnly, ofOverwritePrompt, ofHideReadOnly, ofShowHelp, ofAllowMultiSelect, ofExtensionDifferent, ofPathMustExist, ofFileMustExist, ofCreatePrompt, ofShareAware, ofNoReadOnlyReturn]
Left = 360
Top = 336
end
end |
Partager