Click here to Skip to main content
15,306,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, I have had this problem with a udp packet sender that i have.
I want the port to be inputed with arguments but as my code is set up the port is set with:

C++
#define SERVICE_PORT	123   /*123 is the port for this example*/

before:
C++
int main(int argc, char *argv[])


Is there eny way for me to input the port with arguments insted of going in to the sorce code and re-compile it? If so please tell me how. :)

What I have tried:

i have tried to input it with veriables (storing the argument in the veriable) and input from the code it self, but i have not been so succesfull.
Posted
Updated 21-May-16 6:45am
Comments
Sergey Alexandrovich Kryukov 21-May-16 12:40pm
   
You cannot stop re-posting, even after my warning that it can eventually lead to automatic cancellation of your account, eventually. If I'm not much mistaken, you confessed that you already have been banned by StackOverflow.

I can see that some posts have been removed, so there are more re-posts than one can see right now.

Yes, this very question is somewhat different, so I can answer it. But keep it in mind: some may look only at the title of the question and report it, just because you behave as a persistent re-poster so far. Please understand: it works against you.

—SA

1 solution

The arguments of entry-point function main come not from your code, but from the user.

Say, if the user enters
yourApplication 8080

or
yourApplication -port:8080

it means that the main function is called with argc == 1; and argv[] will be the string containing port number of the option string like "-port:8080". It's up to you to check up the value of argc and parse the array of strings based on some assumptions on the command line input, which you should also document for the user.

You can have any number of arguments, variable number of arguments, and so on. Look how some available console-only utilities work with command line — such applications are very typical for Linux and UNIX-like OS.

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900