通常我們在說到網(wǎng)絡(luò)編程時默認(rèn)是指TCP編程,即用前面提到的socket函數(shù)創(chuàng)建一個socket用于TCP通訊,函數(shù)參數(shù)我們通常填為SOCK_STREAM。即socket(PF_INET, SOCK_STREAM, 0),這表示建立一個socket用于流式網(wǎng)絡(luò)通訊。
通過查看socket的man手冊可以看到socket函數(shù)的第一個參數(shù)的值可以為下面這些值:
Name Purpose PF_UNIX, PF_LOCAL Local communication PF_INET IPv4 Internet protocols PF_INET6 IPv6 Internet protocols PF_IPX IPX - Novell protocols PF_NETLINK Kernel user interface device PF_X25 ITU-T X.25 / ISO-8208 protocol PF_AX25 Amateur radio AX.25 protocol PF_ATMPVC Access to raw ATM PVCs PF_APPLETALK Appletalk PF_PACKET Low level packet interface |
SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be sup‐ ported. 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 con‐ sumer is required to read an entire packet with each read 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). |
1、創(chuàng)建一個socket,用函數(shù)socket(); 2、設(shè)置socket屬性,用函數(shù)setsockopt(); * 可選 3、綁定IP地址、端口等信息到socket上,用函數(shù)bind(); 4、開啟監(jiān)聽,用函數(shù)listen(); 5、接收客戶端上來的連接,用函數(shù)accept(); 6、收發(fā)數(shù)據(jù),用函數(shù)send()和recv(),或者read()和write(); 7、關(guān)閉網(wǎng)絡(luò)連接; 8、關(guān)閉監(jiān)聽; |
1、創(chuàng)建一個socket,用函數(shù)socket(); 2、設(shè)置socket屬性,用函數(shù)setsockopt();* 可選 3、綁定IP地址、端口等信息到socket上,用函數(shù)bind();* 可選 4、設(shè)置要連接的對方的IP地址和端口等屬性; 5、連接服務(wù)器,用函數(shù)connect(); 6、收發(fā)數(shù)據(jù),用函數(shù)send()和recv(),或者read()和write(); 7、關(guān)閉網(wǎng)絡(luò)連接; |
1、創(chuàng)建一個socket,用函數(shù)socket(); 2、設(shè)置socket屬性,用函數(shù)setsockopt();* 可選 3、綁定IP地址、端口等信息到socket上,用函數(shù)bind(); 4、循環(huán)接收數(shù)據(jù),用函數(shù)recvfrom(); 5、關(guān)閉網(wǎng)絡(luò)連接; |
1、創(chuàng)建一個socket,用函數(shù)socket(); 2、設(shè)置socket屬性,用函數(shù)setsockopt();* 可選 3、綁定IP地址、端口等信息到socket上,用函數(shù)bind();* 可選 4、設(shè)置對方的IP地址和端口等屬性; 5、發(fā)送數(shù)據(jù),用函數(shù)sendto(); 6、關(guān)閉網(wǎng)絡(luò)連接; |
聯(lián)系客服