"Remy Lebeau (TeamB)" <
XXXX@XXXXX.COM >wrote in message
Quote
"Egi2kaZz" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>Well I'm using your code posted 2005 01 11 "please help
>me..socket file transfer "
Ok.
>And if don't use:
<snip>
>Client's program crashes with no responding :(
You need to be more specific. What does all of the client's reading code
look like? What is happening when it crashes? Are there any error
messages? Please show code for both parties that actually compiles.
client's code:
bool __fastcall ReadRaw(TCustomWinSocket *Socket, LPVOID Buffer,
int
BufSize)
{
int read;
LPBYTE pBuf = (LPBYTE) Buffer;
while( BufSize>0 )
{
read = Socket->ReceiveBuf(pBuf, BufSize);
if( read == -1 )
{
if( WSAGetLastError() != WSAEWOULDBLOCK )
return false;
}
else
{
pBuf += read;
BufSize -= read;
}
}
return true;
}
bool __fastcall ReadStream(TCustomWinSocket *Socket, TStream *Stream)
{
BYTE buf[1024];
int BufSize;
int BytesToRead = 0;
if( !ReadRaw(Socket, &BytesToRead, sizeof(int)) )
return false;
while( BytesToRead>0 )
{
if( BytesToRead>= 1024 )
BufSize = 1024;
else
BufSize = BytesToRead;
if( !ReadRaw(Socket, buf, BufSize) )
return false;
Stream->Write(buf, BufSize);
BytesToRead -= BufSize;
}
return true;
}
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
TFileStream *file = new TFileStream("film.avi",fmCreate);
try {
ReadStream(Socket, file);
}
__finally {
delete file;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ClientSocket1->Host = "192.168.0.1";
ClientSocket1->Address = "192.168.0.22";
ClientSocket1->Active = true;
}
//---------------------------------------------------------------------------
Server's code:
bool __fastcall SendRaw(TCustomWinSocket *Socket, LPVOID Buffer, int
BufSize)
{
int sent;
LPBYTE pBuf = (LPBYTE) Buffer;
while( BufSize>0 )
{
sent = Socket->SendBuf(pBuf, BufSize);
if( sent == -1 )
{
if( WSAGetLastError() != WSAEWOULDBLOCK )
return false;
}
else
{
pBuf += sent;
BufSize -= sent;
}
}
return true;
}
bool __fastcall SendStream(TCustomWinSocket *Socket, TStream *Stream)
{
BYTE buf[1024];
int BytesToSend = (Stream->Size - Stream->Position);
if( !SendRaw(Socket, &BytesToSend, sizeof(int)) )
return false;
while( BytesToSend>0 )
{
int read = Stream->Read(buf, sizeof(buf));
if( read < 1 ) // unexpected end-of-stream
return false;
if( !SendRaw(Socket, buf, read) )
return false;
BytesToSend -= read;
}
return true;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if( OpenDialog1->Execute() )
{
TFileStream* file = new TFileStream(OpenDialog1->FileName,
fmOpenRead);
try
{
if(
!SendStream(ServerSocket1->Socket->Connections[0],file) )
ShowMessage("Could not send file");
}
__finally {
delete file;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ServerSocket1->Active = true;
}
//---------------------------------------------------------------------------
first I start server's program, then client's..I can see that file is
transfering if I send
big file 700mb(it's takes more time:)), in client pc it's growing, BUT in
the end program stop responding
with no errors and then I close it,file became 0,0kb size..
As I mentioned, I if I place:
try {
ReadStream(Socket, file);
}
__finally {
delete file;
ClientSocket1->Active=false; <----
}
then program work's fine, but every time I send file, have to disactive,
active..:(
e.