Thread View: borland.public.cppbuilder.internet.socket
15 messages
15 total messages
Started by Joe Black
Tue, 25 Oct 2005 16:17
packet contatenation
Author: Joe Black
Date: Tue, 25 Oct 2005 16:17
Date: Tue, 25 Oct 2005 16:17
28 lines
1953 bytes
1953 bytes
Hi all, Using CPP Builder 6 update 4 Windows XP SP2 I have a client/server set of apps, the client uses a TClientSocket and the server the TServerSocket, all works great data sent back and forward fine, apart from one or two places. The problem seems to be when I send several packets straight after each other, the packets are all sent within a thread that activates every 35 ms and it sends one packet at a time. For some reason though several packets seem to get concatenated together to form one larger packet, if I slow down the transmit thread to say every 200ms this problems dissapears, this sort of time delay however is unacceptable. What I would like to know is: Is there anyway to determine whether the last packet was sent before I attempt to send the next one i.e. a buffer empty flag or check function, if the packet was not sent I can delay the transmit to the next activation of my thread and check again, thus curing the concatenation problem. Your thoughts would be appreciated. Thanks Joe
Re: packet contatenation
Author: Hans Galema
Date: Tue, 25 Oct 2005 16:32
Date: Tue, 25 Oct 2005 16:32
31 lines
2157 bytes
2157 bytes
Joe Black wrote: > .... The problem seems to be when I send > several packets straight after each other, the packets are all sent > within a thread that activates every 35 ms and it sends one packet at a > time. But what is the problem ? > For some reason though several packets seem to get concatenated together > to form one larger packet, if I slow down the transmit thread to say > every 200ms this problems dissapears, Yes that is normal. Winsock will send it's buffer when it is full or a certain amount of time has elapsed. > this sort of time delay however is > unacceptable. What I would like to know is: > > Is there anyway to determine whether the last packet was sent before I > attempt to send the next one Don't think so. But why would you? You have to program the receiving side in such a way that it can handle multiple 'packets' in one read event. Just as the receiving side should concatenate read-events until a complete (big) message is received. FYI: it is confusing that you use the word packet for the message that you send. TCP/IP uses packets already. Hans.
Re: packet contatenation
Author: Joe Black
Date: Tue, 25 Oct 2005 17:45
Date: Tue, 25 Oct 2005 17:45
54 lines
2763 bytes
2763 bytes
Hans Galema wrote: > Joe Black wrote: > >> .... The problem seems to be when I send several packets straight >> after each other, the packets are all sent within a thread that >> activates every 35 ms and it sends one packet at a time. > > > But what is the problem ? Several packets get concatenated together. > >> For some reason though several packets seem to get concatenated >> together to form one larger packet, if I slow down the transmit thread >> to say every 200ms this problems dissapears, > > > Yes that is normal. Winsock will send it's buffer when it is full or > a certain amount of time has elapsed. This is what I was wondering, I was hoping I could send data when it was empty. > >> this sort of time delay however is unacceptable. What I would like to >> know is: >> >> Is there anyway to determine whether the last packet was sent before I >> attempt to send the next one > > > Don't think so. But why would you? You have to program the receiving > side in such a way that it can handle multiple 'packets' in one read event. > Just as the receiving side should concatenate read-events until > a complete (big) message is received. I have a fairly simple app and I was hoping to make it easier, if this is the way to do it then I will code it to as you suggest. > > FYI: it is confusing that you use the word packet for the message > that you send. TCP/IP uses packets already. Appologies, the data I send is in a packet format, hence i use the term packet. I am fairly new to the Sockets side of things so my terminology may be confusing. > > Hans. Many thanks, you seem to have answered everything I needed to know Joe
Re: packet contatenation
Author: Eugene Mayevski
Date: Tue, 25 Oct 2005 18:52
Date: Tue, 25 Oct 2005 18:52
18 lines
1916 bytes
1916 bytes
Hello! You wrote on Tue, 25 Oct 2005 17:17:37 +0100: JB> I have a client/server set of apps, the client uses a TClientSocket and JB> the server the TServerSocket, all works great data sent back and JB> forward fine, apart from one or two places. The problem seems to be JB> when I send several packets straight after each other, the packets are JB> all sent within a thread that activates every 35 ms and it sends one JB> packet at a time. The problem is that TCP is not packet-oriented, but stream-oriented. If you call send() 2 times, this doesn't mean that there will be 2 TCP packets on the other end. There can be anything between 1 and 20 (or more) TCP packets. If you want message-based communication, you can use, for example, MsgConnect (http://www.eldos.com/msgconnect/) With best regards, Eugene Mayevski
Re: packet contatenation
Author: Remy Lebeau (TeamB)
Date: Tue, 25 Oct 2005 21:06
Date: Tue, 25 Oct 2005 21:06
54 lines
3097 bytes
3097 bytes
"Joe Black" <nospam@newsgroup.com> wrote in message news:435e5931$1@newsgroups.borland.com... > For some reason though several packets seem to get > concatenated together to form one larger packet That is perfectly normal socket behavior. A socket buffers all outgoing data until either the buffer is full or until the socket has no pending data waiting to be sent. > if I slow down the transmit thread to say every 200ms this > problems dissapears Yes, because you are allowing the socket to have some time to clear its internal buffer before more data is passed to it. > Is there anyway to determine whether the last packet was > sent before I attempt to send the next one No. But that is not needed in the first place. TCP is a stream transport. It doesn't care what the contents of the data is. It sends a stream of arbitrary bytes. So you are responsible for formatting your outgoing data in such a way that the receiver knows where one packet ends and the next begins. There are three ways to do that: 1) send messages that are all the same byte size. The receiver reads the necessary number of bytes and then processes them. 2) preceed each message with a fixed-sized length value. The receiver first reads the necessary number of bytes to receive the length and then continues reading the specified number of bytes to finish the message. 3) Place a unique terminator sequence of bytes at the end of a message. The terminator cannot appear in the message contents at all. This way, the receiver continues reading bytes until the terminator is encountered. In all three situations, you have to code your reading to handle three different scenerios when a read event occurs: 1) no bytes are available at all 2) fewer bytes are received than are needed for a complete message, requiring you to wait for another read event in order to receive more bytes for that message. 3) bytes from multiple messages are received at one time. In all read events, you should buffer the received bytes and then process and remove any complete messages that are in buffer, leaving any incomplete messages in the buffer for later processing. Gambit
Re: packet contatenation
Author: Joe Black
Date: Wed, 26 Oct 2005 08:05
Date: Wed, 26 Oct 2005 08:05
28 lines
2141 bytes
2141 bytes
Eugene Mayevski wrote: > Hello! > You wrote on Tue, 25 Oct 2005 17:17:37 +0100: > > JB> I have a client/server set of apps, the client uses a TClientSocket and > JB> the server the TServerSocket, all works great data sent back and > JB> forward fine, apart from one or two places. The problem seems to be > JB> when I send several packets straight after each other, the packets are > JB> all sent within a thread that activates every 35 ms and it sends one > JB> packet at a time. > > The problem is that TCP is not packet-oriented, but stream-oriented. If you > call send() 2 times, this doesn't mean that there will be 2 TCP packets on > the other end. There can be anything between 1 and 20 (or more) TCP packets. > If you want message-based communication, you can use, for example, > MsgConnect (http://www.eldos.com/msgconnect/) > > With best regards, > Eugene Mayevski > Thanks Eugene, I will have a look at the MsgConnect as you suggested, this app was meant to be a simple thing but the addition of sockets has baked my noodle a bit :-(. Cheers Joe
Re: packet contatenation
Author: Joe Black
Date: Wed, 26 Oct 2005 08:08
Date: Wed, 26 Oct 2005 08:08
74 lines
3644 bytes
3644 bytes
Remy Lebeau (TeamB) wrote: > "Joe Black" <nospam@newsgroup.com> wrote in message > news:435e5931$1@newsgroups.borland.com... > > >>For some reason though several packets seem to get >>concatenated together to form one larger packet > > > That is perfectly normal socket behavior. A socket buffers all outgoing > data until either the buffer is full or until the socket has no pending data > waiting to be sent. > > >>if I slow down the transmit thread to say every 200ms this >>problems dissapears > > > Yes, because you are allowing the socket to have some time to clear its > internal buffer before more data is passed to it. > > >>Is there anyway to determine whether the last packet was >>sent before I attempt to send the next one > > > No. But that is not needed in the first place. TCP is a stream transport. > It doesn't care what the contents of the data is. It sends a stream of > arbitrary bytes. So you are responsible for formatting your outgoing data > in such a way that the receiver knows where one packet ends and the next > begins. There are three ways to do that: > > 1) send messages that are all the same byte size. The receiver reads the > necessary number of bytes and then processes them. > > 2) preceed each message with a fixed-sized length value. The receiver first > reads the necessary number of bytes to receive the length and then continues > reading the specified number of bytes to finish the message. > > 3) Place a unique terminator sequence of bytes at the end of a message. The > terminator cannot appear in the message contents at all. This way, the > receiver continues reading bytes until the terminator is encountered. > > In all three situations, you have to code your reading to handle three > different scenerios when a read event occurs: > > 1) no bytes are available at all > > 2) fewer bytes are received than are needed for a complete message, > requiring you to wait for another read event in order to receive more bytes > for that message. > > 3) bytes from multiple messages are received at one time. > > In all read events, you should buffer the received bytes and then process > and remove any complete messages that are in buffer, leaving any incomplete > messages in the buffer for later processing. > > > Gambit > > Hi Remy, Many thanks for your response, it covers all the areas I'm having the trouble with, I had an feeling I was going to have to put the data back together after I recieved it but I couldn't find the right sort of information about it, my googleing skills must be lacking at the moment :-). Thanks again Joe
Re: packet contatenation
Author: Eugene Mayevski
Date: Wed, 26 Oct 2005 09:00
Date: Wed, 26 Oct 2005 09:00
12 lines
1565 bytes
1565 bytes
Hello! You wrote on Wed, 26 Oct 2005 09:05:05 +0100: JB> I will have a look at the MsgConnect as you suggested, this app was JB> meant to be a simple thing but the addition of sockets has baked my JB> noodle a bit :-(. Yes, this is the place where the thing like MsgConnect can help, since it removes complexities of socket-related stuff from your project. With best regards, Eugene Mayevski
Re: packet contatenation
Author: Bob Gonder
Date: Wed, 26 Oct 2005 15:30
Date: Wed, 26 Oct 2005 15:30
11 lines
1523 bytes
1523 bytes
In addition to Remys excelent suggestions, if you are having a timing problem on the receiving end, that is, if you need the 35 ms to process the data, but when the data is concatinated, you run out of processing time..... Then take a look at the setsockopt( ) function, and the TCP_NODELAY setting. This turns off buffering for that socket. This makes the socket less efficient in terms of bandwidth, but more responsive to real-world events.
Re: packet contatenation
Author: Alfred
Date: Wed, 26 Oct 2005 17:44
Date: Wed, 26 Oct 2005 17:44
6 lines
1114 bytes
1114 bytes
Great post , it helped me too... Data concatenation is only for Socket->SendBuf(...) or Socket->SendText(...) or both ? Thank you Alfred
Re: packet contatenation
Author: Michel Leunen
Date: Wed, 26 Oct 2005 18:12
Date: Wed, 26 Oct 2005 18:12
15 lines
1534 bytes
1534 bytes
Alfred wrote: > Data concatenation is only for Socket->SendBuf(...) or Socket->SendText(...) or both ? Both of course. The socket doesn't know if you're sending text or random data. Michel -- ---------------------------------------- Michel Leunen mailto: see my homepage. C++Builder, BCC5.5.1 Web site: http://www.leunen.com/ ----------------------------------------
Re: packet contatenation
Author: Alfred
Date: Wed, 26 Oct 2005 18:21
Date: Wed, 26 Oct 2005 18:21
8 lines
1249 bytes
1249 bytes
>Both of course. The socket doesn't know if you're sending text or random >data. LOL I am very luchy then.. I got no problem with sendtext till now.. Ok I will modify all my code then.. thank you very much Alfred
Re: packet contatenation
Author: Remy Lebeau (TeamB)
Date: Wed, 26 Oct 2005 23:00
Date: Wed, 26 Oct 2005 23:00
11 lines
1294 bytes
1294 bytes
"Alfred" <alf@no.spam.not.valied.email> wrote in message news:435fc087$1@newsgroups.borland.com... > Data concatenation is only for Socket->SendBuf(...) or Socket->SendText(...) or both ? Both. SendText() passes the AnsiString to SendBuf(). Gambit
Re: packet contatenation
Author: Alfred
Date: Thu, 27 Oct 2005 16:02
Date: Thu, 27 Oct 2005 16:02
7 lines
1135 bytes
1135 bytes
>Both. SendText() passes the AnsiString to SendBuf(). Ah thank you.. indy have the same problem ? Thank you very much alfred
Re: packet contatenation
Author: Remy Lebeau (TeamB)
Date: Thu, 27 Oct 2005 16:45
Date: Thu, 27 Oct 2005 16:45
13 lines
1466 bytes
1466 bytes
"Alfred" <alf@no.spam.not.valied.email> wrote in message news:4360fa19$1@newsgroups.borland.com... > indy have the same problem ? No. Indy hides the concatenation and splitting of packets from you transparently. When you read or send something, Indy does not return control to your code until the data has been read/sent in full, or until an actual error occurs. 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