【转载】linux C …
// setip.h
#ifndef _INCLUDE_SETIP_H_
#define _INCLUDE_SETIP_H_ // 设置IP地址 int setip(char *ip); // 获取IP地址 char* getip(char *ip_buf); #endif // end setip.h
//setip.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/ioctl.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<net/if.h>
#include "setip.h"
//设置IP地址
int setip(char* ip)
{
struct ifreq temp;
struct sockaddr_in *addr;
int fd = 0;
int ret = -1;
strcpy(temp.ifr_name, "eth0");
if((fd=socket(AF_INET, SOCK_STREAM, 0))<0)
{
return -1;
}
addr = (struct sockaddr_in *)&(temp.ifr_addr);
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(ip);
ret = ioctl(fd, SIOCSIFADDR, &temp);
close(fd);
if(ret < 0)
return -1;
return 0;
}
// 获取IP地址
char* getip(char* ip_buf)
{
struct ifreq temp;
struct sockaddr_in *myaddr;
int fd = 0;
int ret = -1;
strcpy(temp.ifr_name, "eth0");
if((fd=socket(AF_INET, SOCK_STREAM, 0))<0)
{
return NULL;
}
ret = ioctl(fd, SIOCGIFADDR, &temp);
close(fd);
if(ret < 0)
return NULL;
myaddr = (struct sockaddr_in *)&(temp.ifr_addr);
strcpy(ip_buf, inet_ntoa(myaddr->sin_addr));
return ip_buf;
}
int main(int argc, char * argv[])
{
char * ip = "172.20.223.117";
char buf[16]="";
setip(argv[1]);
printf("ipaddr=%s/n",getip(buf));
return 0;
}
// end setip.c
【转载】linux C …的更多相关文章
- [转载]Linux进程调度原理
[转载]Linux进程调度原理 Linux进程调度原理 Linux进程调度的目标 1.高效性:高效意味着在相同的时间下要完成更多的任务.调度程序会被频繁的执行,所以调度程序要尽可能的高效: 2.加强交 ...
- [转载]Linux下非root用户如何安装软件
[转载]Linux下非root用户如何安装软件 来源:https://tlanyan.me/work-with-linux-without-root-permission/ 这是本人遇到的实际问题,之 ...
- [转载]Linux 命令详解:./configure、make、make install 命令
[转载]Linux 命令详解:./configure.make.make install 命令 来源:https://www.cnblogs.com/tinywan/p/7230039.html 这些 ...
- [转载]Linux缓存机制
[转载]Linux缓存机制 来源:https://blog.csdn.net/weixin_38278334/article/details/96478405 linux下的缓存机制及清理buffer ...
- [转载] Linux启动过程详解-《别怕Linux编程》之八
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket.为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. = ...
- [转载]Linux命令笔记
*以下内容均来自于网络转载,感谢原作者分享 <对Linux新手非常有用的20个命令> 传送门 英文原文为“Switching From Windows to Nix or a Newbie ...
- 转载 linux内核 asmlinkage宏
转载http://blog.chinaunix.net/uid-7390305-id-2057287.html 看一下/usr/include/asm/linkage.h里面的定义:#define a ...
- [转载] Linux下高并发socket最大连接数所受的各种限制
原文: http://mp.weixin.qq.com/s?__biz=MzAwNjMxNjQzNA==&mid=207772333&idx=1&sn=cfc8aadb422f ...
- [转载]linux下svn常用指令
一下内容转载于:http://blog.chinaunix.net/space.php?uid=22976768&do=blog&id=1640924.这个总结的很好~ windows ...
- [转载]Linux LVM硬盘管理及LVM扩容
最近项目中一直在用Linux,其中涉及到了Linux的LVM,本来想自己写一篇关于LVM的文章,搜了一下,发现了一篇更好的,转载过来,也感谢作者gaojun 原文Linux LVM硬盘管理及LVM扩容 ...
随机推荐
- jquery判断按钮是否被选中了
<script type="text/javascript"> function genjin_view2(elm){ if($(elm).attr("che ...
- 用超链接a来提交form表单
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Redux-Saga学习心得
# Redux Saga ## 简述- Reducers负责处理action的state更新:- Sagas负责协调那些复杂或异步的操作. ## 安装 npm install --save redux ...
- meta小结
mate 标签定义及使用说明 元数据(Metadata)是数据的数据信息. 标签提供了 HTML 文档的元数据.元数据不会显示在客户端,当时会被浏览器解析. META元素通常用于指定网页的描述,关键词 ...
- Log4Net不同日志类型写入到不同文件
1. 一直在用log4net,从来没有自己整理过.实践出真知,只有自己整理过才能真正掌握. 2. log4net,应该读logfornet,以前一直说log4,log4............ 安装 ...
- Unity 游戏框架搭建 (四) 简易有限状态机
为什么用有限状态机? 之前做过一款跑酷游戏,跑酷角色有很多状态:跑.跳.二段跳.死亡等等.一开始是使用if/switch来切换状态,但是每次角色添加一个状态(提前没规划好),所有状态处理相关的代码 ...
- 一个Ruby静态代码分析器 rubocop
A Ruby static code analyzer, based on the community Ruby style guide. http://rubocop.readthedocs.io ...
- Java IO学习笔记(二)缓冲流
处理流:包在别的流上的流,可以对被包的流进行处理或者提供被包的流不具备的方法. 一.缓冲流:套接在相应的节点流之上,带有缓冲区,对读写的数据提供了缓冲的功能,提高读写效率,同时增加一些新的方法.可以减 ...
- Watson Product
This article will discuss Watson related products or services. I will add more detailed information ...
- 64位Win10系统安装Mysql5.7.11
最近在装了64位Win10系统的mac book笔记本上用mysql-installer-community-5.7.11.0安装Mysql5.7.11,在配置mysql server时老是卡住,报错 ...