关闭

嵌入式 整型ip与点式ip互相转换

493人阅读 评论(0) 收藏 举报
分类:

字符串ip转换为整型ip

 char hostname[128];
 int block_time = 1;
 char *jms_ip = "10.10.2.59";
 struct sockaddr_in jms;
 JOSEPH_JMS_SERVER_T jms_server_info;
 interface_t ipnc_interface_t;
 
 strcpy(hostname,"time.asia.apple.com");
 jms_server_info.jms_port = 1999;

 inet_aton(jms_ip, (struct in_addr *)&jms.sin_addr.s_addr);
 jms_server_info.jms_ip =jms.sin_addr.s_addr;

 

 

整型ip转换为字符串IP

 char jms_port_info[64] = {0};
 char jms_ip_info[128] = {0};
 struct sockaddr_in jms_server_info;
 bzero(&jms_server_info,sizeof(jms_server_info));
 jms_server_info.sin_family = AF_INET;
 jms_server_info.sin_port =jms_server_in->jms_port;
 jms_server_info.sin_addr.s_addr =jms_server_in->jms_ip;

 sprintf(jms_ip_info,"%s",inet_ntoa(jms_server_info.sin_addr));
 sprintf(jms_port_info,"%d",jms_server_info.sin_port);
 printf("%s %d jms_ip_info is %s ;jms_port_info is%s \n",__FUNCTION__,__LINE__,jms_ip_info,jms_port_info);

 

 

 

 int sockfd,tmp;
 struct sockaddr_indest;
 char buf[512];
 struct in_addr in;
 int retry = 6;
 if ((sockfd = socket(AF_INET, SOCK_STREAM,IPPROTO_IP)) < 0) {
        perror("Socket");
        return -2;
    }
   bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_port = port;
 dest.sin_addr.s_addr = ip;

 
  printf("connecting to %s %d\n",inet_ntoa(dest.sin_addr),ntohs(dest.sin_port));

  tmp = connect(sockfd,(struct sockaddr *) &dest, sizeof(dest));
  if (tmp != 0) {
   close(sockfd);
   printf("server%s %d connect err!!!\n",inet_ntoa(dest.sin_addr),ntohs(dest.sin_port));
   return-3;
     }else{
      
      printf("server %s %dconnected\n",inet_ntoa(dest.sin_addr),ntohs(dest.sin_port));
     }
  
 return sockfd;

 

 

 struct sockaddr_in jdas;
 SOCK_ADDR jrm;
 void *ret; 
 jdas.sin_port = htons(atoi(jdas_port));
 inet_aton(jdas_ip, (struct in_addr *)&jdas.sin_addr.s_addr);

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1046132次
    • 积分:18266
    • 等级:
    • 排名:第521名
    • 原创:741篇
    • 转载:523篇
    • 译文:0篇
    • 评论:105条
    最新评论