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 

TServerSocket SendText Exception
Goto page 1, 2  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket)
View previous topic :: View next topic  
Author Message
Eric Patterson
Guest





PostPosted: Mon Apr 17, 2006 3:03 pm    Post subject: TServerSocket SendText Exception Reply with quote



I am having this akward exception being thrown randomly by the SendText
method in a TServerSocket component. I check to make sure there is an
active connection right before I do a SendText and pass it an ansistring.
The exception is being thrown at random times usually after running more
then 6 hours. The exception that is being thrown is an invalid integer
exception. Then right after that one, the same line throws an eaccess
violation. As anyone seen this problem before or know what is wrong. Here
is my code snippet:

while(Server->ServerSocket1->Socket->Connections[ii]->SendText(AnsiString(AMsg+"\n"))==-1)
{
Sleep(10); // allow some time for Socket to send the
queued up data.
}

I am using BCD 5. Thanks.

Eric
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Apr 17, 2006 6:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote



"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:44439c71$1 (AT) newsgroups (DOT) borland.com...

Quote:
I check to make sure there is an active connection right before I do
a SendText and pass it an ansistring.

Your code is not using SendText() correctly in general. You are not taking
into account that SendText() may not send all of your data in a single
operation, requiring you to re-call SendText() multiple times in a loop in
order to send all of your string. Please go to http://www.deja.com and
search through the newsgroup archives. Sample code has been posted many
times before.

The exception is being thrown at random times usually after running more
Quote:
then 6 hours. The exception that is being thrown is an invalid integer
exception.

Do you mean that you are getting an EListError exception with a message of
"List index out of bounds (12345)" ? If so, then the only way that
particular exception can occur in the code you showed is if you are passing
an incorrect index value to the Connections[] property.


Gambit
Back to top
Eric Patterson
Guest





PostPosted: Mon Apr 17, 2006 7:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote



After thinking a minute, what about this:

int len = AMsg.Length();
while(len > 0)
{
len -=
Server->ServerSocket1->Socket->Connections[ii]->SendText(AnsiString(AMsg+"\n"))
if( len != 0)
{
Sleep(10); // allow some time for Socket to send
the queued up data.
}
}


That way it will continue to send until no more length is detected. The
only thing I don't like is that when it returns a negative value I won't do
anything but add to my length (which is probably wrong) but I figure if a
negative is returned then the OnError event should catch it.

Eric


"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4443ca2a$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:44439c71$1 (AT) newsgroups (DOT) borland.com...

I check to make sure there is an active connection right before I do
a SendText and pass it an ansistring.

Your code is not using SendText() correctly in general. You are not
taking
into account that SendText() may not send all of your data in a single
operation, requiring you to re-call SendText() multiple times in a loop in
order to send all of your string. Please go to http://www.deja.com and
search through the newsgroup archives. Sample code has been posted many
times before.

The exception is being thrown at random times usually after running more
then 6 hours. The exception that is being thrown is an invalid integer
exception.

Do you mean that you are getting an EListError exception with a message of
"List index out of bounds (12345)" ? If so, then the only way that
particular exception can occur in the code you showed is if you are
passing
an incorrect index value to the Connections[] property.


Gambit

Back to top
Eric Patterson
Guest





PostPosted: Mon Apr 17, 2006 7:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

Thanks for the insight. I understand what you mean but can't find an
example on the newsgroup to help. So I should continue to call SentText
until it returns a value of ???. I would think zero but it does state that
the return value can be less then zero also. What values less then zero can
be returned and what do they mean? Should an OnError event be written since
I will continue to SendText until it goes through? Since I am writing the
same message to multiple connected clients can I use the OnClientWrite event
somehow? Thanks.

Eric


"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4443ca2a$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:44439c71$1 (AT) newsgroups (DOT) borland.com...

I check to make sure there is an active connection right before I do
a SendText and pass it an ansistring.

Your code is not using SendText() correctly in general. You are not
taking
into account that SendText() may not send all of your data in a single
operation, requiring you to re-call SendText() multiple times in a loop in
order to send all of your string. Please go to http://www.deja.com and
search through the newsgroup archives. Sample code has been posted many
times before.

The exception is being thrown at random times usually after running more
then 6 hours. The exception that is being thrown is an invalid integer
exception.

Do you mean that you are getting an EListError exception with a message of
"List index out of bounds (12345)" ? If so, then the only way that
particular exception can occur in the code you showed is if you are
passing
an incorrect index value to the Connections[] property.


Gambit

