我目前正在开发一个接收客户端请求的UDP服务器.我收到的数据报是一个5个元素长的字节(char)数组,最后两个元素是一个端口号.
最终,该服务器必须在其自己的数据报中返回IP地址和端口号.
我已经知道如何使用inet_ntop和我连接和接收的sockaddr结构来打印出ip,但它返回的字符串不是我想要的格式.例如:
string1 = inet_ntop(their_addr.ss_family,get_in_addr(
(struct sockaddr *)&their_addr),s, sizeof s);
Run Code Online (Sandbox Code Playgroud)
收益:
127.0.0.1
Run Code Online (Sandbox Code Playgroud)
要么:
[1][2][7][.][0][.][0][.][1]
Run Code Online (Sandbox Code Playgroud)
当我需要这样的东西:
[127][0][0][1]
Run Code Online (Sandbox Code Playgroud)
我应该使用某种字符和数组操作来制作我的4元素字节数组吗?或者sockaddr是否有这样的信息,我可以把它留在这个十六进制形式并返回它?
我对计划一无所知,我觉得一旦我回答这个问题,其余的功课应该顺利进行.
我正在定义一个以列表作为唯一参数的函数,然后返回相同的列表,其中第一个元素添加到其余元素中.例如:
(addFirst ‘(4 3 2 1)) => (8 7 6 5)
Run Code Online (Sandbox Code Playgroud)
我觉得我应该在这里使用地图和汽车功能......但我似乎无法完全正确.我当前版本的代码如下所示:
(define (addlist x) ;adds the first element of a list to all other elements
(define a (car x)) ;a is definitely the first part of the list
(map (+ a) x)
)
Run Code Online (Sandbox Code Playgroud)
如何使添加功能以这种方式工作?显然我不能提供列表作为参数,但我应该再次使用汽车还是递归?
好的,对于后代,这是完整的,正确的,格式化的代码:
(define (addlist x) ;adds the first element of a list to all other elements
(define a (car x)) ;a is definitely the first part of the list
(map (lambda (y) (+ a y)) …
Run Code Online (Sandbox Code Playgroud)