コード例 #1
0
ファイル: MQTTS7G2.c プロジェクト: sobhamo/hello-world
int ConnectNetwork(Network* n, char* addr, unsigned int port)
{
  UINT status;
  UCHAR record_buffer[200];
  UINT record_count;
  UINT error_count = 0;

  status = nx_tcp_socket_create(n->my_ip, &n->my_socket, "MqttSocket", NX_IP_NORMAL, NX_DONT_FRAGMENT, NX_IP_TIME_TO_LIVE, 512, NX_NULL, NX_NULL);
  if (status)error_count++;
  status =  nx_tcp_client_socket_bind(&n->my_socket, NX_ANY_PORT, 10000);
  if (status)error_count++;

  if( strncmp("http://", addr, sizeof("http://")) == 0 ) {
      status = nx_dns_server_add(&g_dns0, IP_ADDRESS(8,8,8,8));
      if (status)error_count++;
      status = nx_dns_ipv4_address_by_name_get(&g_dns0, (UCHAR*)addr, (VOID *)&record_buffer[0], 200, &record_count, 400);
      if (status)error_count++;
      n->byte_ip = *((ULONG *)(record_buffer));
  } else {
      int ip1,ip2,ip3,ip4;
      sscanf(addr,"%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4);
      n->byte_ip = IP_ADDRESS(ip1,ip2,ip3,ip4);
  }
  status =  nx_tcp_client_socket_connect(&n->my_socket, n->byte_ip, port, 2000);
  if (status)error_count++;
  if (error_count != 0)
	  return -1;
  return 0;
}
コード例 #2
0
ファイル: ip_address.c プロジェクト: wolf623/c_code
int get_interface_ip_address(char *ifname)
{
	struct ifreq ifr;
	memset(&ifr, 0, sizeof(ifr));
	struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
	
	int socketfd;
	if((socketfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
	{
		fprintf(stderr, "create a socket failed, errno = %d\n", errno);
		perror("");
		return -1;
	}
	
	if(ioctl(socketfd, SIOCGIFADDR, &ifr) == -1)
	{
		fprintf(stderr, "ioctl call failed, errno = %d\n", errno);
		perror("");
		close(socketfd);
		return -1;
	}

	fprintf(stdout, "ipaddress: %u.%u.%u.%u\n", IP_ADDRESS(sin->sin_addr.s_addr));
	fprintf(stdout, "ipaddress: %s\n", inet_ntoa(sin->sin_addr));

	return 0;
}
コード例 #3
0
ファイル: ip_address.c プロジェクト: wolf623/c_code
int main(int argc, char *argv[])
{
#if 0
	printf("you input ip addrss: %s\n", argv[1]);
	struct in_addr ip;
	ip.s_addr = inet_addr(argv[1]);
	printf("ip_address(int): %d\n", ip.s_addr);

	printf("ip address: %u.%u.%u.%u\n", IP_ADDRESS(ip.s_addr));
	printf("ip_address: %s\n", inet_ntoa(ip));

	//char ipv6_txt[64]; 
	//inet_ntop(AF_INET6,  &ipv6_addr, ipv6_txt, sizeof(ipv6_txt));
	
	char ipv4_txt[32];
	unsigned int ipv4_addr;
	inet_pton(AF_INET, argv[1], &ipv4_addr);
	printf("ip address(inet_pton): %d\n", ipv4_addr);

	inet_ntop(AF_INET, &ipv4_addr, ipv4_txt, sizeof(ipv4_txt));
	printf("ip address(inet_ntop): %s\n", ipv4_txt);
#endif
	
	//get_interface_ip_address("eth0");
	printf("smm6 ip: %u.%u.%u.%u\n", IP_ADDRESS(0x7f000001));
	printf("smm7 ip: %u.%u.%u.%u\n", IP_ADDRESS(0x7f010108));
	printf("slot1 ip: %u.%u.%u.%u\n", IP_ADDRESS(0x7f010002));
	printf("slot11 ip: %u.%u.%u.%u\n", IP_ADDRESS(0x7f01000c));
	printf("ip?: %u.%u.%u.%u\n", IP_ADDRESS(0xf0000000));
	printf("ip??: %u.%u.%u.%u\n", IP_ADDRESS(0xe0000000));

	char addr[23] = {0};
	struct in_addr inaddr;
	inaddr.s_addr = 0xe0000000;
	char *paddr = inet_ntoa(inaddr);
	strcpy(addr, paddr);
	printf("??: %s\n", addr);




	return 0;
}