Board index » cppbuilder » TClientSocket And TServer Socket... Timing

TClientSocket And TServer Socket... Timing


2004-02-05 05:03:33 PM
cppbuilder110
Hello everybody. I am making a simple program to send some kind of data
through TCP/IP protocol, using the TClientSocket and TServerSocket provided
for C++ Builder. The program works, but I have a time problem:
I try to send a data of 70 bytes (approx.) each 10 milliseconds, but the
faster message come from cleint to server in 200 milliseconds. Im working
in a intranet of 100 mb/s. In addition, not only the messages are slow,
also im lose them. So what can i do? or better... what im doing wrong?.
Should I to create a queue to send and to receive?? I trhead?.
Can somebody tell me where can i find information about this? i need at less
receive messages every 10 milliseconds.
Thank you very much in advance
 
 

Re:TClientSocket And TServer Socket... Timing

"McKool" < XXXX@XXXXX.COM >wrote in message
Quote
I try to send a data of 70 bytes (approx.) each 10 milliseconds,
but the faster message come from cleint to server in 200 milliseconds.
Windows is not a real-time OS. There are going to be delays.
Also, are you using the sockets in blocking or non-blocking mode?
Non-blocking is must slower as it relies on the message queue, and thus is
effected by the number of messages the application receives and ow fast it
can process them.
Quote
In addition, not only the messages are slow, also im lose them.
You are most likely not managing the sockets correctly. Please show your
actual code.
Gambit
 

Re:TClientSocket And TServer Socket... Timing

Ok, first i made a mistake in the reception of the messages, for that reason
i was not receiving all the packages. It was something really stupid
becasue i was reading with Socket->ReceiveBuf() but all the data, i mean,
receiving all, using the function Socket->ReceiveLength() and i was
procesing only the first message of that buffer.
Im using a Non Blocking connection. . After repair my mistake in the
messages reception, the speed is very good only when I send messages of 1
millisecond comes the messages in the queue. Here is the code for the
reception and this occurs every time that in the socket a receive event
happen
pTByte Buffer;
Buffer = (pTByte)new TByte[sizeof(TCANMsg)];
if (!Buffer)
return false;
try {
if (Socket->ReceiveBuf((void *)Buffer, sizeof(TCANMsg)) <= 0) {
delete[] Buffer;
return false;
}
}
catch (...) {
return false;
}
try{
memcpy(&Message, Buffer, sizeof(TCANMsg));
}
catch(...){
return false;
}
return true;
TCANMsg is
tipedef struct{
DWORD ID;
BYTE MSGTYPE;
BYTE LEN;
BYTE DATA[8];
}TCANMsg;
So like i said, was a stupid mistake. Thank you for your answer.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im
Newsbeitrag news:40229967$ XXXX@XXXXX.COM ...
Quote

"McKool" < XXXX@XXXXX.COM >wrote in message
news:4022052c$ XXXX@XXXXX.COM ...

>I try to send a data of 70 bytes (approx.) each 10 milliseconds,
>but the faster message come from cleint to server in 200 milliseconds.

Windows is not a real-time OS. There are going to be delays.

Also, are you using the sockets in blocking or non-blocking mode?
Non-blocking is must slower as it relies on the message queue, and thus is
effected by the number of messages the application receives and ow fast it
can process them.

>In addition, not only the messages are slow, also im lose them.

You are most likely not managing the sockets correctly. Please show your
actual code.


Gambit


 

{smallsort}

Re:TClientSocket And TServer Socket... Timing

McKool wrote:
Quote
Here is the code for the
reception and this occurs every time that in the socket a receive event
happen
Is that all your code in the event ?
What I miss is i.e. if ( Socket->ReceiveLength() <= 0 ) return;
Quote
pTByte Buffer;

