🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
13 messages
13 total messages Started by Billy Holliday Wed, 21 Sep 2005 06:48
TServerSocket sends a unwanted acknowlege
#3466
Author: Billy Holliday
Date: Wed, 21 Sep 2005 06:48
58 lines
2822 bytes
Hello.

I have two problem with TServerSocket component.

I want to have a easy TCP/IP - port connection with a client.
If the client send a data package, i want to send a special response to this
data package.
I do this in the event:
ServerSocketClientRead( TObject *Sender, TCustomWinSocket *Socket )
{
    count = Socket->ReceiveLength ();  // checking how much bytes received
    unsigned char   Buff[5000];  // make a big buffer
    Socket->ReceiveBuf ( Buff, count );  // read the data

   // processing the data..

  char response[20];  // sendbuffer

  // building the response, terminated with 0x00

   Socket->SendText(response);  // send the response (19 Bytes)
}


Now the two problems:

Sometimes the TCP/IP send a ACKnowlage packege with data length zero by
himself.
(I see it on a package analyser..)
After this, the really response, send with the SendText methode, reveived.

But the client checks the first resonse (zero bytes) and has a problem. (I
can't change the client !)

Can i switch off the automativ send of a ACK with zero bytes. I think, it
was send by the tcp/ip stack automatically.   rightly ?
I only want to send my special response, no other....


The second problem is:
Sometimes the client is sending a data package. The TServerSocket received
it and send's a response. But the response wasn't send over ethernet line.
Than a timeout on the client reached (2 secondes later) and a second data
package was send. The TServerSocked received the data and send a second
answer packed.

Now is this:  over the ethernet line, ONE packed was send, containing both
resonses (2 * 19 bytes)...

Why is this? I think the TServerSocket has time enougt to send the first
response.....
A   Application->ProcessMessages() after Socket->SendText(response);
doesn't resolve the problem.

How can i make sure, the response packed is send immediate

Please help me...
thanks.
Re: TServerSocket sends a unwanted acknowlege
#3465
Author: Remy Lebeau (TeamB)
Date: Wed, 21 Sep 2005 06:57
62 lines
3955 bytes
"Billy Holliday" <NoSpam@Hohoho.com> wrote in message
news:433101e8@newsgroups.borland.com...

> Sometimes the TCP/IP send a ACKnowlage packege with
> data length zero by himself.
> (I see it on a package analyser..)

What you describe should only be occuring at a lower level of the network
architecture, if at all.  You do not need to concern yourself with it.

> But the client checks the first resonse (zero bytes) and has a problem.

There is no such packet being sent at the application level, nor should it
be received at the application level, either.  Your cliet should only be
receiving data that you have explicitally sent in your own code.

> I only want to send my special response, no other....

That is exactly what should already be happening.  Every TCP project I have
ever seen and/or written has worked that way as expected on every system it
is run on.

> Sometimes the client is sending a data package. The TServerSocket
> received it and send's a response. But the response wasn't send
> over ethernet line.

That is possible, and likely.  See below.

> Than a timeout on the client reached (2 secondes later) and a
> second data package was send. The TServerSocked received
> the data and send a second answer packed.
>
> Now is this:  over the ethernet line, ONE packed was send,
> containing both resonses (2 * 19 bytes)...

That is quite possible, and to be expected since you are sending very small
packets.  By default, a socket will internally buffer all outgoing data and
send it over the connection only when a sufficient amount of data has been
buffered to warrant an efficient transmission.  This is known as "send
coalescing", also known as the "Nagle algorithm".  You can use setsockopt()
to disable it so that small packets are transmitted immediately, though it
may cause performance hits to do so.  According to MSDN:

    "The TCP_NODELAY option is specific to TCP/IP service providers.
Enabling the TCP_NODELAY option disables the TCP Nagle Algorithm (and vice
versa). The Nagle algorithm (described in RFC 896) is very effective in
reducing the number of small packets sent by a host by essentially buffering
send data if there is unacknowledged data already "in flight" or until a
full-size packet can be sent. It is highly recommended that TCP/IP service
providers enable the Nagle Algorithm by default, and for the vast majority
of application protocols the Nagle Algorithm can deliver significant
performance enhancements. However, for some applications this algorithm can
impede performance, and TCP_NODELAY can be used to turn it off. These are
applications where many small messages are sent, which need to be received
by the peer with the time delays between the messages maintained.
Application writers should not set TCP_NODELAY unless the impact of doing so
is well-understood and desired, since setting TCP_NODELAY can have a
significant negative impact of network and application performance."


Gambit
Re: TServerSocket sends a unwanted acknowlege
#3467
Author: Billy Holliday
Date: Wed, 21 Sep 2005 08:04
25 lines
1502 bytes
Hello Remy Lebeau.


Many thanks  :-)


