Linux_Socket_Programming_By_Example
时间:2007-02-14 来源:ffjnfj
socket和普通open打开的file disc是共享同一名字空间的,否则select怎么做呢?
shutdown()
The problem develops when the local process wants to signal to the remote
endpoint that there is no more data to be received. If the local process
closes its socket, this much will be accomplished. However, if it needs to
receive a confirmation from the remote end, it cannot, because its socket is
now closed. Situations like these require a means to half close a socket.
该函数直接关掉socket,而不管有几个fd引用了该socket.这是跟close()不同,close只
有在最后一个引用调用的地方执行关闭操作。也就是说,shutdown在以下2种情况使用:
1. 半关闭一个socket
2. 马上关闭一个socket,而不管别人有没有打开
需要注意的是,shutdown并不会release fd,只有在调用close()后才会release.
当然一次close只能close一个fd
struct sockaddr {
sa_family_t sa_family; /* Address Family */
char sa_data[14]; /* Address data. */
};
PF_LOCAL,SOCK_DGRAM是一个较好的组合,这样可以保留数据的边界,同时在本机上数据
也有一定的保证,不过还是不能保证可靠性的
int socket(int domain, int type, int protocol);
domain: 协议族,所以最好用PF_
type: SOCK_STREAM等
protocol: 具体的协议
Connectionless-oriented communications offer some advantages:
Broadcast capability
In the context of the UDP protocol, there exist the following disadvantages:
There are message size limitations.
If a large UDP packet is sent, it will have to be broken up into several
smaller IP fragments and later re-assembled at the receiving end.
注意是IP层defragment到link层,UDP本身不做
Using fdopen(3) to Associate a Socket with a Stream:
FILE *fdopen(int fildes,const char *mode);
不再write:
fflush(tx); /* 因为FILE可能有buffer,而shutdown是不知道这个buffer的 */
shutdown(fileno(tx),SHUT_WR);
fclose(tx);
do {
clearerr(rx);
ch = fgetc(rx);
} while ( ferror(rx) && errno == EINTR );
有SO_KEEPALIVE, TCP也有KEEPALIVE,有什么区别?
255.255.255.255广播到world,但是一般的router都会丢弃,最好别用
Technically speaking, a TCP stream cannot send out-of-band data. What it does support is a concept
of "urgent" data, which is mapped to the socket API as out-of-band data.
The tcpd program determines whether the client should be given access or not. This is
determined by the combination of the socket addresses involved and the configuration
files /etc/hosts.deny and /etc/hosts.allow.
shutdown()
The problem develops when the local process wants to signal to the remote
endpoint that there is no more data to be received. If the local process
closes its socket, this much will be accomplished. However, if it needs to
receive a confirmation from the remote end, it cannot, because its socket is
now closed. Situations like these require a means to half close a socket.
该函数直接关掉socket,而不管有几个fd引用了该socket.这是跟close()不同,close只
有在最后一个引用调用的地方执行关闭操作。也就是说,shutdown在以下2种情况使用:
1. 半关闭一个socket
2. 马上关闭一个socket,而不管别人有没有打开
需要注意的是,shutdown并不会release fd,只有在调用close()后才会release.
当然一次close只能close一个fd
struct sockaddr {
sa_family_t sa_family; /* Address Family */
char sa_data[14]; /* Address data. */
};
PF_LOCAL,SOCK_DGRAM是一个较好的组合,这样可以保留数据的边界,同时在本机上数据
也有一定的保证,不过还是不能保证可靠性的
int socket(int domain, int type, int protocol);
domain: 协议族,所以最好用PF_
type: SOCK_STREAM等
protocol: 具体的协议
Connectionless-oriented communications offer some advantages:
Broadcast capability
In the context of the UDP protocol, there exist the following disadvantages:
There are message size limitations.
If a large UDP packet is sent, it will have to be broken up into several
smaller IP fragments and later re-assembled at the receiving end.
注意是IP层defragment到link层,UDP本身不做
Using fdopen(3) to Associate a Socket with a Stream:
FILE *fdopen(int fildes,const char *mode);
不再write:
fflush(tx); /* 因为FILE可能有buffer,而shutdown是不知道这个buffer的 */
shutdown(fileno(tx),SHUT_WR);
fclose(tx);
do {
clearerr(rx);
ch = fgetc(rx);
} while ( ferror(rx) && errno == EINTR );
有SO_KEEPALIVE, TCP也有KEEPALIVE,有什么区别?
255.255.255.255广播到world,但是一般的router都会丢弃,最好别用
Technically speaking, a TCP stream cannot send out-of-band data. What it does support is a concept
of "urgent" data, which is mapped to the socket API as out-of-band data.
The tcpd program determines whether the client should be given access or not. This is
determined by the combination of the socket addresses involved and the configuration
files /etc/hosts.deny and /etc/hosts.allow.
相关阅读 更多 +