BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SendBuf Socket Delay

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students)
View previous topic :: View next topic  
Author Message
BJ
Guest





PostPosted: Sat Apr 07, 2007 7:08 am    Post subject: SendBuf Socket Delay Reply with quote



Hello,

The code below I borrow it from Gambit which works great. But I
want to delay the sending every 100 bytes for 100 millisecond.
Is having a Sleep function in the loop a correct way to do it?
I am afraid that the Sleep function in my code will hang the
application and not delay the sending of 100 bytes.

bool __fastcall TEthernet::SendStream(TCustomWinSocket *Socket, TStream *Stream)
{
BYTE buffer[100]; //1024
int Size = Stream->Size;
AnsiString tempBuffer = "";
int NumWrite = 0;
int NumToSend = (Size - Stream->Position);

try
{
while( NumToSend > 0 )
{
int NumRead = Stream->Read(buffer, min(NumToSend, sizeof(buffer)));
if( NumRead < 1 )
return false;

NumWrite = SendBuf(Socket, buffer, NumRead);
if( !NumWrite )
return false;
else
{
tempBuffer = "num sent: " + NumWrite;
StatusFromPrinterMemo->Lines->Add(tempBuffer);
}

NumToSend -= NumRead;

Sleep(100);
}
}
catch(const Exception &)
{
return false;
}

return true;
}
Back to top
chenzero
Guest





PostPosted: Sat Apr 07, 2007 7:08 am    Post subject: Re: SendBuf Socket Delay Reply with quote



"BJ" <caophong (AT) gmail (DOT) com> 写入消息新闻:4616ef1d$1 (AT) newsgroups (DOT) borland.com...
Quote:


Hello,
The common solution for executing a long time task in the "main" thread
which
freezes the UI is executing the long time task in another thread.
In c++builder, there is a class named "TThread" for convinent use. however,
in my opinion, it's always not easy to write a bug free multi-threading
program.

On the net, there are lots of resources on discussing how to write
multi-thread
program,a good article is:
http://cc.codegear.com/Item.aspx?id=14809
hope it helps.
zero
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sat Apr 07, 2007 7:24 am    Post subject: Re: SendBuf Socket Delay Reply with quote



"BJ" <caophong (AT) gmail (DOT) com> wrote in message
news:4616ef1d$1 (AT) newsgroups (DOT) borland.com...

Quote:
The code below I borrow it from Gambit which works great. But
I want to delay the sending every 100 bytes for 100 millisecond.
Is having a Sleep function in the loop a correct way to do it?

Yes.

Quote:
I am afraid that the Sleep function in my code will hang the
application

The code you are using was intentially designed to block the calling
thread until the function exits. So if you are calling it in the
context of the main thread, then the application will appear frozen
until the function exits. Putting a Sleep() into the loop does not
change that, only makes it more noticable to the user. That is why I
don't normally use sockets in the main thread to begin with.

Quote:
and not delay the sending of 100 bytes.

It will definately delay the sending.

Quote:
NumWrite = SendBuf(Socket, buffer, NumRead);
if( !NumWrite )
return false;

You are not checking to see if SendBuf() returns < 0, which indicates
a socket error. Using the '!' operator like that will only be able to
detect a result of 0, which indicated a graceful socket disconnect
occured.


Gambit
Back to top
BJ
Guest





PostPosted: Sat Apr 07, 2007 8:10 am    Post subject: Re: SendBuf Socket Delay Reply with quote

Hi Guys,

Yeah...working with thread will be more stable. But my
application is pretty small. I don't care if my application
hang at all, just as long as it completely sends out the 100
bytes and wait for 100milisecond. I am happy.
Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Mon Apr 09, 2007 6:36 pm    Post subject: Re: SendBuf Socket Delay Reply with quote

"Remy Lebeau \(TeamB\)" <no.spam (AT) no (DOT) spam.com> writes:

Quote:
The code you are using was intentially designed to block the calling
thread until the function exits. So if you are calling it in the
context of the main thread, then the application will appear frozen
until the function exits. Putting a Sleep() into the loop does not
change that, only makes it more noticable to the user. That is why
I don't normally use sockets in the main thread to begin with.

Using blocking sockets virtually requires a background thread to
service the connection, but it doesn't scale well when you have many
sockets. Using non-blocking sockets works well in the main thread, as
it's also "event driven" though by a different mechanism than the main
VCL event loop.

Not that I'm advocating this, but it's a very viable alternative for
multi-threadding simply for the sake of getting around blocking socket
calls.

--
Chris (TeamB);
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.