Chinaunix首页 | 论坛 | 博客
  • 博客访问: 412154
  • 博文数量: 38
  • 博客积分: 2513
  • 博客等级: 少校
  • 技术积分: 471
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-22 13:35
文章分类

全部博文(38)

文章存档

2010年(4)

2009年(6)

2008年(28)

我的朋友

分类: LINUX

2008-06-26 18:08:18

持续更新中......


/****************************************************************************
*                                        COPYRIGHT (C) 2008, stavy.sun
*                           ALL RIGHTS RESERVED
*
*    This source code has been made available to you by stavy.sun. Anyone
*    receiving this source code is licensed under stavy.sun copyrights to
*    use it in any way including copying,modifying,compiling and redistributing
*    it either with or without modifications. Any person who transfers this source
*    code or any derivative work must include the stavy.sun copyright notice
*    and    this paragrvalsth in the transferred software.
*
*Author: stavy.sun   
*
*DateTime: 2008_06_26
*
*Descriptions:
*    trace module used in debug.

*Version        Description                            Date              Author
*   1.0          created                          2008_06_26    stavy.sun
****************************************************************************/
#include
#include
#include
#include
#include
#include




/****************************************************************************
*Description:
*    it is used to connect to specified host with socket.
*
*Parameters:
*    hostname:    host name
*    port:        port
*    sockfd:        socket handle returned if success
*
*Returns:
*    return 0 if success, 1 i failed.
*
****************************************************************************/
int sock_connect(char *hostname, int port, int *sockfd)
{
char tmpbuf[64];
int  sfd = 0;
struct sockaddr_in addr;
struct hostent *ph = 0;

    ph = gethostbyname(hostname);
    if ( ph == NULL ) {
        strcpy(tmpbuf, hostname);
    } else {
        strcpy(tmpbuf, (char*)inet_ntoa(*(struct in_addr*)ph->h_addr_list[0]));
    }

    addr.sin_family            = AF_INET;
    addr.sin_port            = htons(port);
    addr.sin_addr.s_addr    = inet_addr(tmpbuf);
    if(addr.sin_addr.s_addr == INADDR_NONE) {
        return 1;
    }

    sfd = socket(AF_INET,SOCK_STREAM,0);
    if ( sfd < 0 ) {
        return 1;
    }
     if ( connect(sfd, (struct sockaddr *)&addr, sizeof(addr)) ) {
        return 1;
    }
    *sockfd = sfd;

    return 0;
}




/****************************************************************************
*Description:
*    it is used to get local ipaddress.
*
*Parameters:
*    eth:    device name, eg: "eth0"
*    ipaddr:    ipaddress returned if success
*
*Returns:
*    return 0 if success, 1 if failed.
*
****************************************************************************/
int sock_ipaddr(char *eth, char *ipaddr)
{
struct ifreq req;
struct sockaddr_in *addr;
unsigned long ip = 0;
int sock = 0;

    if ( (!eth) || (!ipaddr) ) {
        return 1;
    }

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if ( sock < 0 ) {
        return 1;
    }

    strcpy(req.ifr_name, eth);
    if ( ioctl(sock,SIOCGIFADDR,&req) == -1 ) {
        return 1;
    }
    addr = (struct sockaddr_in *)&req.ifr_addr;
    ip = addr->sin_addr.s_addr;
    sprintf(ipaddr, "%d.%d.%d.%d", ip&0xff, (ip>>8)&0xff, (ip>>16)&0xff, (ip>>24)&0xff);

    return 0;
}




阅读(2030) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~