Buffer = (pTByte)new TByte[sizeof(TCANMsg)];
You do not need to new a buffer for every call. You can
receive in Message directly.
Quote
try {
if (Socket->ReceiveBuf((void *)Buffer, sizeof(TCANMsg)) <= 0) {
<= 0 will not happen if you checked ReceiveLength before.
What can happen though is that ReceiveBuf() returns a value less
then sizeof(TCANMsg). Possible if you sended many, many
messages at once, and the last one went over the winsockbufferborder.
You have no code for that.
Quote
delete[] Buffer;
Are you also deleting in the otehr cases ?
Quote
return false;
Heh, returning false in OnRead() ?
Hans.
 

Re:TClientSocket And TServer Socket... Timing

Thanks for your points.
Quote
Is that all your code in the event ?

What I miss is i.e. if ( Socket->ReceiveLength() <= 0 ) return;
No, is not all. This is a function inside a class and is called only when
exist data.
Quote
>pTByte Buffer;
>
>Buffer = (pTByte)new TByte[sizeof(TCANMsg)];

You do not need to new a buffer for every call. You can
receive in Message directly.
Your right, but in this moment I am changind my class, now i have to pass
information inside the package that i dont know the length at the moment to
receive the package, for that reason the buffer will change of length. And
the Message structure wil be have pointers that I will create in the moemnt
of the reception.
Quote

>try {
>if (Socket->ReceiveBuf((void *)Buffer, sizeof(TCANMsg)) <= 0) {

<= 0 will not happen if you checked ReceiveLength before.

Here your right, i only put that to prevent maybe memory errors.
Quote
What can happen though is that ReceiveBuf() returns a value less
then sizeof(TCANMsg). Possible if you sended many, many
messages at once, and the last one went over the winsockbufferborder.
You have no code for that.

>delete[] Buffer;

If a message is not complete, simple i dont take it in count because this
message will be replay to other program.
Quote
Are you also deleting in the otehr cases ?

>return false;

Heh, returning false in OnRead() ?
Is also for the message, if the message has an error, is like read
nothing for me.
even this code would be so:
int i = sizeof(TCANMsg);
if(Socket->ReceiveBuf(&Message,i) != i) return false;
else return true;
I still have many things to debug, but this code is inside a class..... the
outside code is this:
void __fastcall TfTcpCan::wskClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
clsPack MiPack;
int L = (Socket ? Socket->ReceiveLength() : 0);
TCANMsg MsgTemp;
AnsiString strTemp;
if (!Socket || !L)
return;
if(L < sizeof(TCANMsg))
return;
if(MiPack.Receive(Socket)){
MsgTemp = MiPack.ReturnData();
strTemp = "ID: " + IntToStr(MsgTemp.ID) + ". ";
strTemp = strTemp + "Length: " + IntToStr(MsgTemp.LEN) + ". ";
strTemp = strTemp + "DATA: ";
for(int i=0;i<MsgTemp.LEN;i++)
strTemp = strTemp + IntToStr(MsgTemp.DATA[i]) + " - ";
ListBox1->Items->Add("Remote: " + strTemp);
}
if(bCanConnected)
SendToLocalCAN(MsgTemp);
}
 

Re:TClientSocket And TServer Socket... Timing

Cut and Paste fail ;-)
void __fastcall TfTcpCan::wskClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
clsPack MiPack;
int L = (Socket ? Socket->ReceiveLength() : 0);
TCANMsg MsgTemp;
AnsiString strTemp;
if (!Socket || !L)
return;
if(L < sizeof(TCANMsg))
return;
if(MiPack.Receive(Socket)){
MsgTemp = MiPack.ReturnData();
strTemp = "ID: " + IntToStr(MsgTemp.ID) + ". ";
strTemp = strTemp + "Length: " + IntToStr(MsgTemp.LEN) + ". ";
strTemp = strTemp + "DATA: ";
for(int i=0;i<MsgTemp.LEN;i++)
strTemp = strTemp + IntToStr(MsgTemp.DATA[i]) + " - ";
ListBox1->Items->Add("Remote: " + strTemp);
if(bCanConnected)
SendToLocalCAN(MsgTemp);
}
}
 

Re:TClientSocket And TServer Socket... Timing

McKool wrote:
Quote
Cut and Paste fail ;-)
Some remarks:
void __fastcall TfTcpCan::wskClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
if ( Socket->ReceiveLength() <= 0 )
return; // this you should do always
if( Socket->ReceiveLength() < sizeof(TCANMsg))
return; // this only in your special protocol
clsPack MiPack = {0};
if(MiPack.Receive(Socket))
{
TCANMsg MsgTemp = MiPack.ReturnData();
AnsiString strTemp = "ID: " + IntToStr(MsgTemp.ID) + ". ";
// .. only declare variables where you use them
.....
}
}
Hans.
 

