How to Create a Hosted Network Program in C++?

In summary, the code has been fixed to properly handle user input for starting, stopping, and restarting a hosted network. The issue with the system commands printing to the console can be resolved by using the cout stream instead of calling the system function. Additionally, the output can be suppressed by redirecting it to a null pipe.
  • #1
ExplosiveMercury
8
0
Hi, I've been recently started programming, and wanted to make program I can use in everyday life. I use CMD hosted network every day on my other PC, and wanted to make program in witch I can stop/start/restart hosted network.
This is current progress:

Code:
#include <iostream>

int main ()    {
    using namespace std;
 
    string start;
    string stop;
    string restart = stop + start;
    string answer;
 
    cout << "\n\n        Hosted Network Control        \n\n" << endl;
    cout << "\nStop Hosted Network\n" << endl << "\nStart Hosted Network\n" << endl << "\nRestart Hosted Network\n" << endl;
    cin >> answer;
 
    if (answer == start)
    {
    cout << "\n        Hosted Network Has Been Started.    \n" << endl;
    system("netsh wlan start hostednetwork");
    system("pause>nul");
    }
    if (answer == stop)
    {
    cout << "\n        Hosted Network Has Been Stopped.   \n" << endl;
    system("netsh wlan stop hostednetwork");
    system ("pause>nul");
    }
    if (answer == restart)
    {
    system("netsh wlan stop hostednetwork");
    system("netsh wlan start hostednetwork");
    cout << "\n        Hosted Network Has Been Restarted.    \n" << endl;
    system("pause>nul");
    }
 
 
    system("pause");
    return 0;
}

The thing is, when I input stop, start or restart, no text appears on the screen. Can somebody explain it to me and correct my script. Thanks!
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
ExplosiveMercury said:
Hi, I've been recently started programming, and wanted to make program I can use in everyday life. I use CMD hosted network every day on my other PC, and wanted to make program in witch I can stop/start/restart hosted network.
This is current progress:

C:
#include <iostream>

int main ()    {
    using namespace std;
 
    string start;
    string stop;
    string restart = stop + start;
    string answer;
 
    cout << "\n\n        Hosted Network Control        \n\n" << endl;
    cout << "\nStop Hosted Network\n" << endl << "\nStart Hosted Network\n" << endl << "\nRestart Hosted Network\n" << endl;
    cin >> answer;
 
    if (answer == start)
    {
    cout << "\n        Hosted Network Has Been Started.    \n" << endl;
    system("netsh wlan start hostednetwork");
    system("pause>nul");
    }
    if (answer == stop)
    {
    cout << "\n        Hosted Network Has Been Stopped.   \n" << endl;
    system("netsh wlan stop hostednetwork");
    system ("pause>nul");
    }
    if (answer == restart)
    {
    system("netsh wlan stop hostednetwork");
    system("netsh wlan start hostednetwork");
    cout << "\n        Hosted Network Has Been Restarted.    \n" << endl;
    system("pause>nul");
    }
 
 
    system("pause");
    return 0;
}

The thing is, when I input stop, start or restart, no text appears on the screen. Can somebody explain it to me and correct my script. Thanks!

What's the purpose of your declaration for restart?
C:
    string start;
    string stop;
    string restart = stop + start;
start and stop are empty strings, which causes restart to be initialized also as an empty string.
It appears to me that you are confusing the names of variables with their contents. The start, stop, and restart variables do not contain the strings "start", "stop", and "restart".
 
  • Like
Likes ExplosiveMercury
  • #3
Oh, yeah, I was confused about it, I'll try to fix it. Thanks!
 
  • #4
I've made new code, but problem is still the same. When I input start/stop/reset it just says "Press any key to continue..."
 
  • #5
ExplosiveMercury said:
I've made new code, but problem is still the same. When I input start/stop/reset it just says "Press any key to continue..."

What is your new code?
 
  • #6
Why initialize are::strings at all?

If (answer == "start")

Also, you are aware that you can create an alias in your .bash_rc file for restarting your network since you're just using system anyway.What do you expect > nul to do? Did you mean >/dev/null?
 
  • #7
UPDATE: I just fixed the code, I didn't add "" to start/pause/restart, it works now. Only thing that annoys me is cmd says that hosted network started/ stopped and I want to print only my text. Is there a way to fix it?
 
  • #8
Here's the new program

//Hosted Network Control
#include <iostream>
int main (){
using namespace std;

cout << "HOSTED NETWORK CONTROL" << endl;
cout << "1. Start" << endl << "2. Stop" << endl << "3. Restart" << endl;

string answer;
cin >> answer;

if ((answer == "start") || (answer == "1")) {
system ("netsh wlan start hostednetwork");
cout << "Hosted Network Has Been Started" << endl;
}

if ((answer == "stop") || (answer == "2")) {
system ("netsh wlan stop hostednetwork");
cout << "Hosted Network Has Been Stopped" << endl;
}

if ((answer == "restart") || (answer == "3")) {
system ("netsh wlan stop hostednetwork");
system ("netsh wlan start hostednetwork");
cout << "Hosted Network Has Been Restarted" << endl;
}

system ("pause>nul");
return 0;
}
 
  • #9
ExplosiveMercury said:
UPDATE: I just fixed the code, I didn't add "" to start/pause/restart, it works now. Only thing that annoys me is cmd says that hosted network started/ stopped and I want to print only my text. Is there a way to fix it?
Sure -- don't call the system() function. Just send the strings to the console using the cout stream.

C:
cout << "netsh wlan stop hostednetwork" << endl;
 
  • #10
Mark44 said:
Sure -- don't call the system() function. Just send the strings to the console using the cout stream.

C:
cout << "netsh wlan stop hostednetwork" << endl;
I think the poster is asking how to get what's in the system call to still work, but not print it's stdout.

In the command line, you can tell something not to print by redirecting its output into a null pipe:

echo "hello"

echo "hello" > /dev/null

Try them both and see what happens. It will still print the std errors for you though, which is nice. If you want to remove those too, you can redirect stderr to stdout, which then both go to /dev/null
 

FAQ: How to Create a Hosted Network Program in C++?

1) What is a Hosted Network C++ program?

A Hosted Network C++ program is a computer program written in the C++ programming language that is designed to create and manage a virtual network connection on a host computer.

2) How does a Hosted Network C++ program work?

A Hosted Network C++ program uses the Hosted Network feature in Windows to create a virtual network adapter on the host computer. This adapter can then be used to connect other devices to the host computer and share its internet connection.

3) What are the advantages of using a Hosted Network C++ program?

One of the main advantages of using a Hosted Network C++ program is that it allows for the creation of a virtual network without the need for additional hardware. This can be useful for testing or setting up a temporary network. Additionally, it can be used to extend the range of a wireless network by creating a virtual access point.

4) Are there any limitations to using a Hosted Network C++ program?

Yes, there are some limitations to using a Hosted Network C++ program. It can only be used on Windows operating systems, and the host computer must have a wireless network adapter with Hosted Network support. Additionally, the number of devices that can connect to the virtual network may be limited.

5) How can I get started with using a Hosted Network C++ program?

To get started with using a Hosted Network C++ program, you will need to have a basic understanding of the C++ programming language and the Hosted Network feature in Windows. You can then find and follow tutorials or guides online to help you create your own program. Alternatively, you can use existing libraries or frameworks to simplify the process.

Similar threads

Replies
5
Views
2K
Replies
14
Views
32K
Replies
29
Views
9K
Replies
8
Views
4K
Replies
17
Views
2K
Replies
6
Views
1K
Replies
1
Views
1K
Back
Top