Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read Packet by Packet

Posted on 2008-10-28
10
Medium Priority
?
1,114 Views
Last Modified: 2008-10-29
Hi

I have a TCP Server which is listening for incoming requets (Simple string format).

Trouble is if a client sends two packets and Servers executes a recv, both the packets come in same recv()

How can i read packet by packet.
0
Comment
Question by:carchitect
  • 4
  • 4
  • 2
10 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22829273
>>>> How can i read packet by packet.
If the packets have a fixed size, you could provide a buffer which takes exactly one packet but not more.

If the packets have variable size, the clients should add the length to their buffers, e, g. by using a struct like

   struct Packet
   {
        unsigned int     siz : 32;
        unsigned char  buf[MAX_BUF_SIZ];
   };

so that you easily could parse the received buffer for multiple packets.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22829278
>> How can i read packet by packet.

Not on recv level. You need to go on a lower level.

In fact, how do you know that the two sends were sent in different packets ?
0
 
LVL 6

Author Comment

by:carchitect
ID: 22829287
By running ethereal on server, i came to know that the packets received are two but when i make recv() call, i get the full data at one go.

Pls suggest

I would like to go for lower level if needed
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 22829322
>> By running ethereal on server, i came to know that the packets received are two but when i make recv() call, i get the full data at one go.

What I meant is : which data is put into which packets is decided on a lower level (not on application level). On the receiving end, the same is true : when the data received from packets is available to the application depends on the lower protocol stack levels.
Do not let your code rely on such things.

It shouldn't be a problem anyway ... Your code should simply receive data until it has read (at least) one message, then process the message (and leave the extra data in the buffer). After the message is processed, it checks the buffer - if there's another full message in the buffer, it processes that one - if not, it does another recv to receive more data. Etc.


>> I would like to go for lower level if needed

You don't need to :)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22829355
Note, if clients and server were on different platforms, you would need to check whether a struct like the above has the same alignment and size on eaxch of the platforms. To support platforms of different endian types the clients would need to assign the length in network order format by

     Packet p = { 0 };
     p.siz = htonl(strlen(message)+1);
     memcpy(p.buf, message, strlen(message)+1);
0
 
LVL 6

Author Comment

by:carchitect
ID: 22829366
Requirement is client can send various kinds of XML requests e.g. Login, Request etc
Every command has got different argument.
In addition, i need to implement one HeartBeat message between client and server (Necessity).

Now if i have sent one HB from Server to client, i am expecting one HB Reply but it can come along with other requests also like Login, Request etc.

I need to divide all before processing where as i go on packet level and sniff, all are coming as different packets only.

So i was wondering if there is any way to read packet by packet rather than by buffer.

Sequence of incoming packets can be anything. HB can come before Login and vice-versa.

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 100 total points
ID: 22829482
>> So i was wondering if there is any way to read packet by packet rather than by buffer.

As I said : you shouldn't. You can simply process the buffer that contains the incoming data. Process the first message, then the second message, etc. When the buffer becomes empty, you recv some more.

That's the way all such applications work, and that's the way the TCP/IP protocol stack has been designed. The application level should not worry about packets - the lower level handle all of that, and provide a nice stream of data to the application.


One thought though : how come the client can send more requests, before the previous one has been processed ? If there's a login request, shouldn't the client wait until the login was approved by the server before sending more requests ?
Also, if the client sends multiple requests without waiting for the response, how will it know which responses were for which requests when they arrive ?
0
 
LVL 6

Author Comment

by:carchitect
ID: 22829568
Thank you for showing such an interest.

1. Client can send more than one requests at a time as there is a requirement of Heart Beat Message from client to server and vice-versa at every defined time interval which is not related to other requests in progress.

2. Every request is coming with a Sequence Number from cleint and server responds back with that sequence number as one of the fields of the reply.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22829627
A heartbeat message should only be sent in periods where there are no requests, no ? When requests are being sent, there's no need for checking the connection, since that's already done by the actual requests being sent.

In any case, that doesn't change what I said earlier. If different requests can arrive before responses are sent, then the server simply processes those requests one by one, as they come in.

You need to make sure that it's easy to discern the boundaries between the different packets, but with XML, that shouldn't be a problem.
0
 
LVL 6

Author Comment

by:carchitect
ID: 22829667
Heart beat is coming from a 3rd party on which i have no control. I have to support it. Either server should send or client will send, irrespective of requests. (No other option to ignore it).

I will continue with the way you suggested. Reading buffer and diving it into various requests if it includes.
 
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question