Re:TClientSocket And TServer Socket... Timing

"McKool" < XXXX@XXXXX.COM >wrote in message
Quote
i was reading with Socket->ReceiveBuf() but all the data, i mean,
receiving all, using the function Socket->ReceiveLength() and i
was procesing only the first message of that buffer.
Yes, you need to be careful when using ReceiveLength() like that. If
possible, it would be better to prepend each packet with its own header that
specifies the exact size of the packet. Then you always know exactly how
much to read. You also need to be careful with incomplete packets.
ReceiveLength() only tells you how much data is available, but that is no
guarantee that complete packets are available. You may have partial packets
until more data arrives. You should be buffering the socket data and then
processing the buffer when you know you have enough data onhand for
processing complete packets.
Quote
Im using a Non Blocking connection.
What I described above applies doubly then. The OnRead event is triggered
whenever data is received, even just 1 byte, so you don't know yet whether
you have complete packets or not, so buffering is even more important.
Quote
pTByte Buffer;

Buffer = (pTByte)new TByte[sizeof(TCANMsg)];
Why are you dynamically allocating a fixed size? It would be better to
declare it statically instead:
TByte Buffer[sizeof(TCANMsg)];
Or better yet, just declare a TCANMsg and read into it directly:
TCANMsg Msg;
Socket->ReceiveBuf(&Msg, sizeof(TCANMsg))
Quote
if (Socket->ReceiveBuf((void *)Buffer, sizeof(TCANMsg)) <= 0) {
delete[] Buffer;
return false;
You just corrupted your data stream if ReceiveBuf() returns fewer then a
full TCANMsg. There is no guarantee that when OnRead is triggered,
sizeof(TCANMsg) amount of bytes will actually be available. It is very
possible (and likely) for packets to be split amongst several OnRead event
triggerings. Thus you must buffer your data so that it is still available
the next time the event is triggered so that you can finish any partial
packets packet and then process everything correctly. If you return
prematurely like you are, then you lose your positioning in the socket
stream and you can't guarantee that you will ever be able to read any
packets correctly ever again until you close and reset the socket.
Try this instead:
// create this before you connect the socket,
// and free it after you close the socket
TMemoryStream *DataBuffer = new TMemoryStream;
//...
// see how many bytes are available
int Available = Socket->ReceiveLength();
if( Available>0 )
{
// resize the buffer to store the new bytes
int CurrentSize = DataBuffer->Size;
DataBuffer->Size = (CurrentSize + Available);
// read the new bytes into the buffer, preserving
// any bytes that have not been processed yet
void* ptr = (((BYTE*)(DataBuffer->Memory)) + CurrentSize);
if( Socket->ReceiveBuf(ptr, Available) < Available )
// error ...
// read the buffer until all full packets have been processed
while( (DataBuffer->Position + sizeof(TCANMsg)) <=
DataBuffer->Size )
{
TCANMsg Msg;
DataBuffer->Read(&Msg, sizeof(TCANMsg));
// process Msg as needed.
}
// remove any bytes from memory that have already been processed
// and preserve just the partial packets that are still awaiting
more data
int DataSize = (DataBuffer->Size - DataBuffer->Position);
if( DataSize>0 )
{
MoveMemory(DataBuffer->Memory, ((BYTE*)(DataBuffer->Memory)) +
DataBuffer->Position, DataSize);
DataBuffer->Size = DataSize;
}
else
DataBuffer->Clear();
}
Gambit
 

Re:TClientSocket And TServer Socket... Timing

"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
<= 0 will not happen if you checked ReceiveLength before.
It might, if an error occurs in the socket after ReceiveLength() is called
but before ReceiveBuf() is called.
Quote
What can happen though is that ReceiveBuf() returns a value
less then sizeof(TCANMsg).
If ReceiveLength() returns a value greater than 0, then ReceiveBuf() is
guaranteed to return at least that many bytes in a single call, as long as
the socket itself is still valid.
Gambit
 

Re:TClientSocket And TServer Socket... Timing

Thank you Gambit. Can you tell me where can i find information (general
information) about sending package using TCP/IP and UDP, the different and
how to implement UDP in builder ??? or is what i already use?? I want to
make this small project first to run in a LAN, but in the future i will try
to make it using internet.
Thanks.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im
Newsbeitrag news:4023eacd$ XXXX@XXXXX.COM ...
Quote

"McKool" < XXXX@XXXXX.COM >wrote in message
news:40235120$ XXXX@XXXXX.COM ...

>i was reading with Socket->ReceiveBuf() but all the data, i mean,
>receiving all, using the function Socket->ReceiveLength() and i
>was procesing only the first message of that buffer.

Yes, you need to be careful when using ReceiveLength() like that. If
possible, it would be better to prepend each packet with its own header
that
specifies the exact size of the packet. Then you always know exactly how
much to read. You also need to be careful with incomplete packets.
ReceiveLength() only tells you how much data is available, but that is no
guarantee that complete packets are available. You may have partial
packets
until more data arrives. You should be buffering the socket data and then
processing the buffer when you know you have enough data onhand for
processing complete packets.

>Im using a Non Blocking connection.

What I described above applies doubly then. The OnRead event is triggered
whenever data is received, even just 1 byte, so you don't know yet whether
you have complete packets or not, so buffering is even more important.

>pTByte Buffer;
>
>Buffer = (pTByte)new TByte[sizeof(TCANMsg)];

Why are you dynamically allocating a fixed size? It would be better to
declare it statically instead:

TByte Buffer[sizeof(TCANMsg)];

Or better yet, just declare a TCANMsg and read into it directly:

TCANMsg Msg;
Socket->ReceiveBuf(&Msg, sizeof(TCANMsg))

>if (Socket->ReceiveBuf((void *)Buffer, sizeof(TCANMsg)) <= 0) {
>delete[] Buffer;
>return false;

You just corrupted your data stream if ReceiveBuf() returns fewer then a
full TCANMsg. There is no guarantee that when OnRead is triggered,
sizeof(TCANMsg) amount of bytes will actually be available. It is very
possible (and likely) for packets to be split amongst several OnRead event
triggerings. Thus you must buffer your data so that it is still available
the next time the event is triggered so that you can finish any partial
packets packet and then process everything correctly. If you return
prematurely like you are, then you lose your positioning in the socket
stream and you can't guarantee that you will ever be able to read any
packets correctly ever again until you close and reset the socket.

Try this instead:

// create this before you connect the socket,
// and free it after you close the socket
TMemoryStream *DataBuffer = new TMemoryStream;

//...

// see how many bytes are available
int Available = Socket->ReceiveLength();
if( Available>0 )
{
// resize the buffer to store the new bytes
int CurrentSize = DataBuffer->Size;
DataBuffer->Size = (CurrentSize + Available);

// read the new bytes into the buffer, preserving
// any bytes that have not been processed yet
void* ptr = (((BYTE*)(DataBuffer->Memory)) + CurrentSize);
if( Socket->ReceiveBuf(ptr, Available) < Available )
// error ...

// read the buffer until all full packets have been processed
while( (DataBuffer->Position + sizeof(TCANMsg)) <=
DataBuffer->Size )
{
TCANMsg Msg;
DataBuffer->Read(&Msg, sizeof(TCANMsg));
// process Msg as needed.
}

// remove any bytes from memory that have already been processed
// and preserve just the partial packets that are still awaiting
more data
int DataSize = (DataBuffer->Size - DataBuffer->Position);
if( DataSize>0 )
{
MoveMemory(DataBuffer->Memory, ((BYTE*)(DataBuffer->Memory)) +
DataBuffer->Position, DataSize);
DataBuffer->Size = DataSize;
}
else
DataBuffer->Clear();
}


Gambit


 

Re:TClientSocket And TServer Socket... Timing

Thanks again. Im trying this code and it works ok. But now i have others
question (with this code i can enlarge a little what i was trying to do).
1- I woul like to know if exist any way to know when my program is runing
(like a server) in a computer from the LAN. I want to connect clients
automatically (if it is possible). The only way that i know is set my
client with a port number, and start to open the port with all the machines
in the net until a connection is made or all the machines were tested, but
that is too sllow because i have to wait for each time out.
2- What if i dont know the port used by the server application? can i at
least, to know if the application is runing?
3- If i want to send something from a client to other client, should i send
first to the server and this must make a replay of the message?.
(maybe you can tell me a link if you diont have time ;-), but really,
thanks )
Thanks - .
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im
Newsbeitrag news:4023eacd$ XXXX@XXXXX.COM ...
Quote

"McKool" < XXXX@XXXXX.COM >wrote in message
news:40235120$ XXXX@XXXXX.COM ...

>i was reading with Socket->ReceiveBuf() but all the data, i mean,
>receiving all, using the function Socket->ReceiveLength() and i
>was procesing only the first message of that buffer.

Yes, you need to be careful when using ReceiveLength() like that. If
possible, it would be better to prepend each packet with its own header
that
specifies the exact size of the packet. Then you always know exactly how
much to read. You also need to be careful with incomplete packets.
ReceiveLength() only tells you how much data is available, but that is no
guarantee that complete packets are available. You may have partial
packets
until more data arrives. You should be buffering the socket data and then
processing the buffer when you know you have enough data onhand for
processing complete packets.

>Im using a Non Blocking connection.

What I described above applies doubly then. The OnRead event is triggered
whenever data is received, even just 1 byte, so you don't know yet whether
you have complete packets or not, so buffering is even more important.

>pTByte Buffer;
>
>Buffer = (pTByte)new TByte[sizeof(TCANMsg)];

Why are you dynamically allocating a fixed size? It would be better to
declare it statically instead:

TByte Buffer[sizeof(TCANMsg)];

Or better yet, just declare a TCANMsg and read into it directly:

TCANMsg Msg;
Socket->ReceiveBuf(&Msg, sizeof(TCANMsg))

>if (Socket->ReceiveBuf((void *)Buffer, sizeof(TCANMsg)) <= 0) {
>delete[] Buffer;
>return false;

You just corrupted your data stream if ReceiveBuf() returns fewer then a
full TCANMsg. There is no guarantee that when OnRead is triggered,
sizeof(TCANMsg) amount of bytes will actually be available. It is very
possible (and likely) for packets to be split amongst several OnRead event
triggerings. Thus you must buffer your data so that it is still available
the next time the event is triggered so that you can finish any partial
packets packet and then process everything correctly. If you return
prematurely like you are, then you lose your positioning in the socket
stream and you can't guarantee that you will ever be able to read any
packets correctly ever again until you close and reset the socket.

Try this instead:

// create this before you connect the socket,
// and free it after you close the socket
TMemoryStream *DataBuffer = new TMemoryStream;

//...

// see how many bytes are available
int Available = Socket->ReceiveLength();
if( Available>0 )
{
// resize the buffer to store the new bytes
int CurrentSize = DataBuffer->Size;
DataBuffer->Size = (CurrentSize + Available);

// read the new bytes into the buffer, preserving
// any bytes that have not been processed yet
void* ptr = (((BYTE*)(DataBuffer->Memory)) + CurrentSize);
if( Socket->ReceiveBuf(ptr, Available) < Available )
// error ...

// read the buffer until all full packets have been processed
while( (DataBuffer->Position + sizeof(TCANMsg)) <=
DataBuffer->Size )
{
TCANMsg Msg;
DataBuffer->Read(&Msg, sizeof(TCANMsg));
// process Msg as needed.
}

// remove any bytes from memory that have already been processed
// and preserve just the partial packets that are still awaiting
more data
int DataSize = (DataBuffer->Size - DataBuffer->Position);
if( DataSize>0 )
{
MoveMemory(DataBuffer->Memory, ((BYTE*)(DataBuffer->Memory)) +
DataBuffer->Position, DataSize);
DataBuffer->Size = DataSize;
}
else
DataBuffer->Clear();
}


Gambit


 

Re:TClientSocket And TServer Socket... Timing

"McKool" < XXXX@XXXXX.COM >wrote in message
Quote
The only way that i know is set my client with a port number,
and start to open the port with all the machines in the net until
a connection is made or all the machines were tested
If you are using TCP, then that is exactly what you have to do.
Otherwise, you will need to use UDP to broadcast a message to the network,
and have each server respond when it receives the message so that the client
can see which servers are available. Note, though, that if your network has
any routers or switches, your UDP broadcast may not reach the entire
network, only your particular subnet of it.
Quote
but that is too sllow because i have to wait for each time out.
Make your timeouts small.
Quote
2- What if i dont know the port used by the server application?
Then you are out of luck. You must know the exact port number in order to
connect to it.
Quote
can i at least, to know if the application is runing?
Not if it is on a remote machine, no.
Quote
3- If i want to send something from a client to other client, should
i send first to the server and this must make a replay of the message?.
That is one way to do it. The other way is for one of the clients to open
its own server socket and send that socket info to the other client (such as
through the main server relay) so that the connecting client can connect to
the listening client directly.
Gambit
 

Re:TClientSocket And TServer Socket... Timing

-OK, then i'm walking in the right direction. But let me understand
something .... I'm using the TCLIENTSOCKET and TSERVERSOCKET. That is mean,
i'm using TCP/IP right?. So i suppose that i can not use UDP Protocol with
this sockets, do i have to make my own sockets?
-In all the reading about TCP that i have, i can read: TCP verify that the
packages arrive to their destinies. That is mean that when i have the
length of my message in the receive buffer ... can i be sure that the data
is correct (i mean, no trash, so i sent it, i received it)???
-To Send messages with a "header" to know the length of the messages... Is
something like this that i made? and in the other side do i have to read
always the header and after that the data right?:
struct TPackage {
int Bytes; // Length in bytes of the data below
BYTE *Dt; // Data (TCANMsg) by example
};
TPackage MyPack;
MyPack->Dt = new BYTE[sizeof(TCANMsg)];
MyPack->Bytes = sizeof(TCANMsg);
int n = sizeof(TPackage) + MyPack->Bytes;
BYTE *Buffer = (BYTE*)new BYTE[n];
memcpy(Buffer, MyPack, sizeof(TPackage));
memcpy(Buffer+sizeof(TPackage), MyPack->Dt, MyPack->Nb);
Socket->SendBuf(Buffer, n);
Thank you for the help.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im
Newsbeitrag news:40293f17$ XXXX@XXXXX.COM ...
Quote

"McKool" < XXXX@XXXXX.COM >wrote in message
news:4028c0a9$ XXXX@XXXXX.COM ...

>The only way that i know is set my client with a port number,
>and start to open the port with all the machines in the net until
>a connection is made or all the machines were tested

If you are using TCP, then that is exactly what you have to do.

Otherwise, you will need to use UDP to broadcast a message to the network,
and have each server respond when it receives the message so that the
client
can see which servers are available. Note, though, that if your network
has
any routers or switches, your UDP broadcast may not reach the entire
network, only your particular subnet of it.

>but that is too sllow because i have to wait for each time out.

Make your timeouts small.

>2- What if i dont know the port used by the server application?

Then you are out of luck. You must know the exact port number in order to
connect to it.

>can i at least, to know if the application is runing?

Not if it is on a remote machine, no.

>3- If i want to send something from a client to other client, should
>i send first to the server and this must make a replay of the message?.

That is one way to do it. The other way is for one of the clients to open
its own server socket and send that socket info to the other client (such
as
through the main server relay) so that the connecting client can connect
to
the listening client directly.


Gambit


 

Re:TClientSocket And TServer Socket... Timing

"McKool" < XXXX@XXXXX.COM >wrote in message
Quote
I'm using the TCLIENTSOCKET and TSERVERSOCKET.
That is mean, i'm using TCP/IP right?
Yes.
Quote
So i suppose that i can not use UDP Protocol with this sockets
No.
Quote
do i have to make my own sockets?
That, or find some UDP-oriented components.
Quote
-In all the reading about TCP that i have, i can read: TCP verify
that the packages arrive to their destinies. That is mean that when
i have the length of my message in the receive buffer ... can i be sure
that the data is correct (i mean, no trash, so i sent it, i received
it)???
You will receive it, and most of the time it will be fine. But it is your
own responsibility to make sure that your data wasn't corrupted during
tranport. If the sender had multiple threads writing to the socket at the
same time, or there is a router/proxy/spy in the middle of the connection
between the sender and receiver, are a couple of possibilities.
Quote
-To Send messages with a "header" to know the length of the
messages... Is something like this that i made?
What you have is very basic, but you have the general idea. Your
implementation is a bit rough, though, and you are using more pointers then
you really need. Given the code you have shown, you don't even need the
structure at all, just use the TCANMsg directly, ie:
--- sender ---
TCANMsg Msg;
// fill in Msg as needed...
int size = sizeof(TCANMsg);
Socket->SendBuf(&size, sizeof(int));
Socket->SendBuf(&Msg, size);
--- receiver ---
int size = 0;
Socket->ReceiveBuf(&size, sizeof(int));
TCANMsg* Msg = (TCANMsg*) malloc(size);
Socket->ReceiveBuf(Msg, size);
// use Msg as needed...
free(Msg);
A more complete implementation is more involved than that, but that is the
basic logic without all the extras like buffering, handling partial packets,
etc.
Quote
and in the other side do i have to read always the header and after
that the data right?:
Of course.
Gambit
 

Re:TClientSocket And TServer Socket... Timing

Gabit i have a problem. Everything is going but there is a time (when i
sending too much messagesand and too fast) htat the program crash, but
without Error messages, is terminated by windows. What can be that? the
buffer maybe? what can i do? send and receive with thread?
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im
Newsbeitrag news:402a003c$ XXXX@XXXXX.COM ...
Quote

"McKool" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>I'm using the TCLIENTSOCKET and TSERVERSOCKET.
>That is mean, i'm using TCP/IP right?

Yes.

>So i suppose that i can not use UDP Protocol with this sockets

No.

>do i have to make my own sockets?

That, or find some UDP-oriented components.

>-In all the reading about TCP that i have, i can read: TCP verify
>that the packages arrive to their destinies. That is mean that when
>i have the length of my message in the receive buffer ... can i be sure
>that the data is correct (i mean, no trash, so i sent it, i received
it)???

You will receive it, and most of the time it will be fine. But it is your
own responsibility to make sure that your data wasn't corrupted during
tranport. If the sender had multiple threads writing to the socket at the
same time, or there is a router/proxy/spy in the middle of the connection
between the sender and receiver, are a couple of possibilities.

>-To Send messages with a "header" to know the length of the
>messages... Is something like this that i made?

What you have is very basic, but you have the general idea. Your
implementation is a bit rough, though, and you are using more pointers
then
you really need. Given the code you have shown, you don't even need the
structure at all, just use the TCANMsg directly, ie:

--- sender ---

TCANMsg Msg;
// fill in Msg as needed...
int size = sizeof(TCANMsg);
Socket->SendBuf(&size, sizeof(int));
Socket->SendBuf(&Msg, size);


--- receiver ---

int size = 0;
Socket->ReceiveBuf(&size, sizeof(int));
TCANMsg* Msg = (TCANMsg*) malloc(size);
Socket->ReceiveBuf(Msg, size);
// use Msg as needed...
free(Msg);

A more complete implementation is more involved than that, but that is the
basic logic without all the extras like buffering, handling partial
packets,
etc.

>and in the other side do i have to read always the header and after
>that the data right?:

Of course.


Gambit