Back to top
Eric Patterson
Guest





PostPosted: Mon Apr 17, 2006 8:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

What about this:

int len = AMsg.Length();
while(len > 0)
{
int sendReturn =
Server->ServerSocket1->Socket->Connections[ii]->SendText(AnsiString(AMsg+"\n"));
if(sendReturn == -1)
{
//need to wait for OnClientWrite
while( OnClientWriteBool != true )
{
Sleep(10);
}
OnClientWriteBool = false;
}else
{
len -= sendReturn;
}
if( len != 0)
{
Sleep(10); // allow some time for Socket to send the
queued up data.
}
}

The OnClientWriteBool is turned 'true' by the OnClientWrite event.

Eric



"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4443ca2a$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:44439c71$1 (AT) newsgroups (DOT) borland.com...

I check to make sure there is an active connection right before I do
a SendText and pass it an ansistring.

Your code is not using SendText() correctly in general. You are not
taking
into account that SendText() may not send all of your data in a single
operation, requiring you to re-call SendText() multiple times in a loop in
order to send all of your string. Please go to http://www.deja.com and
search through the newsgroup archives. Sample code has been posted many
times before.

The exception is being thrown at random times usually after running more
then 6 hours. The exception that is being thrown is an invalid integer
exception.

Do you mean that you are getting an EListError exception with a message of
"List index out of bounds (12345)" ? If so, then the only way that
particular exception can occur in the code you showed is if you are
passing
an incorrect index value to the Connections[] property.


Gambit

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Apr 17, 2006 10:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:4443e13c$1 (AT) newsgroups (DOT) borland.com...

Quote:
What about this:

You are still not checking the reason why -1 is being returned. The only
condition that is valid for the OnWrite event to be triggered is when
WSAGetLastError() returns WSAEWOULDBLOCK. You are also not taking into
account that non-blocking sockets require window messages to be processed in
order for the OnWrite event to be triggered at all, so your code is going to
deadlock indefinately since you do not process pending messages at all. And
you are still not adjusting the writing to not send characters that have
already been sent.


Gambit
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Apr 17, 2006 10:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:4443d955$1 (AT) newsgroups (DOT) borland.com...

Quote:
I understand what you mean but can't find an example on the newsgroup to
help.


Did you do a search for SendText? Did you search for SendBuf()? I know
that code examples exist in the archives on this topic.

SendText() calls SendBuf() internally. Since SendText() does not allow you
specify an index into the string, it is generally more efficient to call
SendBuf() directly and not use SendText() at all.

Quote:
So I should continue to call SentText until it returns a value of ???.

That is not the way to handle it. If you want to send 50 characters, and
SendText() only accepts 10, you have to remove the first 10 from the string
and then call SendText() again pass in only the remaining 40 characters,
repeating as needed until all 50 characters have been sent.

Quote:
I would think zero

No. A return value of zero means that the socket was disconnected by the
other party.

Quote:
but it does state that the return value can be less then zero also.

That means that a socket error occured. You have to use WSAGetLastError()
to find out which error occured. The only error you can safely ignore is
WSAEWOULDBLOCK.

Quote:
What values less then zero can be returned

-1 is the only one. It is the same value as SOCKET_ERROR, as SendText()
(and SendBuf()) simply returns whatever value the socket API send() function
returns.

Quote:
Should an OnError event be written since I will continue to SendText until
it goes through?


If an error other than WSAEWOULDBLOCK occurs, SendText() will throw an
exception, unless the OnError event handler prevents it.

Quote:
Since I am writing the same message to multiple connected clients can
I use the OnClientWrite event somehow?

The only time the OnWrite event is triggered is when a socket becomes
writable after a blocking send occured on a non-blocking socket. When
send() returns SOCKET_ERROR and WSAGetLastError() then returns
WSAEWOULDBLOCK, nothing can be sent to that socket until the OnWrite event
occurs. Any attempt to send data before that time will just produce more
WSAEWOULDBLOCK errors.


Gambit
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Apr 17, 2006 10:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:4443dd4e (AT) newsgroups (DOT) borland.com...

Quote:
After thinking a minute, what about this:
snip
That way it will continue to send until no more length is detected.

No, because you are re-sending the entire string contents every time, even
for portions of it that are successfully sent in previous loop iterations.
Youare not adjusting the sending to send only the characters that are
actually pending.

A more accurate approach is as follows:

TCustomWinSocket *Socket =
Server->ServerSocket1->Socket->Connections[ii];
AnsiString temp = AMsg + "\n";
int len;

