|
be careful when you send numbers throw sockets.
You should do some htonl() and/or htons() calls to make sure the bytes are in correct order (the bytes order is not always the same between your computer and on the network). these function so abstract this from you.
Me, i should even have done this :
<font style="color:blue;">#define</font> BUFFER_SIZE 32 <font style="color:green;">// 32 for example</font>
<font style="color:green;">
<font style="color:blue;">long</font> MyFirstLong, MySecondLong; <font style="color:green;">
<font style="color:blue;">char</font> strBuffer[BUFFER_SIZE];
<font style="color:blue;">int</font> sockfd;
<font style="color:green;">
recv(sockfd, strBuffer, BUFFER_SIZE, 0); <font style="color:green;">
sscanf(strBuffer, "%l%l", &MyFirstLong, &MySecondLong);
Is that correct for you Mr L. ?
TOXCCT >>> GEII power
|
|
|
|
|
Thanks, but it doesn't seem to be working.
When I output the strBuffer , it does change, and it should, but when using sscanf() the values of firstLong and secondLong are both what they were initialized to.
So, do I use htonl/htons on the values I send/receive. I currently use it when I set the value of the port. Could that be the problem?
Thanks,
- Steve
|
|
|
|
|
yes
in fact, the names of these functions means :
htons() -> host to network short
htonl() -> host to network long
ntohs() -> network to host short
ntohl() -> network to host long
when you send) datas to the network, use htonX(), and when you recv(), use ntohX().
i hope that's clear enought...
sorry for my bad example, but perhaps there's a mistake in your code.... who knows...
see you later
TOXCCT >>> GEII power
|
|
|
|
|
Are you using UDP? If so then you will probably find that both 'longs' were returned in the same message and that since you haven't read the whole message the remainder has been discarded.
Byte order is a matter for the client and server to agree upon, in this case it's a secondary issue since you aren't able to see the right data, byte reversed or not.
I would expect you to have to do something like,
long lReq ;
lReq = htonl ( 123456 ) ;
sendto ( s, &lReq, sizeof ( lReq ), ... ) ;
...
long Response [ 2 ] ;
recv ( s, Response, sizeof ( resp ), 0 ) ;
long lR1 = ntohl ( Response [ 0 ]) ;
long lR2 = ntohl ( Response [ 1 ]) ;
If you're using connected TCP sockets then ignore this...
Paul
|
|
|
|
|
Hello,
I am writing a application in Visual C++ .NET 7.0. I'd like to keep my classes in seperate files to keep things organized, but I am having difficulty accessing classes from another file. Can someone help me find a solution accessing between files? I would rather not have multiple classes in a single file. What am I doing wrong?
Here is a simplified version:
Class1.h
#ifndef __Class1H__
#define __Class1H__
#include "Class2.h"
class Class1
{
public:
Class1();
~Class1();
//This doesn't work because it is in a different file!
Class1(Class2* object);
//This doesn't work because it is in a different file!
Class2 obj;
};
#endif // __Class1H__
Class2.h
#ifndef __Class2H__
#define __Class2H__
class Class2
{
public:
Class2();
~Class2();
};
#endif // __Class2H__
Class1.cpp
#include "Class1.h"
Class1::Class1()
{
}
Class1::~Class1()
{
}
Class2.cpp
#include "Class2.h"
Class2::Class2()
{
}
Class2::~Class2()
{
}
|
|
|
|
|
I don't see any problem. VC++ 7 is quite happy with those four files.
Maxwell Chen
|
|
|
|
|
Oops, well it appears that my simplified sample compiles. I was able to make the problem I was having go away, by declaring the class at the top of the Class1.h file:
include...
class Class2;
class Class1;
Even though it works for my simplified version, is there a better or correct way to do this?
|
|
|
|
|
Well, I encourage you to read Chapter 9 : "Source Files and Programs" of the book "The C++ Programming Language, 3rd Edition" by Stroustrup.
Maxwell Chen
|
|
|
|
|
Good advice,
I will get the book. thanks
|
|
|
|
|
Hello,
I figured out the problem I was having, but I still don't know a good solution.
If you take the code posted above... and add the following line to Class2.h:
#include "Class1.h"
So there seems to be a problem at the posted comments when I add this include. Why can't I link in this way, and what suggestions can anybody give me to avoid this sort of thing.
Thank You
|
|
|
|
|
Originally Class2 has been included by Class1 in file "Class1.H". Now you added #include "Class1.h" in file Class2.h, you got cross inclusion! ... like your bowels getting knotted.
#include "some_file" means to embed the entire texts of the file "some_file" into where the #include directive being invoked.
Foe example, "MyHeader.H":
class Foo { };
And another file, FooBar.h, includes MyHeader:
#include "MyHeader.h"
class Bar {
public:
Bar() { }
};
You can now imagine file "FooBar.h" as the below:
class Foo { };
class Bar {
public:
Bar() { }
};
And to the view of compiler, it is!
Maxwell Chen
|
|
|
|
|
Thanks,
No more cross-inclusion.
|
|
|
|
|
I want to write a voice chat program in LAN using Visual C++, but don't know where to begin. Please give me some hints and documentations . Thanks.!
|
|
|
|
|
You could use Microsoft Speech (if both comptuers will be using XP (and NT/2000?), or DirectX DirectPlay (I think, or DirectSound) for the voice part.
I would get the DirectX and/or Speech SDK from microsoft and look around there.
Speech: http://www.microsoft.com/speech/download/sdk51/
DirectX: http://www.microsoft.com/downloads/details.aspx?FamilyId=124552FF-8363-47FD-8F3B-36C226E04C85&displaylang=en
wWw.KruncherInc.cOm - My cool programs
|
|
|
|
|
|
I want to make a program like some of the installers I've seen, if the directory does not exist it asks if you would like to create it. This is my only idea:
<br />
if(CreateDirectory("C:\\Something",0) == TRUE)<br />
{<br />
RemoveDirectory(C:\\Something");<br />
if(MessageBox(0,"Directory exists, would you like me to create it?","?",0) == IDYES)<br />
CreateDirectory(C:\\Something",0);<br />
}<br />
<br />
<br />
if(CreateDirectory("C:\\Something",0) == TRUE)<br />
{<br />
if(MessageBox(0,"Directory exists, would you like me to create it?","?",0) == IDNO)<br />
RemoveDirectory(C:\\Something");<br />
}<br />
Either way they don't seem to be very good, is there another way?
wWw.KruncherInc.cOm - My cool programs
|
|
|
|
|
KingKruncher wrote:
is there another way
Yes there is (actually more than one). One of them is to use GetFileAttributes to check for the existence of a file or directory. Take a look:
if (GetFileAttributes(TEXT("c:\\some_directory")) == (DWORD)-1)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
if (MessageBox(NULL, TEXT("Directory c:\\some_directory does not exists. Would you like to create it?"), TEXT("Please Confirm"), 0) == IDYES)
{
}
}
}
Hope this helps.
Gurmeet S. Kochar If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
Thank you alot Gurmeet S. Kochar. That still doesn't seem right, but it's better than what I had. Thanks again.
wWw.KruncherInc.cOm - My cool programs
|
|
|
|
|
|
Well, I was just thinking there would be a function called "DoesDirectoryOrFileExist" or something like that, but I just couldn't find any thing.
wWw.KruncherInc.cOm - My cool programs
|
|
|
|
|
I am trying to open a popup menu from a menubar without progmatically, without the mouse necesarily being near the menu and without SendInput.
SendMessage( targetWindow, WM_SYSCOMMAND, SC_MOUSEMENU, lParam );
The documentation for SC_MOUSEMENU says "Retrieves the window menu as a
result of a mouse click.". Now I know the parameters I send are correct, but it does nothing. Can someone explain why it doesnt work? Or possibly explain how I could get it to work?
lParam = MAKELPARAM( screenIntersect.x, screenIntersect.y );
I have matched my screenIntersect to the values I got from Spy++ just to ensure they were correct compared to normal ( not gernerated by me )
messages.
I know I can send ALT+downarrow key command to open the first menu.. only problem with that is if the menu is already in tht "ALT" state sending another ALT will turn that state off. Also its not really the same since the ALT state will wait for a hotkey, not what normall happens when you open a menu with a mouse click.
Thanks,
|
|
|
|
|
I need to develope a text editor that can also display bmp images. I need to perform clipboard functions on bitmaps as well as text.
I m inheriting a new class from CEdit and having a CBitmap member variable. I want to override the CEdit paste (infact clipboard methods) method so taht I can display bitmaps alongwith the text since CEdit allows only CF_TEXT format.
I cant find any way at the moment to do this.
A lot of thanks for the suggestions.
|
|
|
|
|
|
OK. I try to see whether CRichEdit provides this functionality and how to use it. but isnt there anyway to override CEdit::paste??
|
|
|
|
|