绘声绘彩的生活志

彩动力、彩信之星的诞生地

Mac OSX下SO_NOSIGPIPE的怪异表现

在linux下为了避免网络出错引起程序退出,我们一般采用MSG_NOSIGNAL来避免系统发送singal。这种错误一般发送在网络断开,但是程序仍然发送数据时,在接收时,没有必要使用。但是在linux下,使用此参数,也不会引起不好的结果,下面是我常用的代码:

    isent = send(socket_fd, szRecvbuff, irecv, MSG_DONTWAIT|
    #if (defined(__APPLE__) && defined(__MACH__))
                                 SO_NOSIGPIPE
    #else
                                 MSG_NOSIGNAL
    #endif
    );
    //recv同

Mac OSX,底层融合了FreeBSD,兼容大部分的POSIX函数,绝大多数的Linux代码可以很少的修改即可使用,在我写的一个TCP测试server中,也是用了上述的代码:

    irecv = recv(socket_fd, szRecvbuff, sizeof(szRecvbuff), MSG_DONTWAIT|
    #if (defined(__APPLE__) && defined(__MACH__))
                                 SO_NOSIGPIPE
    #else
                                 MSG_NOSIGNAL
    #endif
    );

程序表现出来的症状是:
1)recv循环接收到相同的数据,即recv执行后,不会清除底层socket的缓冲区指针,在使用select时,一直提示有读数据,在不使用select循环recv时,永远不会出现0以下结果。直到
2)socket在下次accept时出现段错误。
但同期,使用linux,并无此现象。首先,程序中使用到了没有必要的多余参数,其次,是否说明MacOSX下对于Socket的处理有值得商榷的地方哪?存疑中。 使用下面的方法来设置socket,正常:

    int optval = 1;
    setsockopt(new_fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&optval, sizeof(int));

不同编译器下union的size

 
#include <stdio.h>   
#pragma pack( push, 1 )
union type
{
    unsigned char udpdatatype;
    struct {
        unsigned int version:4;
        unsigned int reserved_sign:2;
        unsigned int needack_sign:1;
        unsigned int resend_sign :1;
    } protocol;
};
 
#pragma pack( pop )
 
int main(int argc, const char *argv[])
{
    printf("sizeof: %u.\n", sizeof(union type));
    return 0;
}

试着在gcc和VC中运行下,看看sizeof是多少,修改struct中的unsigned int 为unsigned char再看看:

1)unsigned int 情况下,sizeof在gcc下为1,VC下为4
2)unsigned char情况下,sizeof在gcc下为1,VC下也为1

strftime下result buffer长度不足造成的函数执行失败.

代码如下:

#include 
#include 
 
int main(int argc, const char *argv[])
{
    time_t t;
    struct tm m;
    char resultbuf[8];
    int resultbuflen = sizeof(resultbuf);
    int len;
    t = time(NULL);
    localtime_r(&amp;t, &amp;m);
    len = strftime(resultbuf, resultbuflen, "%Y%m%d", &amp;m);
    if(len&lt;resultbuflen)
        printf("something wrong, result len:%d.\n\t%s.\n", len, resultbuf);
    else
        printf("now: %s.\n", resultbuf);
    return 0;
}

代码的本意是输出YYYYMMDD格式的当前日期到指定buffer中,此buffer是结构的一部分,此处代指,在buffer长度恰好等于输出结果时,执行出错.即在调用strftime时,需要保证输入的buffer长度大于预期结果至少一个字节.

短信发送状态报告(Status Report)的处理

彩动力

术语:

MS: Mobile Station

SC: Service Centre (used for SMS)

SMS: Short Message Service

SMSC: Short Message Service – Service Centre

SRStatus Report Message storage

SMS作为一种信息载体,不仅仅可以用来传输文本信息,还有很多别的用法,例如使用8位元传输push、铃声、乃至图片,甚至可以于其上构建专属TCP/IPWAP协议栈(事实上很少有人这么去做)。按照ETSI GSM 03.40规范规定,SMS具有下面几种类型:

1)SMS-DELIVER
2)SMS-DELIVER-REPORT
3)SMS-SUBMIT
4)SMS-SUBMIT-REPORT,
5)SMS-STATUS-REPORT
6)SMS-COMMAND

可以看出,短信的发送状态报告(SMS-STATUSREPORT)是一种特定格式的短信,被用来从SCMS传送状态报告。下面我们以一条中国移动实际的状态报告获得过程为例分析其组成元素。

第一步:发送短信前的准备工作

[Read the rest of this entry…]

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny