歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 【Unix網絡編程】socket函數與描述符

【Unix網絡編程】socket函數與描述符

日期:2017/3/3 12:51:19   编辑:Linux技術

Socket

Socket 用於創建一個會話的端點,返回一個描述符. 失敗將返回-1,並設置errno.
[code]#include <sys/socket.h>
int socket(int domain, int type, int protocol);

參數

domain

用於指定會話的區域.這個選擇決定用於會話的協議族.這些協議族位於
sys/socket.h

參考手冊.
[code]Name                Purpose                          Man page
AF_UNIX, AF_LOCAL   Local communication              unix(7)
AF_INET             IPv4 Internet protocols          ip(7)
AF_INET6            IPv6 Internet protocols          ipv6(7)
AF_IPX              IPX - Novell protocols
AF_NETLINK          Kernel user interface device     netlink(7)
AF_X25              ITU-T X.25 / ISO-8208 protocol   x25(7)
AF_AX25             Amateur radio AX.25 protocol
AF_ATMPVC           Access to raw ATM PVCs
AF_APPLETALK        AppleTalk                        ddp(7)
AF_PACKET           Low level packet interface       packet(7)

常用的應該時
AF_LOCAL
AF_INET
.

type

這是定義數據語義(semantics)的.
SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported.
[code]   SOCK_DGRAM      Supports datagrams (connectionless, unreliable messages of a fixed maximum length).

   SOCK_SEQPACKET  Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length;  a  consumer
                   is required to read an entire packet with each input system call.

   SOCK_RAW        Provides raw network protocol access.

   SOCK_RDM        Provides a reliable datagram layer that does not guarantee ordering.

   SOCK_PACKET     Obsolete and should not be used in new programs; see packet(7).

   Some socket types may not be implemented by all protocol families.

   Since Linux 2.6.27, the type argument serves a second purpose: in addition to specifying a socket type, it may include the bitwise OR of any of the following values, to modify the behavior of socket():

   SOCK_NONBLOCK    Set the O_NONBLOCK file status flag on the new open file description.
        Using this flag saves extra calls to fcntl(2) to achieve the same result.

   SOCK_CLOEXEC Set  the close-on-exec (FD_CLOEXEC) flag on the new file descriptor.
        See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful.

基於網絡的知識,Stream基於TCP,DGram基於UDP.
我在面試中遇到過一個問怎麼才能讓
accept()
變成非阻塞,這時候就type要位或(|)上
SOCK_NONBLOCK
.

protocol

制定所使用的協議,通常一種socket的協議族只支持一種協議,這種情況可以設為0.但是也有可能有特殊的1對多的情況,這時候我們可以通過getprotoent來獲取.

其他

套接字選項

[code]#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>

int getsockopt(int sockfd, int level, int optname,
               void *optval, socklen_t *optlen);
int setsockopt(int sockfd, int level, int optname,
               const void *optval, socklen_t optlen);

我們可以使用getsockopt和setsockopt來獲取或修改套接字的選項.
其中level是指操作的級別,這裡面的級別分為兩種.第一種時socketAPI級,第二種時協議級.
第一級
level=SOL_SOCKET
,第二種,
level=Protocol_numer
(
Protocol_number
,就是所要操作的協議編號,通過getprotoent來查看).
optname是選項的名字,包含在
<sys/socket.h>
裡了.
optval是值,optlen是值的長度.
其中:
>
SO_KEEPALIVE 保持連接檢測對方主機是否崩潰,避免(服務器)永遠阻塞於TCP連接的輸入。
設置該選項後,如果2小時內在此套接口的任一方向都沒有數據交換,TCP就自動給對方 發一個保持存活探測分節(keepalive probe)。這是一個對方必須響應的TCP分節.它會導致以下三種情況:
1、對方接收一切正常:以期望的ACK響應,2小時後,TCP將發出另一個探測分節。
2、對方已崩潰且已重新啟動:以RST響應。套接口的待處理錯誤被置為ECONNRESET,套接 口本身則被關閉。
3、對方無任何響應:源自berkeley的TCP發送另外8個探測分節,相隔75秒一個,試圖得到一個響應。在發出第一個探測分節11分鐘15秒後若仍無響應就放棄。套接口的待處理錯誤被置為ETIMEOUT,套接口本身則被關閉。如ICMP錯誤是“host unreachable(主機不可達)”,說明對方主機並沒有崩潰,但是不可達,這種情況下待處理錯誤被置為 EHOSTUNREACH。
有關SO_KEEPALIVE的三個參數詳細解釋如下:
(16)tcp_keepalive_intvl,保活探測消息的發送頻率。默認值為75s。
發送頻率tcp_keepalive_intvl乘以發送次數tcp_keepalive_probes,就得到了從開始探測直到放棄探測確定連接斷開的時間,大約為11min。
(17)tcp_keepalive_probes,TCP發送保活探測消息以確定連接是否已斷開的次數。默認值為9(次)。
注意:只有設置了SO_KEEPALIVE套接口選項後才會發送保活探測消息。
(18)tcp_keepalive_time,在TCP保活打開的情況下,最後一次數據交換到TCP發送第一個保活探測消息的時間,即允許的持續空閒時間。默認值為7200s(2h)。
這裡有更詳細的解釋.
但其實像FTP服務器這種有應用層分鐘量級的超時,這是應用層實現的,這可以使得應用層對這種機制有完全控制權.
在SCTP中有心跳機制與此類似.
Copyright © Linux教程網 All Rights Reserved