sock = socket(PF_INET, SOCK_STREAM, 0);
然後在綁定本地地址或連接遠程地址時需要初始化sockaddr_in結構,其中指定address family時一般設置為AF_INET,即使用IP。
問題是AF_INET和PF_INET到底有什麼區別,甚至很多參考書上都是混用。今 天查了一下相關頭文件的定義,找到了答案。
AF = Address Family
PF = Protocol Family
AF_INET = PF_INET
所以,理論上建立socket時是指定協議,應該用PF_xxxx,設置地址時應該用AF_xxxx。當然AF_INET和 PF_INET的值是相同的,混用也不會有太大的問題。
reference Andy Yang
The manifest constants used under 4.x BSD for protocol families are PF_UNIX, PF_INET, etc.,while AF_UNIX etc. are used for address families. However, already the BSD man page promises: "The protocol family generally is the same as the address family", and subsequent standards use AF_* everywhere.
回覆刪除