濂楁帴瀛

濂楁帴瀛楀拰鍦板潃

socket (濂楁帴瀛)

瀵撴剰鏄彲浠ラ氳繃鎺ュ彛鎻掑叆鐨勬柟寮忥紝蹇熷畬鎴愮綉缁滆繛鎺ュ拰鏁版嵁鏀跺彂銆備綘鍙互鎶婂畠鎯宠薄鎴愮幇瀹炰笘鐣岀殑鐢垫簮鎻掑彛锛屾垨鑰呮槸鏃╂湡涓婄綉闇瑕佺殑缃戠粶鎻掓Ы锛屼篃鍙互鐪嬪仛鏄鐗╃悊涓栫晫鐨勭洿鎺ユ槧灏勩

瀹㈡埛绔 鏈嶅姟绔

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濂楁帴瀛楁牸寮忓湴鍧

/* 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];   /* 璺緞鍚 */
};

鍑犵濂楁帴瀛楀湴鍧鏍煎紡姣旇緝

All posts

Other pages

鍙戣〃璇勮

鐢靛瓙閭欢鍦板潃涓嶄細琚叕寮銆 蹇呭~椤瑰凡鐢*鏍囨敞