你好,我有一个udp数据包发送方有这个问题。 我希望端口用参数输入但是当我的代码设置时,端口设置为:
#define SERVICE_PORT 123 / * 123是此示例的端口* /之前:
int main( int argc, char * argv [])我有什么方法可以输入带有参数的端口并输入到代码并重新编译它?如果是这样,请告诉我如何。 :) 我的尝试: i试图输入它与变量(将参数存储在veriable中)和自己的代码输入,但我没有那么成功。
解决方案入口点的参数函数 main 不是来自您的代码,而是来自用户。 如果用户输入 yourApplication 8080
或
yourApplication - 端口:8080这意味着使用 argc == 1调用 main 函数;和 argv [] 将是包含选项字符串的端口号的字符串,如-port:8080。您可以检查 argc 的值,并根据命令行输入的一些假设解析字符串数组,您还应该为用户记录这些假设。 br /> 您可以拥有任意数量的参数,可变数量的参数等等。看看一些可用的仅限控制台的实用程序如何使用命令行 - 这类应用程序非常适用于Linux和类UNIX操作系统。
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:
#define SERVICE_PORT 123 /*123 is the port for this example*/before:
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.
解决方案 The arguments of entry-point function main come not from your code, but from the user. Say, if the user enters yourApplication 8080or
yourApplication -port:8080it 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.
更多推荐
Linux C / C ++ udp socket端口问题
发布评论