Solved

Read Packet by Packet

Posted on 2008-10-28
10
1,092 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 25 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now