while( temp.Length() > 0 )
{
len = Socket->SendText(temp);
if( len > 0 )
{
temp = temp.SubString(len+1, MaxInt);
continue;
}

if( (len == -1) && (WSAGetLastError() == WSAEWOULDBLOCK) )
{
Sleep(10);
continue;
}

break;
}

Alternatively:

TCustomWinSocket *Socket =
Server->ServerSocket1->Socket->Connections[ii];
AnsiString temp = AMsg + "\n";
int sent, len = temp.Length();
char *ptr = temp.c_str();

while( len > 0 )
{
sent = Socket->SendBuf(ptr, len);
if( sent > 0 )
{
ptr += sent;
len -= sent;
continue;
}

if( (sent == -1) && (WSAGetLastError() == WSAEWOULDBLOCK) )
{
Sleep(10);
continue;
}

break;
}


Quote:
I figure if a negative is returned then the OnError event should catch it.

SendText/Buf() will still return the negative value regardless of whether
the OnError event is used. All the OnError event can do is choose whether
to stop the error from throwing an exception or not.


Gambit
Back to top
Eric Patterson
Guest





PostPosted: Wed Apr 19, 2006 1:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

Thank you for all your help.

Eric


"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4443ca2a$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:44439c71$1 (AT) newsgroups (DOT) borland.com...

I check to make sure there is an active connection right before I do
a SendText and pass it an ansistring.

Your code is not using SendText() correctly in general. You are not
taking
into account that SendText() may not send all of your data in a single
operation, requiring you to re-call SendText() multiple times in a loop in
order to send all of your string. Please go to http://www.deja.com and
search through the newsgroup archives. Sample code has been posted many
times before.

The exception is being thrown at random times usually after running more
then 6 hours. The exception that is being thrown is an invalid integer
exception.

Do you mean that you are getting an EListError exception with a message of
"List index out of bounds (12345)" ? If so, then the only way that
particular exception can occur in the code you showed is if you are
passing
an incorrect index value to the Connections[] property.


Gambit

Back to top
Eric Patterson
Guest





PostPosted: Thu Apr 20, 2006 4:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

Mr. Gambit

I believe I understand how the ServerSocket should be used. I have though
had this setup working flawlessly until I moved to windows xp; I was running
on nt which makes me reluctent to change my code drastically. I did finally
get a good stack call trace of the exception that is being thrown and it is
an invalid pointer exception and the stack looks like this:

Module Unit Method Line
-----------------------------------------------------
VCL50.BPL System.pas _FreeMem

VCL50.BPL System.pas _LStrClr

VmeServer.exe MsgServer.cpp Execute 56


The starting point of the exception is coming from this line (line 56 in
MsgServer.cpp):

while(Server->ServerSocket1->Socket->Connections[ii]->SendText(AnsiString(AMsg+"\n"))==-1)


I also did some digging into onError errorCodes that the serverSocket can be
throwing and I found at no time does my application go into the onError
function. So I am hoping you have some insight into this invalid pointer
exception coming from SendText. Thanks again

Eric



"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4443ca2a$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:44439c71$1 (AT) newsgroups (DOT) borland.com...

I check to make sure there is an active connection right before I do
a SendText and pass it an ansistring.

Your code is not using SendText() correctly in general. You are not
taking
into account that SendText() may not send all of your data in a single
operation, requiring you to re-call SendText() multiple times in a loop in
order to send all of your string. Please go to http://www.deja.com and
search through the newsgroup archives. Sample code has been posted many
times before.

The exception is being thrown at random times usually after running more
then 6 hours. The exception that is being thrown is an invalid integer
exception.

Do you mean that you are getting an EListError exception with a message of
"List index out of bounds (12345)" ? If so, then the only way that
particular exception can occur in the code you showed is if you are
passing
an incorrect index value to the Connections[] property.


Gambit

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Apr 20, 2006 5:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:4447a6f6$1 (AT) newsgroups (DOT) borland.com...

Quote:
I believe I understand how the ServerSocket should be used.

Apparently you don't, because you didn't pay attention to a word I've told
you. Your code is still wrong. You are not even close to using the kind of
code I gave you earlier. Did you even try it?

Quote:
The starting point of the exception is coming from this line
(line 56 in MsgServer.cpp):


while(Server->ServerSocket1->Socket->Connections[ii]->SendText(AnsiString(AM

sg+"\n"))==-1)

As I explained to you earlier, that is a very bad way to send text to a
socket.


Gambit
Back to top
Eric Patterson
Guest





