国产肉体XXXX裸体137大胆,国产成人久久精品流白浆,国产乱子伦视频在线观看,无码中文字幕免费一区二区三区 国产成人手机在线-午夜国产精品无套-swag国产精品-国产毛片久久国产

新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > Ping程序的C語言編程

Ping程序的C語言編程

作者: 時間:2011-02-16 來源:網(wǎng)絡 收藏

  大部分人用ping命令只是作為查看另一個系統(tǒng)的網(wǎng)絡連接是否正常的一種簡單方法。在這篇文章中,作者將介紹如何用C語言編寫一個模擬ping命令功能的程序。

  ping命令是用來查看網(wǎng)絡上另一個主機系統(tǒng)的網(wǎng)絡連接是否正常的一個工具。ping命令的工作原理是:向網(wǎng)絡上的另一個主機系統(tǒng)發(fā)送ICMP報文,如果指定系統(tǒng)得到了報文,它將把報文一模一樣地傳回給發(fā)送者,這有點象潛水艇聲納系統(tǒng)中使用的發(fā)聲裝置。

  例如,在Linux終端上執(zhí)行ping localhost命令將會看到以下結果:

  PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of data.

  64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 time=112 usec

  64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 time=79 usec

  64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=2 ttl=255 time=78 usec

  64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=3 ttl=255 time=82 usec

  --- localhost.localdomain ping statistics ---

  4 packets transmitted, 4 packets received, 0% packet loss

  round-trip min/avg/max/mdev = 0.078/0.087/0.112/0.018 ms

  由上面的執(zhí)行結果可以看到,ping命令執(zhí)行后顯示出被測試系統(tǒng)主機名和相應IP地址、返回給當前主機的ICMP報文順序號、ttl生存時間和往返時間rtt(單位是毫秒,即千分之一秒)。要寫一個模擬ping命令,這些信息有啟示作用。

  要真正了解ping命令實現(xiàn)原理,就要了解ping命令所使用到的TCP/IP協(xié)議。

  ICMP(Internet Control Message,網(wǎng)際控制報文協(xié)議)是為網(wǎng)關和目標主機而提供的一種差錯控制機制,使它們在遇到差錯時能把錯誤報告給報文源發(fā)方。ICMP協(xié)議是IP層的一個協(xié)議,但是由于差錯報告在發(fā)送給報文源發(fā)方時可能也要經(jīng)過若干子網(wǎng),因此牽涉到路由選擇等問題,所以ICMP報文需通過IP協(xié)議來發(fā)送。ICMP數(shù)據(jù)報的數(shù)據(jù)發(fā)送前需要兩級封裝:首先添加ICMP報頭形成ICMP報文,再添加IP報頭形成IP數(shù)據(jù)報。如下圖所示

  IP報頭

  ICMP報頭

  ICMP數(shù)據(jù)報

  IP報頭格式

  由于IP層協(xié)議是一種點對點的協(xié)議,而非端對端的協(xié)議,它提供無連接的數(shù)據(jù)報服務,沒有端口的概念,因此很少使用bind()和connect()函數(shù),若有使用也只是用于設置IP地址。發(fā)送數(shù)據(jù)使用sendto()函數(shù),接收數(shù)據(jù)使用recvfrom()函數(shù)。IP報頭格式如下圖:

  在Linux中,IP報頭格式數(shù)據(jù)結構()定義如下:

  struct ip

  {

  #if __BYTE_ORDER == __LITTLE_ENDIAN

  unsigned int ip_hl:4;    /* header length */

  unsigned int ip_v:4;    /* version */

  #endif

  #if __BYTE_ORDER == __BIG_ENDIAN

  unsigned int ip_v:4;    /* version */

  unsigned int ip_hl:4;    /* header length */

  #endif

  u_int8_t ip_tos;      /* type of service */

  u_short ip_len;     /* total length */

  u_short ip_id;     /* identification */

  u_short ip_off;     /* fragment offset field */

  #define IP_RF 0x8000      /* reserved fragment flag */

  #define IP_DF 0x4000      /* dont fragment flag */

  #define IP_MF 0x2000      /* more fragments flag */

  #define IP_OFFMASK 0x1fff    /* mask for fragmenting bits */

  u_int8_t ip_ttl;      /* time to live */

  u_int8_t ip_p;     /* protocol */

  u_short ip_sum;     /* checksum */

  struct in_addr ip_src, ip_dst; /* source and dest address */

  };

  其中ping程序只使用以下數(shù)據(jù):

  IP報頭長度IHL(Internet Header Length)?D?D以4字節(jié)為一個單位來記錄IP報頭的長度,是上述IP數(shù)據(jù)結構的ip_hl變量。

  生存時間TTL(Time To Live)?D?D以秒為單位,指出IP數(shù)據(jù)報能在網(wǎng)絡上停留的最長時間,其值由發(fā)送方設定,并在經(jīng)過路由的每一個節(jié)點時減一,當該值為0時,數(shù)據(jù)報將被丟棄,是上述IP數(shù)據(jù)結構的ip_ttl變量。

  ICMP報頭格式

  ICMP報文分為兩種,一是錯誤報告報文,二是查詢報文。每個ICMP報頭均包含類型、編碼和校驗和這三項內(nèi)容,長度為8位,8位和16位,其余選項則隨ICMP的功能不同而不同。

  Ping命令只使用眾多ICMP報文中的兩種:"請求回送'(ICMP_ECHO)和"請求回應'(ICMP_ECHOREPLY)。在Linux中定義如下:

  #define ICMP_ECHO  0

  #define ICMP_ECHOREPLY 8

  這兩種ICMP類型報頭格式如下:

  在Linux中ICMP數(shù)據(jù)結構()定義如下:

  struct icmp

  {

  u_int8_t icmp_type; /* type of message, see below */

  u_int8_t icmp_code; /* type sub code */

  u_int16_t icmp_cksum; /* ones complement checksum of struct */

  union

  {

  u_char ih_pptr;   /* ICMP_PARAMPROB */

  struct in_addr ih_gwaddr;  /* gateway address */

  struct ih_idseq   /* echo datagram */

  {

  u_int16_t icd_id;

  u_int16_t icd_seq;

  } ih_idseq;

  u_int32_t ih_void;

  /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */

  struct ih_pmtu

  {

  u_int16_t ipm_void;

  u_int16_t ipm_nextmtu;

  } ih_pmtu;

  struct ih_rtradv

  {

  u_int8_t irt_num_addrs;

  u_int8_t irt_wpa;

  u_int16_t irt_lifetime;

  } ih_rtradv;

  } icmp_hun;

  #define icmp_pptr  icmp_hun.ih_pptr

  #define icmp_gwaddr icmp_hun.ih_gwaddr

  #define icmp_id   icmp_hun.ih_idseq.icd_id

  #define icmp_seq    icmp_hun.ih_idseq.icd_seq

  #define icmp_void  icmp_hun.ih_void

  #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void

  #define icmp_nextmtu  icmp_hun.ih_pmtu.ipm_nextmtu

  #define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs

  #define icmp_wpa  icmp_hun.ih_rtradv.irt_wpa

  #define icmp_lifetime  icmp_hun.ih_rtradv.irt_lifetime

  union

  {

  struct

  {

  u_int32_t its_otime;

  u_int32_t its_rtime;

  u_int32_t its_ttime;

  } id_ts;

  struct

  {

  struct ip idi_ip;

  /* options and then 64 bits of data */

c語言相關文章:c語言教程


tcp/ip相關文章:tcp/ip是什么




評論


相關推薦

技術專區(qū)

關閉