Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Datagram and Thread

Posted on 2003-03-29
Medium Priority
Last Modified: 2013-12-14
Hi Every one,
I am new to C++ and I need help in the following so badly.

I wanted to write one program in C++ under Window. The program use Datagram Socket or Raw Socket and use the UDP protorol. When the program start, I wanted it to fire a thread that will be always runing independent of the rest of the program. The fired thread, will be just used for listening for incoming packets. Now after the thread is fired, the program should continue while the thread is listening and prompt the user for input. Once the user input the required information, the program will take these inputs and pass it to another method that use the info to send packets to another machine.
I tired to do this but I never was able to have the thread and the rest of program to be runing at the same time.
A sample program, or a like to a web page that address this problem will be great.

Question by:Mo009
LVL 32

Expert Comment

ID: 8230569
C++ lacks threads.  So you'll have to use some operating specific feature that manages threads.  

But it's impossible to help more at this point since you've said nothing about your host operating system.

Author Comment

ID: 8231673
Thanks jhance,

So if C++ lacks threads and I am using VC++ compiler and window 2000, what should I do ?

LVL 32

Expert Comment

ID: 8231766
C++ lacks threads but Windows 2000 has them.  That's good news for you.

1) To create a thread in Windows use the CreateThread() API. Threads run until they finish or are killed by the creating process or another thread.

2) To program raw sockets in W2K, see the SDK documentation.  Specifically the section titled "TCP/IP Raw Sockets"

3) For a sample of using UDP with WINSOCK, see:

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!


Author Comment

ID: 8232362
Thanks jhance,
sorry but this really driving me crazy since I couldn't get it to work :-(

you said C++ lacks threads but Windows 2000 has them, doesn Window XP has them too ?
Is there something else in C++ rather than thread that I can use that has the same effects as thread ?

1) I already used the Thread() API but the problem I couldn't get the thread to be listening all the time while the program doing what is should do. You can think of this as if I wana create a tranciever that is both server and client. However, I don't want them to be runing sequentially, but rather in parallel. So you can send info while you are listening for other ppl to send to you. I was able to do this easily in Java using threads but i couldn't using C++ :-(
2) I couldn't find the source for TCP/IP Raw Sockets and when I went to SDK it ask for passward that I don't have.
3) thanks for the sample
LVL 32

Accepted Solution

jhance earned 280 total points
ID: 8233515
C++ is a programming language.  It does NOT have anything in it dealing with threads!

So ANY thread solution available will be provided by the host operating system and will likely be specific to one platform.

As a matter of fact, C++ also lacks any support for networking.  But again you have Windows and its sockets implementation to thank.

Yes, Win2000 and WinXP share the same threading model so a W2K theaded application should work on XP as well.  But you said "Windows 2000" here, not XP so I was addressing what you asked!

>>Is there something else in C++ rather than thread that I can use that has the same effects as thread ?

As I said, there is NOTHING in C++ having to do with threads.  There is also nothing in Windows "like a thread but not a thread" that would be useful here.  Why would you need such a thing???

1) I'm quite unfamiliar with ANY Windows API called "Thread()".  If you are using that, then you must be using a non-Windows library of some sort.  Please specify.  But just saying that "I couldn't get it to work.." doesn't provide anyone much to go on to provide advice.  As it turns out the Windows threading model is quite robust and it DOES WORK.  I must conclude that you are simply "doing it wrong" here.  Post some relevant code and perhaps your mistake can be pointed out.

BTW, you mentioned your are "new to C++" here.  Can I also assume you are new to Windows programming?  If so, why don't you learn to "walk" before you "run"?  Muli-threaded programming is (IMHO) an ADVANCED TOPIC.  MAybe you need to spend some quality time programming a simgle-threaded app and learn what you are doing.

2) NO PASSWORD is needed to access the MSDN documentation on the web (http://msdn.microsoft.com) nor is any password needed to download the Windows SDK.  (Frankly, if you cannot figure this out you're in the wrong line of work!!)

3) Well at least you got one thing figured out!

Expert Comment

ID: 8234092
Since you are using VC++ you could use MFC. Look at the sample application CHAT. You can find it at msdn.microsoft.com or if you installed the whole MSDN you have it on you computer.

Expert Comment

ID: 9551817
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: jhance

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

572 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