- int SetLocalIp( const char *ipaddr )
- {
- int sock_set_ip;
- struct sockaddr_in sin_set_ip;
- struct ifreq ifr_set_ip;
- bzero( &ifr_set_ip,sizeof(ifr_set_ip));
- if( ipaddr == NULL )
- return -1;
- if(sock_set_ip = socket( AF_INET, SOCK_STREAM, 0 ) == -1);
- {
- perror("socket create failse...SetLocalIp!/n");
- return -1;
- }
- memset( &sin_set_ip, 0, sizeof(sin_set_ip));
- strncpy(ifr_set_ip.ifr_name, "eth0", sizeof(ifr_set_ip.ifr_name)-1);
- sin_set_ip.sin_family = AF_INET;
- sin_set_ip.sin_addr.s_addr = inet_addr(ipaddr);
- memcpy( &ifr_set_ip.ifr_addr, &sin_set_ip, sizeof(sin_set_ip));
- if( ioctl( sock_set_ip, SIOCSIFADDR, &ifr_set_ip) < 0 )
- {
- perror( "Not setup interface/n");
- return -1;
- }
- //设置激活标志
- ifr_set_ip.ifr_flags |= IFF_UP |IFF_RUNNING;
- //get the status of the device
- if( ioctl( sock_set_ip, SIOCSIFFLAGS, &ifr_set_ip ) < 0 )
- {
- perror("SIOCSIFFLAGS");
- return -1;
- }
- close( sock_set_ip );
- return 0;
- }
socket 修改本机IP
最新推荐文章于 2023-12-06 14:34:00 发布