473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Racing the Stack in C++

First let me preface this by saying that I am, unfortunately, working on a close sourced project. Because of this any code that I can supply would be fairly specific and short. I wish I could release the code, one in support of open source, and two, I know it would be beneficial to answering my question, but at this time, my company has decided to remain close source.

Background:

Developing a client - server application communicating over TCP. Therefor, I an using the normal send() and recv() functions. Additionally, packet payloads are encrypted with a custom algorithm. Commands are sent to the server, but the server does not execute the commands immediately, but stores them in a string array and executes them after the socket is closed (Few exceptions).

Program Information:
Linux sockets
g++
Currently single threaded (This will change)

Problem:

When the client is finished communicating with the server it sends a specific command that is essentially a " I am done, will you send me the commands that you received and the hashed values of each command" packet. When the server receives this command (This is one of the exceptions), it sends the list of commands that it received in one packet followed by the hashes in another packet. On the client side, the client notes that the "finish" command has been sent and blocks with recv(). It receives the first packet, in this case the list of commands, and then blocks with yet another recv() [ Note: I currently have this in a loop, but it doesn't work either way]. At this receive however, I get one character and the client crashes [Note: It crashes because it is looking for a substr in a one character string, so this isn't the real problem].

Additional Information:

I know that the server is sending information because wireshark captures the packet with a byte size of 502 [PSH, ACK], the same size for all data packets and the client replies with a [ACK] packet, so hypothetically, it gets the packet. Also, I ran into this problem before and it seemed that placing a usleep(10) between sends worked, but it does not work on this occasion.
The receive and send buffers are cleared with:
Expand|Select|Wrap|Line Numbers
  1. memset(miscvar, 0x0, LINE_ARRAY_SIZE)
This has always worked, but so have my recv() functions. The code for this send() - recv() is nearly identical to all the others with small variable value changes.

On another note: the recv() recieves more data that is sent, but is cut off at the null character, hence my use of memset.

My thoughts:

I feel as if C++ is racing the stack and recv() before all the data can be sent. I know this isn't true as recv() blocks. I also thought that I wasn't clearing the buffers, but atlas, I am.
--------------------------------------------------------------------------------------------------------------------

Sorry for the anecdote, but I wanted to be as specific as I can under my conditions. If you need to see some part of the code, let me know and I see if I can release it. I apologize ahead of time for any ambiguity.
Jul 29 '08 #1
7 1889
Hmm... Guess I get nothing. :)
Jul 29 '08 #2
Laharl
849 Expert 512MB
Six hours for a fairly complicated question? Give people some time, man. Especially when most of that time covers work hours in the US, where many of the experts are based.
Jul 29 '08 #3
gpraghuram
1,275 Expert 1GB
Why cant you use select function call to check whether data is available in the port and then start reading it.
Also can you put the recv in a loop and read till you get the whole length..

Raghu
Jul 30 '08 #4
@ Laharl

You are absolutely right. I was eager and in all honesty, I thought I would get at least some reply even if it was just "Here's an Idea". I'm use to forums where many people post answers that rarely equate to any solution, mostly because they didn't read the post. At any rate, I apologize.

@ gpraghuram

Both valid Ideas. I'll look into select() as I have only briefly examined them. I originally thought that they only operated on Unix Domain Sockets.

How do you suppose I loop the recv() function? I can't check the exit status of the recv(), as it completes properly. I guess I could loop until I get the size, but the size is "occasionally"* dynamic.

* Sorry, this does slightly negate my original post. I was speaking generally.


Thank you for your input.
Jul 30 '08 #5
Sorry, but this seems worthy of another post:

5. The functions read(), recv(), write(), and send() do not neces-
sarily read/write the full amount of data that you have
requested. If they do read/write the full amount, its because
you have a low traffic load and a fast stream. This is not
always going to be the case. You should cope with the case of
your functions only managing to send or receive a single byte.

6. Never read/write only in single bytes at a time unless your are
really sure that you have a small amount of data to process. It
is extremely inefficient not to read/write as much data as you
can buffer each time. The buffers in the example above are 1024
bytes although they could easily be made larger.
http://www.tin.org/bin/man.cgi?secti...pic=select_tut
Could this perhaps be my problem? I am a little confused by the above statments, they appear to contridict each other, do they not? The send()s and recv()s in both my client and server do not loop, they just sent they message in one shot. This could cause problems when the client and servers are not both on local host because of latency, but could this be the problem now? How would I properly implement this loop?

It seems as if the select() or pselect() will not work, because the recv() functions are returning true ( not less than zero) and it seems that select opperates similarly. Is this true?
Jul 30 '08 #6
gpraghuram
1,275 Expert 1GB
Select basically says whether data is there in the port for reading or writing.
So you should read the data in a loop and keeping track of the number of bytes read.

Raghu
Jul 31 '08 #7
Select basically says whether data is there in the port for reading or writing.
So you should read the data in a loop and keeping track of the number of bytes read.

Raghu
Okay, I try it when I get the chance. We currently merging code so this has been set on the backburner. I will keep you updated.
Jul 31 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is...
0
by: Tipwinners | last post by:
This is a multi-part message in MIME format ---=_AZROORDVTILVT.01AA02E60255CJO.02940930 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit This is an HTML...
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
8
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to...
4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
1
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting...
11
by: tom | last post by:
Hi! Im new to Python and doing exercise found from internet. It is supposed to evaluate expression given with postfix operator using Stack() class. class Stack: def __init__(self): self.items...
7
by: beemomo | last post by:
hi , everyone. I have a logical thinking question here to be solved, any help would be appreciated. There are 25 racing cars in a car racing. The race tracks can only allow maximum 5 cars per...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.