Don't cry, i'm a newbye.....

How and where to set the TCP_NODELAY option.

Is the OnClientConnect event the right place?

I thy this:
void __fastcall TForm::ServerSocketConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
    int fl = 1;
    setsockopt(Socket, IPPROTO_TCP, TCP_NODELAY, &fl, sizeof(fl));
}

but the complier says: can't convert TCustomWinSocket to int!!
in winsock.h the first parameter is a type SOCKET, so i think Socket is
right....

What's wront?
Re: TServerSocket sends a unwanted acknowlege
#3469
Author: Hans Galema
Date: Wed, 21 Sep 2005 08:17
16 lines
1486 bytes
Billy Holliday wrote:

> How and where to set the TCP_NODELAY option.
> 
> Is the OnClientConnect event the right place?

Do not set it.

Better add a Sleep(500); statement after every message sent
to the client. The socket not only sends when a sufficient amount
of data has been buffered but also when a certain amount of
time has passed.

Note: You can experiment with the parameter for Sleep().

Hans.
Re: TServerSocket sends a unwanted acknowlege
#3470
Author: Billy Holliday
Date: Wed, 21 Sep 2005 08:47
12 lines
1202 bytes
Hi Hans.

Sleep() will be a bad solution, because i have to get about 30.000 data
packed's
With Sleep(500) the transmition takes more than 4 hour.....(should be 4-5
minutes..)


Are there no other solutions to send a small data package immediate?


Billy
Re: TServerSocket sends a unwanted acknowlege
#3472
Author: Hans Galema
Date: Wed, 21 Sep 2005 10:11
11 lines
1331 bytes
Billy Holliday wrote:

> i have to get about 30.000 data
> packed's

Is that client just sending 30.000 times at random moments ?

Or does it send a message where upon it waits for the server
to respond and then sends the next message?

Hans.
Re: TServerSocket sends a unwanted acknowlege
#3473
Author: Billy Holliday
Date: Wed, 21 Sep 2005 11:01
20 lines
1735 bytes
Hello Hans
>
> Is that client just sending 30.000 times at random moments ?
>
> Or does it send a message where upon it waits for the server
> to respond and then sends the next message?
>

That's it.

The Client is sending one message and waits for the server respond.
After this the next message is send.


sometimes it runnes whitout a problem.
Next time, two answers from the server are combined to one message,
next time i get a socket error 4 : the windows pc (TServerSocket) has  made
a push, with a wrong sequence-number. (why??)

I'm getting crasy......
Re: TServerSocket sends a unwanted acknowlege
#3474
Author: Hans Galema
Date: Wed, 21 Sep 2005 11:08
17 lines
1785 bytes
Billy Holliday wrote:

> The Client is sending one message and waits for the server respond.
> After this the next message is send.
> 
> sometimes it runnes whitout a problem.
> Next time, two answers from the server are combined to one message,

That then is not possible if both wait for each other.

> next time i get a socket error 4 : the windows pc (TServerSocket) has  made
> a push, with a wrong sequence-number. (why??)

Socket error 4 ? Where ? Does the client display that ? Never
saw a TServerSocket with that error number.

Hans.
Re: TServerSocket sends a unwanted acknowlege
#3475
Author: Billy Holliday
Date: Wed, 21 Sep 2005 11:39
44 lines
2406 bytes
Hi Hans.



> > next time i get a socket error 4 : the windows pc (TServerSocket) has
made
> > a push, with a wrong sequence-number. (why??)
>
> Socket error 4 ? Where ? Does the client display that ? Never
> saw a TServerSocket with that error number.
>

I see it on a debug memo and in the protokoll analyser.


void __fastcall TForm::ServerSocketClientError( TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode )
{
   Memo1->Lines->Add("Socket-Error at Port:" + IntToStr( Socket->RemotePort)
+ " Nr.: "+  IntToStr( (int) (ErrorEvent))    );
   ErrorCode= 0;
}


