测试unix数据报套接字时,一个程序收,一个程序发,分别绑定自己的socket。结果在收的部分,返回的发送方的地址总是空的,但是返回的地址长度又是对的。

while (  )
{
bzero(&clientaddr, sizeof(struct sockaddr_un));
slen = 0;
rn = recvfrom(fd,buf, sizeof(buf), ,(struct sockaddr *)&clientaddr, slen);
if ( rn == -)
{
perror("recvfrom");
}
buf[n] = ;
}

仔细对比unp的代码,发现   slen =   这行改成   slen = sizeof(strcut sockaddr_un) 结果就对了,细看man

  ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
=====================================================================================================================
    If src_addr is not NULL, and the underlying protocol provides the
source address, this source address is filled in. When src_addr is
NULL, nothing is filled in; in this case, addrlen is not used, and
should also be NULL. The argument addrlen is a value-result argument,
which the caller should initialize before the call to the size of the
buffer associated with src_addr, and modified on return to indicate the
actual size of the source address. The returned address is truncated
if the buffer provided is too small; in this case, addrlen will return
a value greater than was supplied to the call.

  红色部分指出,最后一个参数addrlen是一个"值-结果"的参数,赋给它的初始值用来指定拷贝到倒数第二个参数 src_addr 里面的字节数,返回实际值是实际的源地址结构的大小。当传入的参数小于输出的值,说明返回的源地址被截断了。

上面的例子中,传入的值为0,而输出的值大于0,明显这里有问题,未拷贝任何数据到返回的源地址clientaddr。

sendto函数的坑的更多相关文章

  1. C语言sendto()函数-经socket传送数据以及recvfrom函数《转》

    相关函数:send, sendmsg, recv, recvfrom, socket 头文件:#include <sys/types.h>   #include <sys/socke ...

  2. 关于DateTime自带的AddSeconds等函数的坑

    在此记录一下今天在写一个进程查杀小程序时碰到的关于DateTime的问题:  第一次是用AddSeconds后的时间直接和DateTime.Now做相等判断. class Program { stat ...

  3. 日常踩坑——Dev C++ pow()函数的坑

    坑 Dev C++ pow()函数 那年冬天,显示屏前坐着如喽啰,那时候我含泪发誓,再也不用Dev. 蓝桥杯官网给提供的版本,没办法bug也得硬着头皮用. 16年蓝桥杯的第八题 四平方和定理: 在De ...

  4. python 中 logging 模块的 log 函数以及坑

    记录下吧,一个日志的函数,但有个坑是在调用函数时需要先将函数实例化为一个变量,否则进入某个循环时会多次刷新日志: """ 日志模块 """ ...

  5. python函数的坑

    一.默认参数的坑,如果你的默认参数指向的是可变的数据类型,那么你无论调用多少次这个默认参数,都是同一个. def func(a,list=[]): list.append(a) return list ...

  6. [R]R下as.Date()函数的坑

    问题描述: 在本地使用as.Date()函数从POSIXct类型中提取date时,出现了日期不一致的错误.导致处理数据时,总是出问题. 还好被领导发现数据有误,不然这个bug不知道还要潜伏多久.尽管如 ...

  7. javascript Date 函数的坑

    Javascrip中对日期的解析.格式化简直是混乱不堪,各种的坑,攻城狮们多小心.   1. 不要用 Date("yyyy-mm-dd") 构造函数解析字符串. IE.firefo ...

  8. c++ memset 函数 及 坑

    #include <string.h> #include <stdio.h> typedef struct ss{ int num; ][]; }tent; tent a; i ...

  9. PLS-00306:错误解决思路 - OracleHelper 执行Oracle函数的坑

    如果你是像我一样初次使用Net+Oracle的结合,我想你会跟我一样,有很大的概率碰到这个问题 ==================================================== ...

随机推荐

  1. C++指针数组,二级指针和函数指针的练习

    1.编一程序,将字符串“Hello,C++!”赋给一个字符数组, 然后从第一个字母开始间隔地输出该串(请用指针完成). 代码如下 #include<iostream> #include&l ...

  2. 嗨翻C语言笔记(二)

    ~a a中所有位都取反 a & b a中的位 与 b中的位 (都为1则1,否则为0) a | b a中的位 或 b中的位 (只要对应位一个位1则为1) a ^ b a中的位 亦或 b中的位 & ...

  3. DOM查询

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  4. ELK的简述安装

    一.ElasticSearch集群的安装及其配置 https://www.cnblogs.com/gentle-awen/p/10000801.html 可视化x-pack安装: https://ww ...

  5. hadoop生态搭建(3节点)-07.hive配置

    # http://archive.apache.org/dist/hive/hive-2.1.1/ # ================================================ ...

  6. 初识python 面向对象

    what the f**k!!这个知识点学不好的最大元凶就是,我还单身??? python基础(四): 面向对象的三个特点: 封装,继承,多态 类: 对象是面向对象编程的核心,在使用对象的过程中,为了 ...

  7. ctf题目writeup(1)

    2019/1/28 题目来源:爱春秋 https://www.ichunqiu.com/battalion?t=1 1. 该文件是一个音频文件: 首先打开听了一下,有短促的长的....刚开始以为是摩斯 ...

  8. C语言学习记录_2019.01.29

    C语言的灵魂:指针 #include <stdio.h> int main(int argc, char **argv) {  printf("Hello, World!\n&q ...

  9. R语言绘图:时间序列分析

    ggplot2绘制 arima诊断图 library(ggfortify) autoplot(acf(gold[,2], plot = FALSE)) ggtsdiag(auto.arima(gold ...

  10. 43-Identity MVC:UI

    1-打开之前写的MvcCookieAuthSample项目, 在AccountController新加Register,Login方法 public class AccountController : ...