資源簡介
c語言,包含獲取指定網口ip地址,mac地址,網關,以及c語言實現修改ip,網關,子網掩碼,以及設置socket套接字加入到多播組的方法;。

代碼片段和文件信息
/******************************************************************************
*
*
*******************************************************************************
* File?name: ?net.c
* Purpose: ?????
* Author: ?Felix(HuangFei)
* Version: ?? ?v01.00
* Compiler: ?arm-linux-gcc
* Created?on: ?2014-11-28
*******************************************************************************
* Note:
* Revision?history:??
*???????????????????????Created?by?HuangFei????2014-11-28
******************************************************************************/
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?“net.h“
#define?TRUE????1
#define?FALSE???0
#define?MAC_ADDRESS_LEN?6
#define?PATH_PROCNET_ROUTE “/proc/net/route“
#define?IFACE_NAME_LENGTH????????IF_NAMESIZE
/**********************************************************************
?*?Function:??????SetMacAddress
?*?Description:???設置mac地址
?*?Input:?????????pInterface?:網絡接口名字?如:eth0,pMacAddress:mac地址首地址
?*?Return:????????成功?1,失敗?0
?*?Others:????????
**********************************************************************/
unsigned?char?SetMacAddress(char?*pInterfacechar?*pMacAddress)
{
????struct?ifreq?ifr;
????struct?sockaddr?*hwaddr;
????unsigned?char?*hwptr;
????int?sockfd;
????int?i;
/*?判斷接口輸入是否正確?*/
????if((pInterface?==?NULL)||(pInterface[0]!=‘e‘)||(pInterface[1]!=‘t‘)||(pInterface[2]!=‘h‘))
????{
????????printf(“input?inteface?error!\n“);
????????return?FALSE;
????}
????if(pMacAddress?==?NULL)
????{
????????printf(“input?mac?address?error\n“);
????????return?FALSE;
????}
????
sockfd=?socket(AF_INETSOCK_DGRAM0);
if?(sockfd<0)
{
printf(“Can‘t?creat?socket??\r\n“);
}
memset(&ifr?0?sizeof(ifr));
strncpy(ifr.ifr_name?pInterface?sizeof(ifr.ifr_name)-1);
????
?????/*讀出socket的信息*/
if?(0?>?ioctl(sockfd?SIOCGIFFLAGS?&ifr))
{
printf(“close?the?ethx:unknown?interface\r\n“);
}
ifr.ifr_flags?&=?~IFF_UP;
????/*將信息寫到socket中,從而關閉eth0*/
if?(0?>?ioctl(sockfd?SIOCSIFFLAGS?&ifr))?
{
printf(“Can‘t?close?the?ethx?\r\n“);
????????return?FALSE;
}
/*---------------------------------------------------------------------*/
/*?set?the?interface?eth0‘s??MAC?address???????????????????????????????*/
/*---------------------------------------------------------------------*/
memset(&ifr?0?sizeof(ifr));
strncpy(ifr.ifr_name?pInterface?sizeof(ifr.ifr_name)-1);
hwaddr=(struct?sockaddr?*)&ifr.ifr_hwaddr;
????
????/*ARPHRD_ETHER=1,標識接收的ARP為以太網格式*/
hwaddr->sa_family?=?ARPHRD_ETHER;
hwptr=hwaddr->sa_data;
for(i=0;i<6;i++)
*hwptr++?=?(unsigned?char)pMacAddress[i];
if?(0?>?ioctl(sockfd?SIOCSIFHWADDR?&ifr))
{
printf(“Can‘t?set?the?MA
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????26187??2015-04-27?20:40??net.c
?????文件???????1895??2015-04-27?20:40??net.h
-----------?---------??----------?-----??----
????????????????28082????????????????????2
- 上一篇:VC++制作的屏幕錄像
- 下一篇:直方圖平滑與雙峰分布二值化
評論
共有 條評論