PostPosted: Thu Apr 20, 2006 6:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

That is all fine but why? I would love to fix my issue but I am not a
person to shoot shotguns in the dark and hope to hit the target. If what
you have gave me before will fix it then someone here should be able to tell
me why that will fix it. What causes the invalid pointer exception? Why
did this work in NT and now it doesn't in XP? Please tell me the reason why
I have this problem instead of telling me your way will fix it. Thanks.

Eric


"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4447b917$3 (AT) newsgroups (DOT) borland.com...
Quote:

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:4447a6f6$1 (AT) newsgroups (DOT) borland.com...

I believe I understand how the ServerSocket should be used.

Apparently you don't, because you didn't pay attention to a word I've told
you. Your code is still wrong. You are not even close to using the kind
of
code I gave you earlier. Did you even try it?

The starting point of the exception is coming from this line
(line 56 in MsgServer.cpp):


while(Server->ServerSocket1->Socket->Connections[ii]->SendText(AnsiString(AM
sg+"\n"))==-1)

As I explained to you earlier, that is a very bad way to send text to a
socket.


Gambit

Back to top
Bob Gonder
Guest





PostPosted: Thu Apr 20, 2006 7:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

Eric Patterson wrote:

Quote:
That is all fine but why? I would love to fix my issue but I am not a
person to shoot shotguns in the dark and hope to hit the target. If what
you have gave me before will fix it then someone here should be able to tell
me why that will fix it. What causes the invalid pointer exception? Why
did this work in NT and now it doesn't in XP? Please tell me the reason why
I have this problem instead of telling me your way will fix it. Thanks.

Remy explained the "why" in the same message where he explained the
"correct" way to do it.
In short:
If SendText() fails to send the entire amount (due to a slow
network perhaps, or a smaller packet size on the XP machine), then you
are resending the entire string again, and again, and again...

Quote:
while(Server->ServerSocket1->Socket->Connections[ii]->SendText(AnsiString(AM
sg+"\n"))==-1)

In addition to that, you are creating and destroying a temporary
AnsiString each time, which may or may not be a further problem (but,
if you follow Remy's solution, this particular "problem" would also go
away.)
Back to top
Eric Patterson
Guest





PostPosted: Thu Apr 20, 2006 8:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

Thank you. I did do the change and I am getting different problems now
which I am trying to fix but if SendText doesn't send the entire amount and
I fill the socket buffer wouldn't it call the ServerSocket OnError event?
My network is a 10/100 hub which is sitting right on top of the computer and
another computer sitting next to it (they are the only two items on the hub)
so I don't think speed is an issue. Did XP shrink it default packet size
versus what they had on NT? Thanks again.

Eric


"Bob Gonder" <notbg (AT) notmindspring (DOT) invalid> wrote in message
news:4mkf429644ao82ak653gcltqtf4v2g698j (AT) 4ax (DOT) com...
Quote:
Eric Patterson wrote:

That is all fine but why? I would love to fix my issue but I am not a
person to shoot shotguns in the dark and hope to hit the target. If what
you have gave me before will fix it then someone here should be able to
tell
me why that will fix it. What causes the invalid pointer exception? Why
did this work in NT and now it doesn't in XP? Please tell me the reason
why
I have this problem instead of telling me your way will fix it. Thanks.

Remy explained the "why" in the same message where he explained the
"correct" way to do it.
In short:
If SendText() fails to send the entire amount (due to a slow
network perhaps, or a smaller packet size on the XP machine), then you
are resending the entire string again, and again, and again...

while(Server->ServerSocket1->Socket->Connections[ii]->SendText(AnsiString(AM
sg+"\n"))==-1)

In addition to that, you are creating and destroying a temporary
AnsiString each time, which may or may not be a further problem (but,
if you follow Remy's solution, this particular "problem" would also go
away.)


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Apr 20, 2006 11:03 pm    Post subject: Re: TServerSocket SendText Exception Reply with quote

"Eric Patterson" <eric.patterson (AT) bepco (DOT) com> wrote in message
news:4447d8b4$1 (AT) newsgroups (DOT) borland.com...

Quote:
I did do the change and I am getting different problems now
which I am trying to fix

What problems are you having now? Please elaborate.

Quote:
if SendText doesn't send the entire amount and I fill the socket buffer
wouldn't it call the ServerSocket OnError event?

No. The OnError event is only triggered for actual socket errors. A
blocking send is not an error. WSAEWOULDBLOCK will never trigger the
OnError event.


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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.