Linux C / C ++ udp socket端口问题

编程入门 行业动态 更新时间:2024-10-28 11:26:44
本文介绍了Linux C / C ++ udp socket端口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好,我有一个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操作系统。

-SA

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 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

更多推荐

Linux C / C ++ udp socket端口问题

本文发布于:2023-06-10 05:42:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/608214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口   Linux   udp   socket

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!