濂楁帴瀛楀拰鍦板潃
瀵撴剰鏄彲浠ラ氳繃鎺ュ彛鎻掑叆鐨勬柟寮忥紝蹇熷畬鎴愮綉缁滆繛鎺ュ拰鏁版嵁鏀跺彂銆備綘鍙互鎶婂畠鎯宠薄鎴愮幇瀹炰笘鐣岀殑鐢垫簮鎻掑彛锛屾垨鑰呮槸鏃╂湡涓婄綉闇瑕佺殑缃戠粶鎻掓Ы锛屼篃鍙互鐪嬪仛鏄鐗╃悊涓栫晫鐨勭洿鎺ユ槧灏勩
瀹㈡埛绔 鏈嶅姟绔
connect ->(杩炴帴璇锋眰tcp涓夋鎻℃墜) socket
bind
listen
accept
write read
read write
close close
鎴戜滑鍏堜粠鍙充晶鐨勬湇鍔″櫒绔紑濮嬬湅锛屽洜涓哄湪瀹㈡埛绔彂璧疯繛鎺ヨ姹備箣鍓嶏紝鏈嶅姟鍣ㄧ蹇呴』鍒濆鍖栧ソ锛屽彸渚х殑鍥炬樉绀虹殑鏄湇鍔″櫒绔垵濮嬪寲鐨勮繃绋嬶紝棣栧厛鍒濆鍖杝ocket锛屼箣鍚庢湇鍔″櫒绔渶瑕佹墽琛宐ind鍑芥暟锛屽皢鑷繁鐨勬湇鍔¤兘鍔涚粦瀹氬湪涓涓紬鎵鍛ㄧ煡鐨勫湴鍧鍜岀鍙d笂锛岀揣鎺ョ潃锛屾湇鍔″櫒绔墽琛宭isten鎿嶄綔锛屽皢鍘熷厛鐨剆ocket杞寲涓烘湇鍔$鐨剆ocket锛屾湇鍔$鏈鍚庨樆濉炲湪accept涓婄瓑寰呭鎴风璇锋眰鐨勫埌鏉ャ
姝ゆ椂锛屾湇鍔″櫒绔凡缁忓噯澶囧氨缁紝瀹㈡埛绔渶瑕佸厛鍒濆鍖杝ocket锛屽啀鎵цcennect鍚戞湇鍔″櫒绔殑鍦板潃鍜岀鍙e彂璧疯繛鎺ヨ姹傦紝杩欓噷鐨勫湴鍧鍜岀鍙e繀椤绘槸瀹㈡埛绔鍏堢煡鏅撶殑銆傝繖涓繃绋嬶紝灏辨槸钁楀悕鐨則cp涓夋鎻℃墜銆
鍏蜂綋鏉ヨ锛屽鎴风杩涚▼鍚戞搷浣滅郴缁熷唴鏍稿彂璧穡rite瀛楄妭娴佸啓鎿嶄綔锛屽唴鏍稿崗璁爤灏嗗瓧鑺傛祦閫氳繃缃戠粶璁惧浼犺緭鍒版湇鍔$锛屾湇鍔$浠庡唴鏍稿緱鍒颁俊鎭紝灏嗗瓧鑺傛祦浠庡唴鏍歌鍏ュ埌杩涚▼涓紝骞跺紑濮嬩笟鍔¢昏緫鐨勫鐞嗭紝瀹屾垚涔嬪悗锛屾湇鍔$鍐嶅皢寰楀埌鐨勭粨鏋滀互鍚屾牱鐨勬柟寮忓啓缁欏鎴风锛鍙互鐪嬪埌锛屼竴鏃﹁繛鎺ュ缓绔嬶紝鏁版嵁鐨勪紶杈撳氨涓嶅啀鏄崟鍚戠殑锛岃屾槸鍙屽悜鐨勶紝杩欎篃鏄痶cp鐨勪竴涓樉钁楃壒鎬
褰撳鎴风瀹屾垚鍜屾湇鍔″櫒绔殑浜や簰鍚庯紝姣斿鎵ц涓娆elnet鎿嶄綔锛屾垨鑰呬竴娆ttp璇锋眰锛岄渶瑕佸拰鏈嶅姟鍣ㄧ绔紑杩炴帴鏃讹紝灏变細鎵цclose鍑芥暟锛屾搷浣滅郴缁熷唴鏍告鏃朵細閫氳繃鍘熷厛鐨勮繛鎺ラ摼璺悜鏈嶅姟鍣ㄧ鍙戦佷竴涓猣in鍖咃紝灏变細鎵цclose鍑芥暟锛屾暣涓摼璺墠浼氱湡姝e叧闂傚崐鍏抽棴鐨勭姸鎬佷笅锛屽彂璧穋lose璇锋眰鐨勪竴鏂瑰湪娌℃湁鏀跺埌瀵规柟鐨刦in鍖呬箣鍓嶉兘浼氳涓鸿繛鎺ユ槸姝e父鐨勶紱鑰屽湪鍏ㄥ叧闂殑鐘舵佷笅锛屽弻鏂归兘鎰熺煡杩炴帴宸茬粡鍏抽棴銆
浠ヤ笂鐨勬墍鏈夌殑鎿嶄綔锛岄兘鏄氳繃socket鏉ュ畬鎴愮殑锛屾棤璁烘槸瀹㈡埛绔殑connect锛岃繕鏄湇鍔$鐨刟ccept锛屾垨鑰卹ead/write鎿嶄綔锛socket閮芥槸鎴戜滑鐢ㄦ潵寤虹珛杩炴帴锛屼紶杈撴暟鎹殑鍞竴閫斿緞銆
tcp缃戠粶浜や簰鍜屼紶杈撳彲浠ユ兂璞℃垚鎵撶數璇濓紝connect灏卞ソ姣旂數璇濇満鎷ㄥ彿锛屾湇鍔$鐨刡ind灏卞ソ姣旀槸鍦ㄧ數淇″紑鎴凤紝灏嗙數璇濆彿鐮佸拰鎴戜滑鐨勭數璇濇満缁戝畾锛岃繖鏍峰埆浜哄氨鍙互鐢ㄨ繖涓彿鐮佹壘鍒颁綘锛宭isten灏卞ソ姣斾汉浠湪瀹堕噷鍚埌浜嗗搷閾冿紝accept灏卞ソ姣旀槸琚彨鐨勪竴鏂规嬁璧风數璇濆紑濮嬪簲绛旓紝鑷虫锛屼笁娆℃彙鎵嬪氨瀹屾垚浜嗐
鐒跺悗寮濮媟ead/write鐨勬暟鎹紶杈撱
鎸備笂鐢佃瘽锛岃嚜鐒跺氨鏄痗lose銆
鍦ㄤ娇鐢ㄥ鎺ュ瓧鏃讹紝棣栧厛瑕佽В鍐抽氫俊鍙屾柟鐨勫鍧闂锛屾垜浠渶瑕佺煡閬撳鎺ュ瓧鐨勫湴鍧鎵嶈兘寮濮嬪缓绔嬭繛鎺ワ紝灏卞儚鎵撶數璇濇椂闇瑕佸厛鎵剧數璇濈翱锛屾壘鍒颁綘鎯宠鑱旂郴鐨勯偅涓汉锛屼綘鎵嶅彲浠ュ紑濮嬭繛鎺ワ紝寮濮嬩氦娴併
/* POSIX.1g 瑙勮寖瑙勫畾浜嗗湴鍧鏃忎负 2 瀛楄妭鐨勫. */
typedef unsigned short int sa_family_t;
/* 鎻忚堪閫氱敤濂楁帴瀛楀湴鍧 */
struct sockaddr{
sa_family_t sa_family; /* 鍦板潃鏃. 16-bit*/
char sa_data[14]; /* 鍏蜂綋鐨勫湴鍧鍊 112-bit */
};
鍦ㄨ繖涓粨鏋勪綋閲岋紝绗竴涓瓧娈垫槸鍦板潃鏃忥紝瀹冭〃绀轰娇鐢ㄤ粈涔堟牱鐨勬柟寮忓鍦板潃杩涜瑙i噴鍜屼繚瀛橈紝濂芥瘮鐢佃瘽绨块噷鐨勬墜鏈烘牸寮忥紝鎴栬呮槸鍥鸿瘽鏍煎紡锛岃繖涓ょ鏍煎紡鐨勯暱搴﹀拰鍚箟閮芥槸涓嶅悓鐨勩傚湴鍧鏃忓湪glibc閲岀殑瀹氫箟闈炲父澶氾紝甯哥敤鐨勬湁浠ヤ笅鍑犵锛
– AF_LOCAL锛氳〃绀虹殑鏄湰鍦板湴鍧锛屽搴旂殑鏄痷nix濂楁帴瀛楋紝杩欑鎯呭喌涓鑸敤浜庢湰鍦皊ocket閫氫俊锛屽緢澶氭儏鍐典笅涔熷彲浠ュ啓鎴怉F_UNIX锛孉F_FILE;
– AF_INET:鍥犵壒缃戜娇鐢ㄧ殑IPV4鍦板潃锛
– AF_INET6:鍥犵壒缃戜娇鐢ㄧ殑IPV6鍦板潃銆
杩欓噷鐨凙F_琛ㄧず鐨勫惈涔夋槸address family
PF_琛ㄧず鐨勫畯濡侾F_INET,PF_INET6锛孭F_鐨勬剰鎬濇槸`protocol family锛屽崗璁棌鐨勬剰鎬濄
鎴戜滑鐢ˋF_xxx杩欐牱鐨勫兼潵鍒濆鍖杝ocket鍦板潃锛岀敤PF_xxx杩欐牱鐨勫兼潵chushihuasocket銆傛垜浠湪<sys/socket.h>澶存枃浠朵腑鍙互娓呮櫚寰楃湅鍒帮紝杩欎袱涓兼湰韬氨鏄竴涓瀵瑰簲寰椼
/* 鍚勭鍦板潃鏃忕殑瀹忓畾涔 */
#define AF_UNSPEC PF_UNSPEC
#define AF_LOCAL PF_LOCAL
#define AF_UNIX PF_UNIX
#define AF_FILE PF_FILE
#define AF_INET PF_INET
#define AF_AX25 PF_AX25
#define AF_IPX PF_IPX
#define AF_APPLETALK PF_APPLETALK
#define AF_NETROM PF_NETROM
#define AF_BRIDGE PF_BRIDGE
#define AF_ATMPVC PF_ATMPVC
#define AF_X25 PF_X25
#define AF_INET6 PF_INET6
sockaddr鏄竴涓氱敤鐨勫湴鍧缁撴瀯(浣跨敤浜庡绉嶅湴鍧鏃)
/* IPV4 濂楁帴瀛楀湴鍧锛32bit 鍊. */
typedef uint32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
/* 鎻忚堪 IPV4 鐨勫鎺ュ瓧鍦板潃鏍煎紡 */
struct sockaddr_in
{
sa_family_t sin_family; /* 16-bit */
in_port_t sin_port; /* 绔彛鍙 16-bit*/
struct in_addr sin_addr; /* Internet address. 32-bit */
/* 杩欓噷浠呬粎鐢ㄤ綔鍗犱綅绗︼紝涓嶅仛瀹為檯鐢ㄥ */
unsigned char sin_zero[8];
};
鍜宻ockaddr涓鏍凤紝閮芥湁涓涓16-bit鐨剆in_family瀛楁锛屽浜巌pv4鏉ヨ杩欎釜鍊煎氨鏄疉F_INET.
绔彛鍙锋渶澶氭槸16-bit锛屼篃灏辨槸璇存渶澶ф敮鎸2鐨16娆℃柟锛65536.
glibc瀹氫箟鐨勪繚鐣欑鍙
/* Standard well-known ports. */
enum
{
IPPORT_ECHO = 7, /* Echo service. */
IPPORT_DISCARD = 9, /* Discard transmissions service. */
IPPORT_SYSTAT = 11, /* System status service. */
IPPORT_DAYTIME = 13, /* Time of day service. */
IPPORT_NETSTAT = 15, /* Network status service. */
IPPORT_FTP = 21, /* File Transfer Protocol. */
IPPORT_TELNET = 23, /* Telnet protocol. */
IPPORT_SMTP = 25, /* Simple Mail Transfer Protocol. */
IPPORT_TIMESERVER = 37, /* Timeserver service. */
IPPORT_NAMESERVER = 42, /* Domain Name Service. */
IPPORT_WHOIS = 43, /* Internet Whois service. */
IPPORT_MTP = 57,
IPPORT_TFTP = 69, /* Trivial File Transfer Protocol. */
IPPORT_RJE = 77,
IPPORT_FINGER = 79, /* Finger service. */
IPPORT_TTYLINK = 87,
IPPORT_SUPDUP = 95, /* SUPDUP protocol. */
IPPORT_EXECSERVER = 512, /* execd service. */
IPPORT_LOGINSERVER = 513, /* rlogind service. */
IPPORT_CMDSERVER = 514,
IPPORT_EFSSERVER = 520,
/* UDP ports. */
IPPORT_BIFFUDP = 512,
IPPORT_WHOSERVER = 513,
IPPORT_ROUTESERVER = 520,
/* Ports less than this value are reserved for privileged processes. */
IPPORT_RESERVED = 1024,
/* Ports greater this value are reserved for (non-privileged) servers. */
IPPORT_USERRESERVED = 5000
ipv4鐨勫湴鍧鏄竴涓32-bit鐨勫瓧娈碉紝澶ф鏄40浜匡紝涓嶈繃涔熸笎娓愪笉澶鐢ㄤ簡锛屾墍浠pv6涔熺櫥鍦轰簡銆
ipv6濂楁帴瀛楀湴鍧鏍煎紡锛
struct sockaddr_in6
{
sa_family_t sin6_family; /* 16-bit */
in_port_t sin6_port; /* 浼犺緭绔彛鍙 # 16-bit */
uint32_t sin6_flowinfo; /* IPv6 娴佹帶淇℃伅 32-bit*/
struct in6_addr sin6_addr; /* IPv6 鍦板潃 128-bit */
uint32_t sin6_scope_id; /* IPv6 鍩 ID 32-bit */
};
缁撴瀯浣撻暱搴︽槸28涓瓧鑺傦紝鍏朵腑娴佹帶淇℃伅鍜屽煙ip鍦╣libc瀹樼綉娌″嚭鐜帮紝鍙︿竴涓槸褰撳墠杩樻病浣跨敤鐨勫瓧娈
杩欓噷鐨勫湴鍧鏃忔槸AF_INET6锛岀鍙e拰ipv4涓鏍凤紝鍦板潃浠32浣嶅崌绾у埌128浣嶃
ipv4锛宨pv6閮芥槸鍥犵壒缃戝鎺ュ瓧鐨勬牸寮忥紝杩樻湁涓绉嶆湰鍦板鎺ュ瓧鏍煎紡锛岀敤鏉ュ仛浣嶆湰鍦拌繘绋嬮棿鐨勯氫俊锛屼篃灏辨槸AF_LOCAL.
struct sockaddr_un {
unsigned short sun_family; /* 鍥哄畾涓 AF_LOCAL */
char sun_path[108]; /* 璺緞鍚 */
};
鍙戣〃鍥炲