概述
實際應用中,相信各位小伙伴都遇到過以下的情況,只有一個網路埠,但是想要與多臺設備通訊,而不同的設備網段又不一樣,這個時候需要頻繁的修改自己的ip地址,顯得很不方便,
windows系統下,作業系統為我們提供了一種可以添加多個ip地址的方法,用戶可以很方便的把多個網段ip加入到系統網路中,這樣就避免了頻繁修改ip的作業,那么在vxWorks下有沒有這種機制呢?答案是當然有,
注意
開發環境:vxWorks6.9.4,workbench3.3.5,
另外,小編所有文章均是自己親手撰寫驗證,由于檔案太多,小編就不在公眾號后臺一一回復列舉了,若需要小編的工程代碼,請關注公眾號,后臺回復需要的工程檔案,如想要本文中的工程源檔案可回復“實時系統vxWorks - 增加洗掉ip檔案”,小編看到后會第一時間回復,
以下為工程目錄檔案內容,有需要的小伙伴后臺發送相關資訊給小編獲取,
![]()
檔案內容如下:
obj:存放目標檔案,
add_ip:vxWorks應用工程,
介面
庫檔案
關于網路ip的操作,包含在頭檔案ifLib.h中,打開幫助檔案,可以看到各介面定義,
注意,在使用ifLib.h的時候,需要添加組件INCLUDE_IPWRAP_IFLIB,
![]()
/** * @ifLib.h 網路介面庫,使用時需添加組件INCLUDE_IPWRAP_IFLIB **/ ifAddrGet( ); /* get the Internet address of a network interface */ ifAddrAdd( ); /* Add an interface address for a network interface */ ifAddrDelete( );/* delete an interface address for a network interface */ ifAddrSet( ); /* set an interface address for a network interface */ ifBroadcastSet( ); /* set the broadcast address for a network interface */ ifBroadcastGet( ); /* get the broadcast address for a network interface */ ifDstAddrGet( );/* get the Internet address of a point-to-point peer */ ifDstAddrSet( ); /* define an address for the other end of a point-to-point link */ ifMaskSet( );/* define a subnet for a network interface */ ifMaskGet( );/* get the subnet mask for a network interface */ ifFlagSet( );/* specify the flags for a network interface */ ifFlagGet( );/* get the network interface flags */ ifFlagChange( );/* change the network interface flags */ ifMetricSet( );/* specify a network interface hop count */ ifMetricGet( );/* get the metric for a network interface */ ifIndexToIfName( );/* returns the interface name given the interface index */ ifNameToIfIndex( );/* returns the interface index given the interface name */ ifProxyArpDisable( );/* disables proxy Arp service on an interface */ ifProxyArpEnable( );/* enables proxy Arp service on an interface */ ifRouteDelete( );/* delete routes associated with a network interface */ ifAllRoutesDelete( );/* delete all routes associated with a network interface */添加ip
使用介面ifAddrAdd實作
![]()
/** * @增加網路地址到指定網路介面上 * @interfaceName:網路介面名,如gei2,ei0等, * @interfaceAddress:需要添加的IPv4地址 * @broadcastAddress:廣播地址,假的? * @subnetMask:子網掩碼 * @成功回傳OK,失敗回傳ERROR, **/ STATUS ifAddrAdd( char *interfaceName, /* name of interface to configure, i.e. ei0 */ char *interfaceAddress, /* IPv4 Address to be added to interface */ char *broadcastAddress, /* Dummy */ int subnetMask /* Optional subnet mask */ )洗掉ip
使用介面ifAddrDelete實作
![]()
/** * @洗掉指定網路介面上的指定網路地址 * @interfaceName:網路介面名,如gei2,ei0等, * @interfaceAddress:需要洗掉的IPv4地址 * @成功回傳OK,失敗回傳ERROR, **/ STATUS ifAddrDelete( char *interfaceName, /* name of interface to configure, i.e. ei0 */ char *interfaceAddress /* IPv4 Address to be deleted from the interface */ )
示例
★示例通過撰寫代碼向用戶展示如何添加ip和洗掉ip,
★包含演示程式main.c(已驗證通過),
main.c
/** * @Filename : main.c * @Revision : $Revision: 1.00 $ * @Author : Feng(更多編程相關的知識和原始碼見微信公眾號:不只會拍照的程式猿,歡迎訂閱) * @Description : 添加ip和洗掉ip測驗 **/ #include <vxWorks.h> #include <sockLib.h> #include <inetLib.h> #include <hostlib.h> #include "ifLib.h" #include <stdio.h> /** * 添加ip */ void add_ip(void) { if (ifAddrAdd( "gei2", "192.168.100.2", NULL, 0xffffff00) == ERROR) printf("gei2 ip[192.168.100.2] add error...\n"); if (ifAddrAdd( "gei2", "192.168.100.3", NULL, 0xffffff00) == ERROR) printf("gei2 ip[192.168.100.3] add error...\n"); } /** * 洗掉ip */ void del_ip(void) { if (ifAddrDelete( "gei2", "192.168.100.2") == ERROR) printf("gei2 ip[192.168.100.2] delete error...\n"); if (ifAddrDelete( "gei2", "192.168.100.3") == ERROR) printf("gei2 ip[192.168.100.3] delete error...\n"); }
驗證
打開鏡像工程,添加組件INCLUDE_IPWRAP_IFLIB,
![]()
編譯鏡像并啟動目標機,
輸入命令ifconfig,查看當前網路狀態,可以看到,
目前系統使用一個網口,網口名為”gei2”,ip地址為192.168.10.144,
![]()
使用命令列測驗
添加IP
使用命令ifAddrAdd "gei2", "192.168.100.2", "192.168.100.0", 0xffffff00,添加ip(192.168.100.2)到網路介面gei2上,
添加完成后,使用ifconfig命令查看是否添加成功,
![]()
打開windows命令列,使用ping命令,查看網路是否聯通,
![]()
洗掉IP
繼續使用命令ifAddrDelete "gei2", "192.168.100.2",洗掉gei2網口上ip(192.168.100.2),
洗掉完成后,使用ifconfig命令查看是否洗掉成功,
![]()
同樣打開windows命令列驗證網路是否聯通,
![]()
使用代碼的方式測驗
添加ip
新建應用工程add_ip,撰寫相應的代碼,編譯后下載到目標機,
呼叫函式add_ip,用于添加ip,呼叫完成,使用ifconfig查看ip是否添加成功,
![]()
打開windows命令列,使用ping命令,查看網路是否聯通,
![]()
洗掉ip
呼叫函式del_ip,用于洗掉ip,呼叫完成,使用ifconfig查看ip是否洗掉成功,
![]()
同樣打開windows命令列驗證網路是否聯通,
往期 · 推薦
實時系統vxWorks - 任務(重要)
實時系統vxWorks - 加載應用程式的方法
實時系統vxWorks - 在線除錯
實時系統vxWorks - 虛擬機環境搭建
實時系統vxWorks - zynq7020移植vxWorks
關注
更多精彩內容,請關注微信公眾號:不只會拍照的程式猿,本人致力分享linux、設計模式、C語言、嵌入式、編程相關知識,也會抽空分享些攝影相關內容,同樣也分享大量攝影、編程相關視頻和原始碼,另外你若想要獲得更多內容教程請關注公眾號:不只會拍照的程式猿,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/384413.html
標籤:其他

