#include <vcl.h>
#pragma hdrstop
#include "stdio.h"
#include "Client.h"
#define BLOCK 1024
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TPCClient *PCClient;
char *Buffer[BLOCK];
//---------------------------------------------------------------------------
__fastcall TPCClient::TPCClient(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TPCClient::Creacio(TObject *Sender)
{
TextEnviar->Enabled = false;
EnviarText->Enabled = false;
EnviarArxiu->Enabled = false;
Desconnectar->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TPCClient::ConnectarClick(TObject *Sender)
{
ClientSocket1->Host = String(IPHost->Text);
ClientSocket1->Port = StrToInt(NumPort->Text);
ClientSocket1->Active = true;
TextEnviar->Enabled = true;
EnviarText->Enabled = true;
EnviarArxiu->Enabled = true;
IPHost->Enabled = false;
NumPort->Enabled = false;
NomUsuari->Enabled = false;
Connectar->Enabled = false;
Desconnectar->Enabled = true;
ShowMessage ("Connectat correctament");
}
//---------------------------------------------------------------------------
void __fastcall TPCClient::DesconnectarClick(TObject *Sender)
{
TextEnviar->Enabled = false;
EnviarText->Enabled = false;
EnviarArxiu->Enabled = false;
IPHost->Enabled = true;
NumPort->Enabled = true;
NomUsuari->Enabled = true;
Desconnectar->Enabled = false;
Connectar->Enabled = true;
ShowMessage ("Desconnectat correctament");
}
//---------------------------------------------------------------------------
void __fastcall TPCClient::EnviarTextClick(TObject *Sender)
{
if (TextEnviar->Text.IsEmpty()) return;
String strToSend = NomUsuari->Text + " : " + TextEnviar->Text;
ClientSocket1->Socket->SendText(strToSend);
this->TextEnviar->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TPCClient::ApretarTecla(TObject *Sender, char &Key)
{
if(Key == VK_RETURN) EnviarTextClick(this);
}
//---------------------------------------------------------------------------
void __fastcall TPCClient::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
//ShowMessage("Error en l'enviament");
//ErrorCode = 0;
}
//---------------------------------------------------------------------------
long TPCClient::TamanyArxiu(FILE *fitxer)
{
long actual, longitud;
actual = ftell(fitxer);
fseek(fitxer, 0L, SEEK_END);
longitud = ftell(fitxer);
fseek(fitxer, actual, SEEK_SET);
return longitud;
}
//---------------------------------------------------------------------------
void __fastcall TPCClient::EnviarArxiuClick(TObject *Sender)
{
//if (TextEnviar->Text.IsEmpty()) return;
FILE *Arxiu = fopen("C:UsersAdminanillo.jpg", "rb");
long longarxiu = TamanyArxiu(Arxiu);
//ClientSocket1->Socket->SendText("ARXIU PER ENVIAR");
ClientSocket1->Socket->SendText(longarxiu);
int i = 0, llegir = 0;
while (i < longarxiu) {
llegir = fread (Buffer, 1024, 1, Arxiu);
ClientSocket1->Socket->SendBuf(Buffer, llegir);
i = i+llegir;
}
fclose (Arxiu);
ShowMessage ("Arxiu enviat");
//ClientSocket1->Socket->SendText("ARXIU ENVIAT");
//this->TextEnviar->Text = "";
}