The TCP/IP-Stack is sending a automatic generated ACK ( 0 bytes data) as
reponse to the last client message.
Then the TServerSocket is sending  the really response (5 seconds later).
But he is doing this with:
the same sequence number,
the same ack number,
the same win number.

The client makes a correct response, but the TServerSocket rises a
Clienterror, because the sequence number is wrong.

Perhaps i should try the INDY TCP componente, but i don't know, how to get
the lenght of the received data message

:-(

So i don't know, how long is the message......


Thanks.
Re: TServerSocket sends a unwanted acknowlege
#3377
Author: Hans Galema
Date: Wed, 21 Sep 2005 12:28
37 lines
2704 bytes
Billy Holliday wrote:

>>Socket error 4 ? Where ? Does the client display that ? Never
>>saw a TServerSocket with that error number.
>>
> I see it on a debug memo and in the protokoll analyser.
> 
> void __fastcall TForm::ServerSocketClientError( TObject *Sender,
> TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode )
> {
>    Memo1->Lines->Add("Socket-Error at Port:" + IntToStr( Socket->RemotePort)
> + " Nr.: "+  IntToStr( (int) (ErrorEvent))    );
>    ErrorCode= 0;
> }

Aha you get a 4 because you cast a TErrorEvent to an int.
Change your code to use ErrorCode:

    Memo1->Lines->Add("Socket-Error at Port:" + IntToStr( Socket->RemotePort)
+ " Nr.: "+  IntToStr( ErrorCode) );

> The client makes a correct response, but the TServerSocket rises a
> Clienterror, because the sequence number is wrong.


Please tell what the real ErrorCode is.

> Perhaps i should try the INDY TCP componente, but i don't know, how to get
> the lenght of the received data message

You are aware that the code you posted for receiving and sending
is not full proof ? Better code has often been posted.
For instance you do not check the returnvalue of SendText().
Further it happens that ReceiveLength() is less than 1. You
are not checking for that scenario.

Hans.
Re: TServerSocket sends a unwanted acknowlege
#3376
Author: Hans Galema
Date: Wed, 21 Sep 2005 12:37
19 lines
1707 bytes
Billy Holliday wrote:

> ServerSocketClientRead( TObject *Sender, TCustomWinSocket *Socket )
> {
>     count = Socket->ReceiveLength ();  // checking how much bytes received
>     unsigned char   Buff[5000];  // make a big buffer
>     Socket->ReceiveBuf ( Buff, count );  // read the data
> 
>    // processing the data..

How big are the messages that come from the client ?
Are they that small that they come with only one readevent ?
Do you check that a whole message came in ?

As first statement add:

  if ( Socket->ReceiveLength() < 1 ) return;

Hans.
Re: TServerSocket sends a unwanted acknowlege
#3381
Author: Mark Guerrieri
Date: Wed, 21 Sep 2005 16:51
10 lines
1306 bytes
>     setsockopt(Socket, IPPROTO_TCP, TCP_NODELAY, &fl, sizeof(fl));

Should be

setsockopt(Socket->SocketHandle, IPPROTO_TCP, TCP_NODELAY, &fl, sizeof(fl));

TCustomWinSocket is a wrapper class around a Windows Socket.  The actual
socket handle is exposed via the SocketHandle property.

Mark
Re: TServerSocket sends a unwanted acknowlege
#3382
Author: Remy Lebeau (TeamB)
Date: Wed, 21 Sep 2005 16:52
26 lines
1668 bytes
"Billy Holliday" <NoSpam@Hohoho.com> wrote in message
news:433113a8$1@newsgroups.borland.com...

> How and where to set the TCP_NODELAY option.
>
> Is the OnClientConnect event the right place?

That is fine.

>     setsockopt(Socket, IPPROTO_TCP, TCP_NODELAY, &fl, sizeof(fl));
<snip>
> but the complier says: can't convert TCustomWinSocket to int!!

Correct, because you are passing the wrong thing to setsockopt().

> in winsock.h the first parameter is a type SOCKET, so i think
> Socket is right....

No, it is not.  You need to use its SocketHandle property instead:

    setsockopt((SOCKET)Socket->SocketHandle, IPPROTO_TCP, TCP_NODELAY, &fl,
sizeof(fl));


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads