|
Do you want to start it or you want to know what is it if you want to know about it so David's answer is good(his answers are good ) but if you want to leran it if you buy a book is better.;)
|
|
|
|
|
Hi,
I have a simple question concerning the best way to send data over a network. Imagine, you should write a program, e.g. like RADIUS. I actually don`t really know if Radius is a very complicated protocol, but that doesn`t matter.
In a way, it will look like the following example.
- The first byte is contains the id
- The next two byte contains the length of the package
- ...
Now my question is how would you program that in C. Because I think there exist two possibilities:
(a) you create a unsigned char array and save the data into it according to the specifications of the protocol
(b) you create a struct with the elements you need and send this over the network. But is it right that the data, stored in the structure need not be arranged in the memory as I defined it in the struct (from the compilers point of view)?
I`m very glad about any advice.
|
|
|
|
|
You could do it either way. If you know the structure of the data then a struct can be easier.
For example, following your proposed "packet header" which has an id and a length, you could make
a struct like this:
#pragma pack( push, PACKETHEADERpack, 1 )
struct PACKETHEADER
{
unsigned char Id;;
unsigned short Length; <code>
};
#pragma pack( pop, PACKETHEADERpack)
Note that the structure packing is set to 1 so no padding is added to the structure or its
members.
You can then receive a header on a socket something like:
PACKETHEADER PacketHdr;
recv(socket, (char *)&PacketHdr, sizeof(PACKETHEADER), 0);
You also may need to consider byte ordering in multi-byte numeric variables. If you will be
communicating between different platforms then you may need to convert them to network byte
order, send them, then convert them to the native format on the receiving end.
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi Mark
thank you for your help. It was very useful
|
|
|
|
|
I have a DLL which is being developed in VC6.
This DLL uses internal resources such as a look-up table which i access using
AfxGetInstanceHandle() , FindResource and LoadResource API's
This works as expected.
However I have a project configuration that also creates a static library for the same , in which case the above mentioned API's will not be able to access the internal resource.AfxGetInstanceHandle will provide the handle to the application that uses this static library. With this condition , it seems that the internal resource needs to move to the application that uses the static library. For security reasons I dont want to expose this resource to external applications . Is it possible by some means to access the resource from the static library without having to move the internal resource to the application ?
Any ideas or suggestions are desperately needed .
Thanks
|
|
|
|
|
act_x wrote: Is it possible by some means to access the resource from the static library without having to move the internal resource to the application ?
Try this at the beginning of the function that accesses the resource:
AFX_MANAGE_STATE(AfxGetStaticModuleState());
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
New to VC++, working with 6.0
Trying to read a text file one character at a time. If program finds a comma in text file, it stops and creates a string out of all the preceding characters. No problem reading the file or recognizing the comma. The program won't concatenate the chars into one string.
Here's the code:
char buffer[1];
char cur_char = NULL;
CString cur_data;
UINT bytesread;
do
{
cur_data += cur_char;
bytesread=radardata.Read(buffer,1);
cur_char=buffer[0];
}
while (cur_char != ',');
Every time I run it, cur_data ends up NULL. Here's what else I've tried with the same result:
cur_data = cur_data + CString(cur_char);
cur_data = CString(cur_data) + CString(cur_char)
Thanks in advance!
|
|
|
|
|
cur_data should end-up with NULL as you are adding NULL to it from the beginning.
The following will work;
char buffer[1];
char cur_char;
CString cur_data;
UINT bytesread = 0;
do
{
bytesread += radardata.Read(buffer,1);
cur_char=buffer[0];
cur_data += cur_char;
}
while (cur_char != ',');
--
=====
Arman
|
|
|
|
|
|
pkoelsch wrote: Trying to read a text file one character at a time.
You do realize how inefficient this is, don't you?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Like I said, I'm new to VC++. No classes, just opened a book. The last time I did any formal computer programming, we still used line numbers. I'm open to any recommendations.
|
|
|
|
|
Hi All,
I always appreciate your help.
I have a problem with ifstream
This is my 'input.txt' file.
The first digit of each line is the length.
How can I read in this file using while() or for()
2 3 4
5 7 8 10 11 12
7 8 10 16 17 54 37 38
3 33 51 48
...
I tried this one.
...
ifstream inpf_EX("input.txt");
for (i=1; i<=N_Of_LINE;i++)
{
inpf_EX>>EXIST[i][0]);
len=EXIST[i][0];
for(j=1; j<=len; j++)
{
inpf_EX>>EXIST[i][j]);
}
}
Thanks!
...
Yonggoo
|
|
|
|
|
Yonggoo wrote: How can I read in this file using while() or for()
Neither are necessary, and their use would only serve to confuse you further. You can use an iterator on a stringstream object, and then use the copy() function. You don't need an extra digit in the front to tell how many items follow. If it has to stay, however, just read it in and ignore it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I have not used an iterator on a stringstream.
Does std c++ have iterator?
Thanks!
Yonggoo
|
|
|
|
|
Yonggoo wrote: Does std c++ have iterator?
Yes.
class LineItem
{
public:
LineItem() {}
~LineItem() {}
void setLength(const int length) { _Length = length; }
int getLength() const { return _Length; }
void setNumbers(const std::vector<int>& numbers) { _Numbers.assign(numbers.begin(), numbers.end()); }
std::vector<int> getNumbers() const { return _Numbers; }
private:
int _Length;
std::vector<int> _Numbers;
};
std::ostream& operator<<(std::ostream& os, const LineItem& s)
{
os << s.getLength() << "|";
const std::vector<int> numbers = s.getNumbers();
std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}
std::istream& operator>>(std::istream& is, LineItem& s)
{
int length;
is >> length;
std::string sNumbers = "";
std::getline(is, sNumbers);
std::stringstream ss(sNumbers);
std::vector<int> numbers;
std::copy(std::istream_iterator<int>(ss), std::istream_iterator<<int>(), std::back_inserter(numbers));
s.setLength(length);
s.setNumbers(numbers);
return is;
}
void main( void )
{
std::ifstream fin;
std::vector<LineItem> lines;
fin.open("c:\\input.txt");
std::copy(std::istream_iterator<LineItem>(fin), std::istream_iterator<LineItem>(), std::back_inserter(lines));
fin.close();
std::copy(lines.begin(), lines.end(), std::ostream_iterator<LineItem>(std::cout, "\n"));
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
The following is my code for when I click on a button that's labeled "Start". I use a functor to call my class member function pointer (via Execute(), where Execute() takes an int& parameter). When I set a breakpoint at the Execute() calls, and I hover my mouse over "i", the MSVC debugger tells me that i has some really high value like 4632955, and when I check &i in the immediate window, it tells me the memory address of i. Then, I go into Execute() to call the specific function pointer &MainWindow::DoWork, when I check the input parameter (which takes an int& as mentioned earlier), the memory address of that input parameter is &i + 4 bytes. Basically, what I'm saying is that MSVC shows me different memory address values from the calling function ButtonStart() and within the called function DoWork(). The value from ButtonStart() is wrong, as my int is not some really high number, but the value that is actually received in DoWork() is right, and ends up being 5 (what I set it to in the beginning). This is the first problem and I don't know why this is happening.
The second problem is that some of my lines of execution are being skipped. Actually, that is inaccurate, as they are being executed but I cannot set a breakpoint at some of the lines such as the line int i = 5. MSVC will just shift my breakpoint down to the Execute() calls. My debugging info is turned on, and I'm am building in Debug mode, so this shouldn't happen (I've had it happen when building in Release mode).
void MainWindow::ButtonStart()
{
if (!m_threadPool && !m_source && !m_receiver)
{
m_source = new EventSource();
m_receiver = new EventReceiver();
m_threadPool = new ThreadPool(m_source, m_receiver);
m_receiver->HookThreadPool(m_source);
int i = 5;
CallbackWrapperSpecific<MainWindow, void (MainWindow::*)(int&), int> cbwsPtr(this, &MainWindow::DoWork);
CallbackWrapperBase<int>* cbwbPtr = &cbwsPtr;
cbwbPtr->Execute(i);
cbwbPtr->Execute(i);
}
}
|
|
|
|
|
Is a Rebuild All in order?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Yep, I've done multiple cleans and rebuilds -- still no luck. I wonder if I've corrupted the memory somewhere... or I need to enable some kind of extra flag for debugging MFC Multithreaded applications, etc.
|
|
|
|
|
Bah, it was just optimizing for maximum speed (which was a default setting) that caused these problems... I turned it off (/Od) and now I can see everything the way I want to for a debug version.
|
|
|
|
|
Cyrilix wrote: Bah, it was just optimizing...
In debug mode?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Yes, it was the default in debug mode when I created the project.
|
|
|
|
|
<>.,yuutyutyutyu,/,.//,.//,./<.<><<>,<>
';l';l
|
|
|
|
|
Will you stop spamming the forums with your crap please ?
|
|
|
|
|
Dear all
i need your help. in advance i thank u. my issue is as follows:
if i have a menuitem, after i click it, one dialog appears:ask users to enter parameters below: length=10 pixel, width=10pixel, height=10 pixel, then click ok.correspording to cube will show in window. how to program this point? who have any such snippet? so urgent. thanks very much.
regards
zhiyuan
Li Zhiyuan
6/14/2007
|
|
|
|
|
li zhiyuan wrote: who have any such snippet?
My good friend the internet [^] does.
|
|
|
|