包裹函数:包裹函数是大写函数 第一字母 作用 返回错误代码
逗号表达式
[root@lyj test]# vi a.c
main()
{ int a,b,c; c=(a=3,b=--a);
printf("c=%d,a=%d,b=%d/n",c,a,b);
}
gcc -o a a.c
c=2,a=2,b=2
main()
{ int a,b,c; c=(a=3,b=a--);
printf("c=%d,a=%d,b=%d/n",c,a,b);
}
./a
c=3,a=2,b=3
main()
{ int a,b,c; c=(a=3,b=a+5);
printf("c=%d,a=%d,b=%d/n",c,a,b);
}
c=8,a=3,b=8
每当一个unix函数发生错误时 全局变量errno将被置成一个指示错误的正值,其函数则返回
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s/r/n", ctime(&ticks)); sizeof(buff)目标缓冲区大小 可防不溢出!比sprint();要好!
相关:
最好 gets strcat strcpy 用 fgets strncat strncpy代替 更好是 strlcat strlcpy 可确保
char *strcpy(char *str1, char *str2);
char *strncpy(char *destin, char *source, int maxlen);
size_t strlcpy (char *dst, const char *src, size_t size);
strlcpy 源码:
size_t strlcpy (char *dst, const char *src, size_t dst_sz)
{
size_t n;
for (n = 0; n < dst_sz; n++) {
if ((*dst++ = *src++) == '/0')
break;
}
if (n < dst_sz)
return n;
if (n > 0)
*(dst - 1) = '/0';
return n + strlen (src);
}
netstate -ni 给出所有接口的名字和统计信息
netstate -r 输出路由表
ping -b 广播地址
tcp发送的数据室通过每一字节关联一个序号来排序。
通知窗口 指出缓冲区可用大小
sctp 流控制传输协议
面向消息 一个端点可有多个沉郁的网络连接 可以铙过网络故障而传输
2.6 tcp
1 服务器 socket bind listen 服务器进入listen
2 Client connet 发送 SYN(ip头部 tcp头部 等) 服务器进入syn_accept
3 server 确认 SYN 发送 ACK 发送SYN
4 客户确认 服务器的SYN
结束差不多同上(FIN)
SYN 的TCP选项
1》MMS (maximum segment size )
2> 窗口规模选项
3》时间戳选项
关于Rst报文产生
异常的关闭流程是源于Rst报文的。一个典型的例子就是当客户端所要链接的服务器端的端口并没有程序在listen,这时服务器端的TCP层会直接发送一个Rst报文,告诉客户端重置连接。Rst报文是无需确认的。客户端在收到Rst后会通知应用层对方异常结束链接(需通过SOCKET API的设置才能得知对方是异常关闭)。
下面是tcp的11种状态:(参考p35)
Figure 2.4. TCP state transition diagram.
closed listen syn_recv syn_sent established close_wait last_ack fin_wait_1 fin_wait_2 closing time_wait
2.8 Sctp 关联建立终止
四路握手
Four-Way Handshake
The following scenario, similar to TCP, occurs when an SCTP association is established:
-
The server must be prepared to accept an incoming association. This preparation is normally done by calling socket, bind, and listen and is called a passive open.
-
The client issues an active open by calling connect or by sending a message, which implicitly opens the association. This causes the client SCTP to send an INIT message (which stands for "initialization") to tell the server the client's list of IP addresses, initial sequence number, initiation tag to identify all packets in this association, number of outbound streams the client is requesting, and number of inbound streams the client can support.
-
The server acknowledges the client's INIT message with an INIT-ACK message, which contains the server's list of IP addresses, initial sequence number, initiation tag, number of outbound streams the server is requesting, number of inbound streams the server can support, and a state cookie. The state cookie contains all of the state that the server needs to ensure that the association is valid, and is digitally signed to ensure its validity.
-
The client echos the server's state cookie with a COOKIE-ECHO message. This message may also contain user data bundled within the same packet.
-
The server acknowledges that the cookie was correct and that the association was established with a COOKIE-ACK message. This message may also contain user data bundled within the same packet.